部署 Rails 5.2 + Puma + Capistrano3 + Nginx + Sidekiq 自动化部署

ping94 · 2018年06月08日 · 最后由 luodaoyi-github 回复于 2018年06月13日 · 7798 次阅读

自己整了一个个人网站,然后将网站部署到了阿里云服务器上。由于使用的 rails 和 Capistrano3 这些都是最新的版本,网上的一些资料都有些过时,遇到很多问题,然后只能耐心的去看官方文档,整个部署过程整理了一下笔记以供参考

一、部署环境

  • centos7(阿里云服务器)
  • Capistrano 3.10
  • Ruby2.4.4
  • Rails 5.2
  • Puma3.11
  • Nginx1.13
  • Sidekiq5.1.3(monit 5.25)

ruby、nginx、redis 和 monit 的安装过程这里略过

二、初始化部署项目并配置 Capistrano(以部署 production 环境为例)

建好部署项目后在 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'
初始化 capistrano
$ cap install

cap T 可用来查看可执行任务列表

这里直接把代码的链接贴出来了 --> 传送门

三、部署项目

1、检查需要的文件和目录

cap production deploy:check

根据提示创建对应文件或文件夹

新建 shared/config/database.yml 文件
production:
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: xxxxxxx
  host: localhost
  database: myblog_production
新建 master.key 文件

将本地项目 config/master.key 中的内容复制进去。

2、部署并启动 puma

$ cap production puma:config #第一次deploy之前必须将运行此命令,否则puma无法启动
$ cap production deploy
$ cap production puma:start

部署之后最好检查一下 puma 是否正常启动

$ cap production puma:status

3、有新代码提交后重新部署

$ cap production deploy
$ cap production puma:restart

四、nginx 配置

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;
  }
}

感谢 支持 明天去把我的改了用 puma

ping94 关闭了讨论。 10月15日 16:12
ping94 重新开启了讨论。 10月15日 16:12
需要 登录 后方可回复, 如果你还没有账号请 注册新账号