🐻搭建Swift服务器:Ubuntu16.04 + vapor + swift5.2.2 release

4,000 阅读2分钟

时间:2020年4月

如果你看了官方文档以及其他人的博客没成功 ,请参考此文

前言:若不想折腾,劝君还是用Go吧,配置简单,几个命令配置完成。 铁了心要用vapor,你可能需要一天时间,泡杯咖啡吧。

首先 Mac OS X 终端 SSH 登录远程LINUX服务器

在Mac中打开终端、看看是不是root状态、如果不是,则输入

sudo -i

输入root的登录密码

连接主机ssh

ssh root@18x.92.103.15x
password(服务器密码)

一、下载Swift:可能耗时几个小时:建议挂在后台

wget https://swift.org/builds/swift-5.2.2-release/ubuntu1604/swift-5.2.2-RELEASE/swift-5.2.2-RELEASE-ubuntu16.04.tar.gz

二、下载签名文件(Apple需要的)

wget https://swift.org/builds/swift-5.2.2-release/ubuntu1604/swift-5.2.2-RELEASE/swift-5.2.2-RELEASE-ubuntu16.04.tar.gz.sig

三、安装环境(Apple 需要)

sudo apt-get install clang libicu-dev

四、导出密钥

 wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -

五、更新密钥

 gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys Swift

六、验证签名

 gpg --verify swift-5.2.2-RELEASE-ubuntu16.04.tar.gz.sig 

七、解压、需要一会儿

tar xzf swift-5.2.2-RELEASE-ubuntu16.04.tar.gz 

八、关联路径

export PATH=~/swift-5.2.2-RELEASE-ubuntu16.04/usr/bin:"${PATH}"

九、校验

root@iZ2zeg7t7xxxx9lrZ:~# swift --version
Swift version 5.2.2 (swift-5.2.2-RELEASE)
Target: x86_64-unknown-linux-gnu

成功

下面 安装 git,耗时几分钟、或者一小时

apt install git

下面安装 toolbox 需要几分钟

git clone https://github.com/vapor/toolbox.git
cd toolbox
git checkout master

如果libcurl报错,用这个

sudo apt-get install libcurl4-openssl-dev

然后

swift build -c release --disable-sandbox

如果出问题,

error: missing LinuxMain.swift file in the Tests directory
![](https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2020/4/25/171acf03ca610177~tplv-t2oaga2asx-image.image)

试试这个:一个岛国的网址,其实就是在Tests目录下加个LinuxMain.swif文件

vim LinuxMain.swif

然后粘贴拷贝,以下代码:

import XCTest
@testable import MonoGeneratorTests

XCTMain([
     testCase(GeneratorTests.allTests),
])

这是可能会有warning,不必理会。

其他解决办法:

"missing LinuxMain.swift file in the Tests directory" 的问题本质是 Linux 平台缺少 Objective-C runtime 不能动态链接到测试框架导致的。在 build 后面加上 --enable-test-discovery 参数就好,没必要再新建一个 LinuxMain.swif。(PS:我没尝试)作者:juejin.cn/user/245376…

然后,拷贝到/usr/local/bin:

mv .build/release/vapor /usr/local/bin

校验:

vapor --help

嘿嘿搭建成功!

root@iZ2zeg7xxxy9lrZ:~/VaporToolbox/toolbox# vapor --help
Usage:  vapor<command> 

Vapor Toolbox (Server-side Swift web framework)

Commands:
       clean Cleans temporary files.
       xcode Opens an app in Xcode.
       build Builds an app in the console.
      heroku Commands for working with Heroku
  supervisor Commands for working with Supervisord
         new Generates a new app.
         run Runs an app from the console.

Use `vapor <command> [--help,-h]` for more information on a command.

嘿嘿,是不是有点意思啊。

输入:exit 断开SSH链接。

下文介绍开启一个简单Vapor服务器,然后通过IP访问。

加V备注:掘金;入群一起学习🐻