Vapor3项目从0到部署上线

2019-11-28  本文已影响0人  遥遥领先M

环境

目标

创建个 vapor 应用, 且使用 9090 端口运行
通过域名xxx.com访问到该vapor 应用`

在 ubantu 上部署应用

手动添加repo。

wget -q https://repo.vapor.codes/apt/keyring.gpg -O- | sudo apt-key add -
echo "deb https://repo.vapor.codes/apt $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/vapor.list
sudo apt-get update

安装相关环境

sudo apt-get update 
sudo apt-get upgrade
eval "$(curl -sL https://apt.vapor.sh)"
sudo apt-get install swift vapor
sudo apt-get install nginx
sudo apt-get install supervisor

编译项目

拉取 FootprintVapor 项目

git clone xxx.git

编译项目

cd FootprintVapor
vapor build --release

查看编译信息

/root/code/FootprintVapor/.build/release/Run serve  --env=production
./.build/release/Run serve --env=production

nginx

添加 nginx 配置

cd /etc/nginx/sites-enabled/
vim xxx.com;//你的域名

在 xxx.com配置文件中添加

server {
    server_name xxx.com; # 需要修改为你自己的域名
    listen 80;
    root /home/vapor/Hello/Public/; # 需要改为你自己项目的 Public, 当然你也可以不填写这一行
    try_files $uri @proxy; # 这句不要忘了
    location @proxy {
        proxy_pass http://127.0.0.1:9090;
        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;
    }
}

让配置生效

sudo service nginx restart
nginx 常用命令
sudo nginx -t //测试配置文件是否正确
sudo service nginx stop // 停止
sudo service nginx start // 启动
sudo service nginx restart // 重启
sudo service nginx reload// 重新加载

免费HTTPS证书

安装 Certbot
Certbot可以用于管理(申请、更新、配置、撤销和删除等)Let's Encrypt 证书。这里安装的是带 nginx 插件的 certbot:

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:certbot/certbot
sudo apt-get install -y python-certbot-nginx

申请证书
使用 certbot 命令为xxx.com申请 HTTPS 证书。--nginx选项表示 Web 服务器为 nginx,-d选项指定域名,-n选项表示非交互式运行命令。若去除-n选项,则终端会提醒你选择是否将 http 请求重定向为 https 请求。

certbot --nginx -d xxx.com -n --email 952241861@qq.com --agree-tos

证书申请成功之后。Let's Encrypt 证书的有效期只有 3 个月
更新证书只需要一条简单的命令就可以了:

sudo certbot renew

定时任务自动更新

Timers

Certbot 在 /lib/systemd/system/ 下生成了两个文件:certbot.servicecertbot.timer,一个是服务,一个是定时器。
certbot.service:

[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://letsencrypt.readthedocs.io/en/latest/
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true

可以看到也是运行 certbot -q renew 命令。
certbot.timer:

[Unit]
Description=Run certbot twice daily

[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true

[Install]
WantedBy=timers.target

每天 0 点和 12 点激活 certbot.service。其实不需要这么频繁的更新证书,而且在更新证书的时候我们加了钩子来关闭和开启 Nginx 服务,所以最好是在凌晨没人访问网站的时候更新证书,我们稍微修改一下:

[Unit]
Description=Run certbot every 05:00

[Timer]
OnCalendar=*-*-* 05:00:00
Persistent=true

[Install]
WantedBy=timers.target

每天凌晨 5 点更新一次证书。因为只有过期前 30 天才会申请更新,所以前 60 天这个任务什么都没干。

保存修改以后需要重启定时器:

systemctl daemon-reload
systemctl restart certbot.timer

supervisor

配置 supervisor
cd /etc/supervisor/conf.d
sudo vim FootprintVapor.conf 

FootprintVapor.conf 添加

[program:FootprintVapor]
command=/root/code/FootprintVapor/.build/release/Run serve --env=production
directory=/root/code/FootprintVapor/
user=root 
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

command: 运行的命令
directory: 项目的路径
user: 运行人,即你现在登入的用户名

保存文件后,然后让配置生效

sudo supervisorctl reread
sudo supervisorctl add FootprintVapor
sudo supervisorctl start FootprintVapor

ok 项目配置完成

supervisor常用命令
supervisorctl status
supervisorctl reload
supervisorctl update
supervisorctl stop
sudo supervisorctl stop all
sudo supervisorctl start all
上一篇下一篇

猜你喜欢

热点阅读