阿里云 ECS 服務(wù)器 Ubuntu 搭建 Swift Perfect

目錄

前言

一狠半、安裝 Swift

二取胎、安裝 Swift Perfect


前言

我的 Ubuntu 是 16.04 64 位的系統(tǒng)。

一、安裝 Swift

Swift 官網(wǎng):https://swift.org/download/#releases

右鍵復(fù)制鏈接地址

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 啟動文件中音半,方便直接調(diào)用 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 應(yīng)用入門項目

先安裝 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
這個可以忽略的斥铺,不關(guān)你的事啦彻桃!

查看一下文件的內(nèi)容 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)])

網(wǎng)頁有內(nèi)容 <html><title>Hello, world!</title><body>Hello, world!</body></html> 就是說,打開網(wǎng)頁后晾蜘,會一串 “Hello, world!” 出現(xiàn)邻眷。

繼續(xù),運行 .build/debug/PerfectTemplate 啟動服務(wù)

[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 私網(wǎng) 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>

已經(jīng)成功了驯镊!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子板惑,更是在濱河造成了極大的恐慌橄镜,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冯乘,死亡現(xiàn)場離奇詭異蛉鹿,居然都是意外死亡,警方通過查閱死者的電腦和手機往湿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惋戏,“玉大人领追,你說我怎么就攤上這事∠旆辏” “怎么了绒窑?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長舔亭。 經(jīng)常有香客問我些膨,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮纷宇,結(jié)果婚禮上崎脉,老公的妹妹穿的比我還像新娘。我一直安慰自己边败,他們只是感情好,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著噩峦,像睡著了一般。 火紅的嫁衣襯著肌膚如雪抽兆。 梳的紋絲不亂的頭發(fā)上识补,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機與錄音辫红,去河邊找鬼凭涂。 笑死,一個胖子當著我的面吹牛厉熟,可吹牛的內(nèi)容都是我干的导盅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼揍瑟,長吁一口氣:“原來是場噩夢啊……” “哼白翻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤滤馍,失蹤者是張志新(化名)和其女友劉穎岛琼,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體巢株,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡槐瑞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了阁苞。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片困檩。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖那槽,靈堂內(nèi)的尸體忽然破棺而出悼沿,到底是詐尸還是另有隱情,我是刑警寧澤骚灸,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布糟趾,位于F島的核電站,受9級特大地震影響甚牲,放射性物質(zhì)發(fā)生泄漏义郑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一丈钙、第九天 我趴在偏房一處隱蔽的房頂上張望非驮。 院中可真熱鬧,春花似錦著恩、人聲如沸院尔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邀摆。三九已至,卻和暖如春伍茄,著一層夾襖步出監(jiān)牢的瞬間栋盹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工敷矫, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留例获,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓曹仗,卻偏偏與公主長得像榨汤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子怎茫,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1收壕、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,982評論 3 119
  • 上一本讀的是日本作者寫的《麥肯錫教我的思考武器》妓灌。想要繼續(xù)深入學習如何思考,于是想到了這本《六頂思考帽》蜜宪,應(yīng)該有很...
    思維拼圖閱讀 1,437評論 0 5
  • 隨著一個夏天 一波波的流火炎熱 我的心 也跟著一次次憧憬 焦急 與盼望 可最后 都是以一次次的失落 告終 八月來了...
    寞霏閱讀 184評論 1 5
  • 文 / 圓靖 臘月二十七早上三妹到達南充人民醫(yī)院虫埂,而此時遠在家鄉(xiāng)的我們正熱火朝天的辦年貨,籌備著春節(jié)前的一切大小瑣...
    圓立青閱讀 369評論 0 2