自己整了一个个人网站,然后将网站部署到了阿里云服务器上。由于使用的 rails 和 Capistrano3 这些都是最新的版本,网上的一些资料都有些过时,遇到很多问题,然后只能耐心的去看官方文档,整个部署过程整理了一下笔记以供参考
ruby、nginx、redis 和 monit 的安装过程这里略过
建好部署项目后在 Gemfile 中添加 Capistrano 相关 gem
gem "capistrano", "~> 3.10", require: false
gem 'capistrano-rails', '~> 1.3', require: false
gem 'capistrano3-puma', '~> 3.1', require: false
gem 'capistrano-sidekiq', '~> 1.0'
$ cap install
cap T
可用来查看可执行任务列表
这里直接把代码的链接贴出来了 --> 传送门
cap production deploy:check
根据提示创建对应文件或文件夹
production:
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: xxxxxxx
host: localhost
database: myblog_production
将本地项目 config/master.key 中的内容复制进去。
$ cap production puma:config #第一次deploy之前必须将运行此命令,否则puma无法启动
$ cap production deploy
$ cap production puma:start
部署之后最好检查一下 puma 是否正常启动
$ cap production puma:status
$ cap production deploy
$ cap production puma:restart
nginx 常用命令
$ sbin/nginx #启动
$ sbin/nginx -t #检查配置文件
$ sbin/nginx –s reload #重启
$ sbin/nginx –s stop #停止
upstream puma_myblog_production {
server unix:/var/www/myblog/shared/tmp/sockets/puma.sock fail_timeout=0;
}
# 当用户使用HTTP的URL访问时重定向到https的url上去
server {
listen 80;
server_name www.xxx.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.xxx.com;
root /var/www/myblog/current/public;
try_files $uri/index.html $uri @puma_myblog_production;
# HTTPS 配置begin
# CA证书是在阿里云上购买的免费版的,有点不太稳定
ssl on;
ssl_certificate ../cert/214690888480272.pem;
ssl_certificate_key ../cert/214690888480272.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# https 配置end
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
location @puma_myblog_production {
proxy_pass http://puma_myblog_production;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on; # Optional
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Host $host;
# limit_req zone=one;
access_log /var/www/myblog/shared/log/nginx.access.log;
error_log /var/www/myblog/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}