Rails 5.2 无法在 Apache 下以生产模式启动 Puma

Rails 5.2 Trouble Starting Puma Under Apache in Production Mode

所以我是 Apache 的新手。 (来自 Passenger。)我从 Capistrano 部署,看起来 Puma 已经加载...

01:21 puma:start
      using conf file /var/www/mfta/shared/puma.rb
      01 /usr/share/rvm/bin/rvm default do bundle exec puma -C /var/www/mfta/shared/puma.rb --daemon
      01 Puma starting in single mode...
      01
      01 * Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
      01
      01 * Min threads: 4, max threads: 16
      01
      01 * Environment: production
      01
      01 * Daemonizing...
      01

...但它还没有。

Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

如果我 运行 "bundle exec puma -e production"... 它工作正常但结果是 "listening" 而不是 "daemonizing."

Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.2-p47), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

这是我的 deploy.rb:

set :stage,           :production
set :deploy_via,      :remote_cache
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:start'
    end
  end

这是来自我的 apache2 虚拟主机文件:

 <VirtualHost *:80>
    NameVirtualHost 99.99.99.99
    ServerName myapp.org
    DocumentRoot /var/www/myapp/current/public
    ServerSignature Off
    ProxyRequests Off
    <Proxy *>
        Order Allow,Deny
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyVia On
</VirtualHost>

也许我应该告诉 Capistrano 或 Puma 用 Apache 做一些额外的事情?是否有一些额外的步骤将 Apache2 连接到 Puma Daemon?

如我所料...答案就在 Puma Bind 中。

puma.rb

set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"

我相信这是您将用于 NGINX 的设置...如果您使用的是 Apache,那么这应该与您的虚拟主机 ProxyPass 中的任何内容相匹配。在这种情况下:

ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

所以你的 deploy.rb 应该在绑定下:

set :puma_bind,       "tcp://0.0.0.0:3000"

因此您的 puma.bind 文件变为:

bind 'tcp://0.0.0.0:3000'

...这是默认值。或者你选择它是什么。

我敢肯定有十亿种方法可以做到这一点......但这是迄今为止对我最终有用的方法。