Search code examples
apacheruby-on-rails-5capistranopuma

Rails 5.2 Trouble Starting Puma Under Apache in Production Mode


So I'm brand new to Apache. (Coming from Passenger.) I deploy from Capistrano and it LOOKS like Puma has loaded...

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

...but it has not.

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

If I run "bundle exec puma -e production"... it works just fine but the result is "listening" instead of "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

This is from my 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

And this is from my apache2 virtual hosts file:

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

Maybe I should be telling Capistrano or Puma to do something extra with Apache? Is there some additional step to connecting Apache2 to the Puma Daemon?


Solution

  • As I suspected... the answer was in the Puma Bind.

    puma.rb

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

    This is the setting you would use for NGINX I believe... If you're using Apache then this should match whatever is in your Virtual Host ProxyPass. In this case:

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

    So your deploy.rb should have that under bind:

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

    Thus your puma.bind file becomes:

    bind 'tcp://0.0.0.0:3000'
    

    ...which is the default. Or whatever you choose it to be.

    I'm sure there are a billion ways to do this... but this is what's ultimately worked for me so far.