阿里云 ECS 服务器 Ubuntu 搭建 Swift Perf

2018-09-22  本文已影响137人  半纸渊

目录

前言

一、安装 Swift

二、安装 Swift Perfect


前言

我的 Ubuntu 是 16.04 64 位的系统。

一、安装 Swift

Swift 官网:https://swift.org/download/#releases

右键复制链接地址

Swift.org - Download Swift.jpg

使用 wget 下载 Swift 安装包

[root@:~]# wget https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz
--2018-09-22 16:16:46--  https://swift.org/builds/swift-4.2-release/ubuntu1604/swift-4.2-RELEASE/swift-4.2-RELEASE-ubuntu16.04.tar.gz
Resolving swift.org (swift.org)... 169.47.73.10
Connecting to swift.org (swift.org)|169.47.73.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 223291596 (213M) [application/x-gzip]
Saving to: ‘swift-4.2-RELEASE-ubuntu16.04.tar.gz’

swift-4.2-RELEASE-ubuntu16. 100%[=========================================>] 212.95M  8.08MB/s    in 26s     

2018-09-22 16:17:14 (8.11 MB/s) - ‘swift-4.2-RELEASE-ubuntu16.04.tar.gz’ saved [223291596/223291596]

修改 Swift 包的名称,放置在 home 目录下

[root@~]#  ls
swift-4.2-RELEASE-ubuntu16.04.tar.gz

[root@~]#  mv swift-4.2-RELEASE-ubuntu16.04.tar.gz swift-4.2.tar.gz
[root@~]#  ls
swift-4.2.tar.gz

[root@~]# mv swift-4.2.tar.gz /home/
[root@~]# ls /home/
swift-4.2.tar.gz

[root@~]# cd /home/

解压 Swift 安装包

[root@/home]# tar -zxvf swift-4.2.tar.gz 

[root@/home]# ls
swift-4.2-RELEASE-ubuntu16.04  swift-4.2.tar.gz

[root@/home]# mv swift-4.2-RELEASE-ubuntu16.04 swift-4.2

[root@/home]#ls
swift-4.2 swift-4.2.tar.gz

先更新 apt-get

[root@/home]# sudo apt-get update
...... // 等待更新完成
Get:27 http://mirrors.cloud.aliyuncs.com/ubuntu 
xenial-security/universe Translation-en [144 kB]
Fetched 37.9 MB in 6s (5,589 kB/s)                                    
Reading package lists... Done

安装 clang ,clang 是 Swift 的编译工具

[root@/home]# sudo apt-get -y install clang
......
Setting up llvm-3.8-runtime (1:3.8-2ubuntu4) ...
Setting up llvm-3.8 (1:3.8-2ubuntu4) ...
Setting up libjsoncpp1:amd64 (1.7.2-1) ...
Setting up libffi-dev:amd64 (3.2.1-4) ...
Setting up llvm-3.8-dev (1:3.8-2ubuntu4) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...

swift 可以直接运行了,查看安装的版本

[root@/home]# ./swift-4.2/usr/bin/swift --version
Swift version 4.2 (swift-4.2-RELEASE)
Target: x86_64-unknown-linux-gnu

把 swift 添加进 .bashrc 启动文件中,方便直接调用 swift 进行操作

[root@/home]# vim ~/.bashrc

文件末尾处,添加 export PATH=/home/swift-4.2/usr/bin:"${PATH}"

[root@/home]# cat ~/.bashrc | grep "swift"
export PATH=/home/swift-4.2/usr/bin:"${PATH}"

[root@/home]# source ~/.bashrc

使用 swift

[root@/home]# swift
Welcome to Swift version 4.2 (swift-4.2-RELEASE). Type :help for assistance.
  1> print("Hello world !") 
hello world !
  2>  :quit // 退出 swift

二、安装 Swift Perfect

安装依赖包

[root@/home]# sudo apt-get -y install openssl libssl-dev uuid-dev
......
Setting up openssl (1.0.2g-1ubuntu4.13) ...
Setting up zlib1g-dev:amd64 (1:1.2.8.dfsg-2ubuntu4.1) ...
Setting up libssl-dev:amd64 (1.0.2g-1ubuntu4.13) ...
Setting up libssl-doc (1.0.2g-1ubuntu4.13) ...
Setting up uuid-dev:amd64 (2.27.1-6ubuntu3.6) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...

好像完了......

来点示例玩玩,官方 Web 应用入门项目

先安装 git 用于下载 入门项目

[root@/home]# sudo apt-get -y install git
Unpacking git (1:2.7.4-0ubuntu1.4) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.4) ...
Setting up git (1:2.7.4-0ubuntu1.4) ...
[root@/home]# git --version
git version 2.7.4

克隆项目

[root@/home]# mkdir swiftPerfectLearn

[root@/home]# git clone https://github.com/PerfectlySoft/PerfectTemplate.git swiftPerfectLearn/
Cloning into 'swiftPerfectLearn'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 269 (delta 11), reused 33 (delta 9), pack-reused 234
Receiving objects: 100% (269/269), 60.50 KiB | 0 bytes/s, done.
Resolving deltas: 100% (130/130), done.
Checking connectivity... done.

移动文件位置,防止 home 上文件过于混乱方便操作

[root@/home]# mv PerfectTemplate swiftPerfectLearn/

[root@/home]# ls swiftPerfectLearn/
PerfectTemplate
[root@/home]# ls
swift-4.2  swift-4.2.tar.gz  swiftPerfectLearn

[root@/home]# ls swiftPerfectLearn/
PerfectTemplate
[root@/home]# ls swiftPerfectLearn/PerfectTemplate/
LICENSE  LICENSE.zh_CN  Package.swift  README.md  README.zh_CN.md  Sources

[root@/home]# cd swiftPerfectLearn/PerfectTemplate/

[root@/home/swiftPerfectLearn/PerfectTemplate]#

swift 编译项目

[root@.../PerfectTemplate]# swift build
/home/swift-4.2/usr/bin/swift-build: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory

处理 libcurl.so.4 错误

[root@.../PerfectTemplate]# sudo apt-get install libcurl3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-image-4.4.0-87-generic
  linux-image-extra-4.4.0-87-generic
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  libcurl3
0 upgraded, 1 newly installed, 0 to remove and 100 not upgraded.
Need to get 186 kB of archives.
After this operation, 564 kB of additional disk space will be used.
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates/main amd64 libcurl3 amd64 7.47.0-1ubuntu2.9 [186 kB]
Fetched 186 kB in 0s (7,268 kB/s)
Selecting previously unselected package libcurl3:amd64.
(Reading database ... 136968 files and directories currently installed.)
Preparing to unpack .../libcurl3_7.47.0-1ubuntu2.9_amd64.deb ...
Unpacking libcurl3:amd64 (7.47.0-1ubuntu2.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Setting up libcurl3:amd64 (7.47.0-1ubuntu2.9) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...

重新运行 swift build

[root@.../PerfectTemplate]# swift build
......
Resolving https://github.com/PerfectlySoft/Perfect-Crypto.git at 3.1.2
Cloning https://github.com/PerfectlySoft/Perfect-CZlib-src.git
Resolving https://github.com/PerfectlySoft/Perfect-CZlib-src.git at 0.0.4
warning: PackageDescription API v3 is deprecated and will be removed in the future; used by package(s): LinuxBridge, COpenSSL
Compile PerfectCZlib zutil.c
Compile PerfectCZlib uncompr.c
Compile PerfectCZlib trees.c
Compile PerfectCZlib inftrees.c
......
Compile Swift Module 'PerfectNet' (7 sources)
Compile Swift Module 'PerfectHTTP' (10 sources)
Compile Swift Module 'PerfectHTTPServer' (18 sources)
Compile Swift Module 'PerfectTemplate' (1 sources)
Linking ./.build/x86_64-unknown-linux/debug/PerfectTemplate

warning warning: PackageDescription API v3 is deprecated and will be removed in the future; used by package(s): LinuxBridge, COpenSSL
这个可以忽略的,不关你的事啦!

查看一下文件的内容 main.swift

[root@/home]# cat swiftPerfectLearn/PerfectTemplate/Sources/PerfectTemplate/main.swift | grep -v "^ *//" | grep -v "^$"
import PerfectHTTP
import PerfectHTTPServer
func handler(request: HTTPRequest, response: HTTPResponse) {
    // Respond with a simple message.
    response.setHeader(.contentType, value: "text/html")
    response.appendBody(string: "<html><title>Hello, world!</title><body>Hello, world!</body></html>")
    // Ensure that response.completed() is called when your processing is done.
    response.completed()
}
var routes = Routes()
routes.add(method: .get, uri: "/", handler: handler)
routes.add(method: .get, uri: "/**",
           handler: StaticFileHandler(documentRoot: "./webroot", allowResponseFilters: true).handleRequest)
try HTTPServer.launch(name: "localhost",
                      port: 8181,
                      routes: routes,
                      responseFilters: [
                        (PerfectHTTPServer.HTTPFilter.contentCompression(data: [:]), HTTPFilterPriority.high)])

网页有内容 <html><title>Hello, world!</title><body>Hello, world!</body></html> 就是说,打开网页后,会一串 “Hello, world!” 出现。

继续,运行 .build/debug/PerfectTemplate 启动服务

[root@.../PerfectTemplate]# .build/debug/PerfectTemplate
[INFO] Starting HTTP server localhost on :::8181

ctrl + C 退出!

安装 elinks

[root@.../PerfectTemplate]# sudo apt-get -y install elinks
......
Setting up libfsplib0 (0.11-2) ...
Setting up liblua5.1-0:amd64 (5.1.5-8ubuntu1) ...
Setting up libtre5:amd64 (0.8.0-4) ...
Setting up elinks-data (0.12~pre6-11build2) ...
Setting up elinks (0.12~pre6-11build2) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...

我使用的是 Xsell 6 终端,先在一个命令行运行 Perfect

[root@.../PerfectTemplate]# .build/debug/PerfectTemplate
[INFO] Starting HTTP server localhost on :::8181

再打开一个命令行,用 elinks 打开就可以看到了

[root@.../PerfectTemplate]# elinks 私网 IP:8181 
Hello, world!

q 退出 elinks !

动画演示

perfect start

如果不想使用 elinks 可以直接

[root@/.../PerfectTemplate]# curl http://127.0.0.1:8181
<html><title>Hello, world!</title><body>Hello, world!</body></html>

已经成功了!

上一篇下一篇

猜你喜欢

热点阅读