MENU

在Ubuntu18.04上部署 Vapor💧和PostgreSQL🐘

August 27, 2019 • Read: 358 • Note

环境需求

  • Ubuntu 18.04 x64
  • Swift 5.0.2
  • Vapor Toolbox: 3.1.10
  • Nginx
  • Supervisor

搭建完成云服务器之后,使用SSH进入Ubuntu,进行下面的安装

进入root用户

$ sudo -i

Nginx

$ sudo apt-get update
$ sudo apt-get install nginx

添加防火墙规则,允许80端口TCP通信

$ sudo ufw allow 'Nginx HTTP'
$ sudo ufw status

检查Nginx的运行状态

$ systemctl status nginx

输出以下内容证明运行成功

● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2019-08-26 16:20:37 UTC; 1min 49s ago

部署完成后即可在浏览器通过IP访问服务器,出现欢迎页则代表成功,如果未成功的话,Digital Ocean上有一篇更加详细的说明

nginx-welcome

Swift

首先安装Swift所需的依赖库

$ sudo apt-get install clang libicu-dev

Swift 官网下载最新的Ubuntu 18.04 release版本快照

$ wget https://swift.org/builds/swift-5.0.2-release/ubuntu1804/swift-5.0.2-RELEASE/swift-5.0.2-RELEASE-ubuntu18.04.tar.gz

然后下载对应的Signature签名文件

$ wget https://swift.org/builds/swift-5.0.2-release/ubuntu1804/swift-5.0.2-RELEASE/swift-5.0.2-RELEASE-ubuntu18.04.tar.gz.sig

把Swift的GPG key导入当前的钥匙环

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

出现以下输出代表操作成功

gpg: key 7638F1FB2B2B08C4: public key "Swift Automatic Signing Key #2 <swift-infrastructure@swift.org>" imported
gpg: key EF5430F071E1B235: public key "Swift 4.x Release Signing Key <swift-infrastructure@swift.org>" imported

验证GPG签名完整性

$ gpg --verify swift-5.0.2-RELEASE-ubuntu18.04.tar.gz.sig

验证成功

gpg: Good signature from "Swift 5.x Release Signing Key <swift-infrastructure@swift.org>"

解压Swift快照

$ tar zxvf swift-5.0.2-RELEASE-ubuntu18.04.tar.gz

修改文件夹的名字

$ mv swift-5.0.2-RELEASE-ubuntu18.04 swift-5.0.2

swift可执行文件添加到环境变量

$ export PATH=/root/swift-5.0.2/usr/bin:"${PATH}"

输入swift出现欢迎提示证明安装成功

Welcome to Swift version 5.0.2 (swift-5.0.2-RELEASE).
Type :help for assistance.

1> print("Hey, weslie")

Hey, weslie

Vapor

添加Vapor的脚本

$ eval "$(curl -sL https://apt.vapor.sh)"

安装Vapor

$ sudo apt-get install swift vapor

创建新的Vapor项目

$ vapor new hello

vapor-deploy-completed

配置Niginx转发Vapor App

修改默认配置文件

$ vim /etc/nginx/sites-available/default

替换成以下配置

server {
     listen 80 default_server;
     listen [::]:80 default_server;root /var/www/html;index index.html index.htm index.nginx-debian.html;server_name _;
     try_files $uri @proxy;
     location @proxy {
             proxy_pass http://127.0.0.1:8080;
             proxy_pass_header Server;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_pass_header Server;
             proxy_connect_timeout 3s;
             proxy_read_timeout 10s;
     }
}

重启Nginx并运行Vapor

$ sudo systemctl restart nginx
$ cd ~/hello
$ vapor build
$ vapor run

在浏览器中输入http://<IP Address>/hello,出现 "hello world"则部署成功

Supervisor

配置Supervisor,使Vapor进程默认运行,如果被其他错误等终止则自动重启Vapor

$ sudo apt-get update
$ sudo apt-get install supervisor

配置conf文件

$ vim /etc/supervisor/conf.d/hello.conf

添加以下内容

[program:hello]
command=vapor run --env=production
directory=/root/hello        # 修改vapor项目目录
autorestart=true
user=root                      # vapor项目所属用户用户名
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

运行

$ sudo supervisorctl reread
$ sudo supervisorctl add hello
$ sudo supervisorctl start hello

以上就完成了在服务器上部署Vapor,Vapor项目代码变更后重新运行需要重启supervisor

Last Modified: September 20, 2019
Archives Tip
QR Code for this page
Tipping QR Code
Leave a Comment

3 Comments
  1. 啊

    用的什么云服务器?

    1. @啊我用的腾讯云,其实服务器无所谓的,我是因为主要用户都在国内而且他有大学生优惠所以才用的。

  2. doom doom

    PostgreSQL还没写了