Search code examples
ruby-on-railsnginxcapistranopuma

Command to start rails server when using Puma + Nginx


I just pushed my Rails app to my remote server using Capistrano. The app is deployed under the deploy user's home directory - /home/deploy/my_app/current/

I'm using puma as a web server and have nginx configured to look for a socket to which it should forward all incoming web traffic. Snippet from my nginx site config file -

upstream app {
    server unix:/home/deploy/my_app/shared/sockets/puma.sock fail_timeout=0;
}

Correspondingly, my puma.rb file uses bind to create the same socket mentioned above.

# config/puma

# Change to match your CPU core count
workers Integer(ENV["PUMA_WORKERS"] || 2)

# Min and Max threads per worker
threads 1, Integer(ENV["PUMA_MAX_THREADS"] || 5)

DEPLOY_ROOT = "/home/deploy/my_app"
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{DEPLOY_ROOT}/shared"

# Default to production
port ENV["PORT"] || 3000
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"

# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app

on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end

The last piece is to start my Rails app. What is the command that I should use to start it? I tried

SECRET_KEY_BASE=blahblahblah rails s -e production --daemon

however this did not create a socket at the path mentioned above. I also confirmed it by trying to hit my URL and nginx threw up a 502 bad gateway.

Should I be using puma -C config/puma.rb instead? What's the difference, I assume rails server starts puma anyway. And how do I get that socket to be created?

Thanks!

EDIT: When I search for the puma process that's running, it tells me it's listening on a TCP port. I'm not sure how that differs from sockets, but could that be the problem?

[01:08:09] deploy:~  > ps aux | grep puma
deploy   12132  0.0  7.6 523736 78160 ?        Sl   00:59   0:00 puma 3.4.0 (tcp://localhost:3000) [/]
deploy   12314  0.0  0.0  14512   936 pts/0    S+   01:08   0:00 grep puma

Solution

  • No, rails 4's default server is webrick and yes you should use puma -C config/puma.rb