#Vapor3系列之hello小项目从0到部署上线
更多文章请到 Swift 之 Vapor3 系列目录
环境
- macos 10.14.5 & swift 5.0.1
- xcode 10.2.1
- ubantu 16.04.6 & swift 5.0
目标
- 创建个 vapor 应用, 且使用
9090
端口运行 - 通过域名
hello.xxx.com
访问到该 vapor 应用
项目创建
创建应用
-
在 mac 端创建 vapor 应用 hello
$ vapor new hello $ cd hello $ vapor xcode -y # 依赖拉取完成后,会自动生成 xcode 项目且自动打开
然后目录的内容如下:
$ ls -all drwxr-xr-x 17 oheroj staff 544 Jul 18 22:51 . drwxr-xr-x 10 oheroj staff 320 Jul 18 22:36 .. drwxr-x--- 6 oheroj staff 192 Jul 18 22:51 .build drwxr-xr-x 3 oheroj staff 96 Jul 18 22:37 .circleci -rw-r--r-- 1 oheroj staff 54 Jul 18 22:37 .dockerignore drwxr-xr-x 12 oheroj staff 384 Jul 18 22:52 .git -rw-r--r-- 1 oheroj staff 62 Jul 18 22:37 .gitignore -rw-r--r-- 1 oheroj staff 516 Jul 18 22:37 CONTRIBUTING.md -rw-r--r-- 1 oheroj staff 5664 Jul 18 22:51 Package.resolved -rw-r--r--@ 1 oheroj staff 696 Jul 18 22:37 Package.swift drwxr-xr-x 3 oheroj staff 96 Jul 18 22:37 Public -rw-r--r--@ 1 oheroj staff 908 Jul 18 22:37 README.md drwxr-xr-x 4 oheroj staff 128 Jul 18 22:37 Sources drwxr-xr-x 5 oheroj staff 160 Jul 18 22:37 Tests -rw-r--r-- 1 oheroj staff 92 Jul 18 22:37 cloud.yml drwxr-xr-x@ 51 oheroj staff 1632 Jul 18 22:51 hello.xcodeproj -rw-r--r-- 1 oheroj staff 1297 Jul 18 22:37 web.Dockerfile
xcode 项目:
-
修改运行端口为 9090:
在
configure.swift
文件中添加如下代码import FluentSQLite import Vapor /// Called before your application initializes. public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { let serverConfig = NIOServerConfig.default(hostname: "127.0.0.1", port: 9090) services.register(serverConfig) ....
-
运行项目,注意选择
然后用 Chrome 访问
http://127.0.0.1:9090
:OK 项目完工!
代码上传到 github
在 github 创建一个空仓库,然后将代码提交到这个仓库中, 比如我创建了SwiftOldBird/hello
创建完成后,用终端切到 hello 应用根目录下
$ git add .
$ git commit -m "first commit"
$ git remote add origin https://github.com/SwiftOldBird/hello.git
$ git push -u origin master
Enumerating objects: 34, done.
Counting objects: 100% (34/34), done.
Delta compression using up to 4 threads
Compressing objects: 100% (28/28), done.
Writing objects: 100% (34/34), 6.88 KiB | 1.72 MiB/s, done.
Total 34 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/SwiftOldBird/hello.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
配置域名解析
记录值填写你的服务器地址
首先云服务在国内需要备案才能被与域名关联。
在 ubantu 上部署应用
安装相关环境
$ 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
编译项目
-
拉取 hello 项目
$ git clone https://github.com/SwiftOldBird/hello.git
-
编译项目
$ cd hello $ vapor build --release
nginx
添加 nginx 配置
$ cd /etc/nginx/site-avaliables/
$ sudo vim default
在 default 配置文件中添加
server {
server_name hello.loveli.site; # 需要修改为你自己的域名
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 service nginx stop
sudo service nginx start
sudo service nginx restart
supervisor
配置 supervisor
$ cd /etc/supervisor/conf.d
$ touch hello.conf
$ sudo vim hello.conf
给 hello.conf 添加以下文件
[program:hello]
command=/root/code/hello/.build/release/Run serve --env=production
directory=/root/code/hello/
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 hello
sudo supervisorctl start hello
ok 项目配置完成,你可以愉快的在浏览器输入 http://hello.xxx.com
了
supervisor常用命令
sudo supervisorctl stop all
sudo supervisorctl start all
sudo supervisorctl reread
sudo supervisorctl add hello
sudo supervisorctl start hello
文章到此结束,如果有遇到问题或者文章不对的地方,请在文章底部留言. 当然也可以关注微信公众号: