使用Capistrano和Puma部署Rails 5 API

时间:2018-03-15 22:13:49

标签: ruby-on-rails nginx deployment capistrano puma

我试图将带有Capistrano的Rails 5 API部署到AWS服务器,我遇到了一些问题...

我已经关注了this教程,因此我尝试使用以下命令进行部署:

$ cap production deploy

产生以日志结尾的日志:

00:20 deploy:log_revision
      01 echo "Branch master (at <uuid>) deployed as release 20180315215050 by <user>" >> /<path_to_app>/revisions.log
    ✔ 01 <user>@<Public DNS (IPv4)> 0.206s

我想这一切似乎都好。但是当我查询服务器时,我收到了来自nginx的502 Bad Gateway。 当我$ ps aux | grep puma$ ps aux | grep rails时,我找不到任何正在运行的流程......

我的配置文件如下:

Capfile

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Load the SCM plugin appropriate to your project:
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git

require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/puma'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

配置/ deploy.rb

# config valid for current version and patch releases of Capistrano
lock '~> 3.10.1'

set :application, '<app_name>'
set :repo_url, '<git_repo>'
set :branch, 'master'
set :deploy_to, '/<path_to_app>'
set :pty, true
set :ssh_options, {
    forward_agent: true,
    auth_methods: ['publickey'],
    keys: ['/<path_to_key>.pem']
}
set :linked_files, %w{config/database.yml config/application.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
set :keep_releases, 5
set :rvm_type, :user
set :rvm_ruby_version, '2.5.0'

set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [1, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false

@server:/ etc / nginx / sites-available / default

upstream app {
  # Path to Puma SOCK file, as defined previously
  server unix:/<path_to_app>/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;

  root /<path_to_app>/public;

  try_files $uri/index.html $uri @app;

  location / {
    proxy_set_header X-Forwared-Proto $scheme;
    proxy_set_header X-Forwared-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

有人能弄明白我做错了吗?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

您应手动设置<...>参数。

config / deploy.rb

set :application, '<app_name>'
set :repo_url, '<git_repo>'
set :deploy_to, '/<path_to_app>'

set :ssh_options, {
    forward_agent: true,
    auth_methods: ['publickey'],
    keys: ['/<path_to_key>.pem']
}

@server:/ etc / nginx / sites-available / default

server unix:/<path_to_app>/shared/tmp/sockets/puma.sock fail_timeout=0;

root /<path_to_app>/public;