Puma, Nginx and rails5 not working

May 27, 2017 184 views
Nginx Ruby on Rails Ubuntu

I've followed this guide https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
step by step, and I've this error

"1256#0: *12 connect() to unix:///home/me/app/shared/tmp/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream"

If I create the puma.sock file, and I give, as a test, 777 permission, I have this error:
" [error] 1256#0: *14 connect() to unix:///home/me/app/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream"

If I follow this other tutorial, https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma, the file uploaded to shared/puma.rb contains wrong values, the path of the socket is pointing to /var/www instead to /home/me

this is my puma config file:

# Change to match your CPU core count
workers 1

# Min and Max threads per worker
#threads 1, 6

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

# Default to production
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
  ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end

This is my deploy file:

# config valid only for current version of Capistrano
lock '3.8.1'

set :application, 'app'
set :repo_url, 'git@example.com:me/my_repo.git'

set :pty,             true
set :use_sudo,        false
set :stage,           :production
set :deploy_via,      :remote_cache
set :ssh_options,     { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }

set :repo_url, 'ssh://me@10.10.10.10/home/me/app.git'
set :deploy_to, "/home/me/app"

set :rbenv_ruby, '2.4.1'

# Default value for :format is :pretty
set :format, :pretty

set :bundle_without, %w{development test}.join(' ')
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

set :keep_releases, 5

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
  after :finishing, 'deploy:cleanup'
  after 'deploy:publishing', 'deploy:restart'
  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      within release_path do
        execute :rake, 'tmp:clear'
      end
    end
  end

  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 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

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

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart

end

This is my site-available/app.com

upstream myapp {
    server unix:///home/me/app/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
    listen       80;
    server_name  app.com;
    root /home/ma/app/current/public;

    try_files $uri/index.html $uri @puma;
    location @puma {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://myapp;
    }

    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /home/me/app/current/public;
    }
}

I've already read these:
https://www.digitalocean.com/community/questions/unable-to-connect-on-deploying-a-rails-app-with-nginx-capistrano-and-puma-on-ubuntu-16-04

https://www.digitalocean.com/community/questions/puma-sock-failed-111-connection-refused-while-connecting-to-upstream-mina-puma-nginx-rails

3 Answers

Hi @contactiocose

Don't know anything about Puma/Rails, but are both Nginx and Puma/Rails running as the same user - otherwise it simply might be a permission problem.

And in Nginx server unix:// should just be server unix: so:

server unix:/home/me/app/shared/tmp/sockets/puma.sock fail_timeout=0;

puma.sock is a unix socket file that puma is supposed to listen on—puma should create it once it's started. It sounds like puma is not running. Try running cap production puma:start and cap production puma:status (to check if the start command was successful).

Yes, Puma was not running, the problem was in my deploy file. since capistrano-3, the restart is automatic, this part:

  after  :finishing,    :restart

Needed to be removed, otherwise i have a warning AND puma was not started

Have another answer? Share your knowledge.