Puma not creating socket file while deployed with Capistrano - ruby-on-rails

I have followed the tutorial here on ow to deploy a Rails App using Capistrano and Puma. I had initially used with a Rails 4 and all worked fine, however I'm now trying with Rails 5 App and is not working. I've made some changes due to changes in Capistrano (I've also upgraded Capistrano) and now I'm stuck as the deployment does not seem to create the sock file while deploying and I get the following error:
*412 connect() to unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock failed (2: No such file or directory) while connecting to upstream, client: 172.245.92.157, server: api.myapp.com, request: "GET / HTTP/1.0", upstream: "http://unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock:/", host: "roboarticle.com"
Here's my Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
# Include default deployment tasks
# require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma
require 'capistrano/secrets_yml'
require 'capistrano/sidekiq'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Here's my deploy.rb:
# config valid only for current version of Capistrano
lock '3.8.0'
set :application, 'myapp'
# set :repo_url, 'git#example.com:me/my_repo.git'
# Change these
# server '178.62.117.38', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#gitlab.com:WagnerMatos/myapp.git'
set :application, 'myapp'
set :user, 'deployer'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, false
set :use_sudo, false
set :deploy_to, "/var/www/apps/#{fetch(:application)}"
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, false # Change to false when not using ActiveRecord
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
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
unless `git rev-parse HEAD` == `git rev-parse origin/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
end
end
desc "reload the database with seed data"
task :seed do
puts "\n=== Seeding Database ===\n"
on primary :db do
within current_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:seed'
end
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'
# execute :bundle, 'exec cap production setup'
end
end
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
# before :starting, :check_revision
# after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
# after("deploy:compile_assets", "deploy:build_missing_paperclip_styles")
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
And here's my nguni config file:
upstream puma {
server unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 default_server deferred;
server_name api.myapp.io;
ssl on;
ssl_certificate /etc/nginx/ssl/api.myapp.io/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/api.myapp.io/api.myapp.io.key;
ssl_prefer_server_ciphers on;
root /var/www/apps/myapp/current/public;
access_log /var/www/apps/myapp/current/log/nginx.access.log;
error_log /var/www/apps/myapp/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control 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://puma;
proxy_read_timeout 300;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4000M;
keepalive_timeout 10;
}
Any ideas of what I'm missing here?

Related

Capistrano + puma + nginx: Capistrano puma is not creating puma-sockets and puma ids

Puma sockets and pids are not created after deploying with Capistrano. I have rails 5.1.2 application with ruby 2.4.0. Getting following error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
cap staging deploy:initial successfully completes. Have googled it and followed many links and answers but nothing is working. The deploy.rb and other files are following.
config/deploy.rb
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"
# Change these
server 'my_iP-address', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :deploy_via, :remote_cache
set :deploy_to, "/home/deploy/#{fetch(:application)}"
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/#{fetch(:application)}-puma.sock"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :bundle_binstubs, nil
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'
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
if fetch(:stage) == (:staging || 'staging')
if `git rev-parse HEAD` != `git rev-parse origin/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
elsif `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
Setting up branch, application, and stage in config/deploy/staging.rb
set :application, 'affiliate-staging'
set :deploy_to, "/home/deploy/#{fetch(:application)}"
set :stage, :staging
set :rails_env, :staging
set :branch, :staging
config/nginx.conf
upstream puma {
#using same puma socket name which I am sertting up in staging.rb
server unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/affiliate-staging/current/public;
access_log /home/deploy/affiliate-staging/current/log/nginx.access.log;
error_log /home/deploy/affiliate-staging/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control 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://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 100M;
keepalive_timeout 10;
}
As you can see there should be pids and affiliate-saintlbeau-puma.sock but its not created. I am getting this error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
shared/puma.rb
#!/usr/bin/env puma
directory '/home/deploy/affiliate-staging/current'
rackup "/home/deploy/affiliate-staging/current/config.ru"
environment 'staging'
tag ''
pidfile "/home/deploy/shared/tmp/pids/puma.pid"
state_path "/home/deploy/shared/tmp/pids/puma.state"
stdout_redirect '/home/deploy/current/log/puma.error.log', '/home/deploy/current/log/puma.access.log', true
threads 4,16
bind 'unix:///home/deploy/shared/tmp/sockets/puma.sock'
workers 0
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = ""
end
before_fork do
ActiveRecord::Base.connection_pool.disconnect!
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
I am unable to understand what I am missing here. I hope I haven't messed up the question.

Capistrano: Don't know how to build task 'deploy:new_release_path'

I am deploying for the first time to a Digital Ocean Ubuntu droplet. I have configured everything and followed all the steps and am now on the step where I issue the command: cap production deploy:initial. For this command I am getting back this error message:
cap aborted!
Don't know how to build task 'deploy:new_release_path' (see --tasks)
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task_manager.rb:58:in `[]'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:361:in `[]'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/dsl/task_enhancements.rb:12:in `after'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/scm/git.rb:21:in `register_hooks'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/configuration/plugin_installer.rb:28:in `install'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/configuration.rb:155:in `install_plugin'
/Users/Christopher/ClientProjects/PawBookings/Capfile:3:in `<top (required)>'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:687:in `raw_load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:96:in `block in load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:95:in `load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:79:in `block in run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/application.rb:14:in `run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/bin/cap:3:in `<top (required)>'
/Users/Christopher/.rbenv/versions/2.3.1/bin/cap:22:in `load'
/Users/Christopher/.rbenv/versions/2.3.1/bin/cap:22:in `<main>'
Capfile
# Load DSL and Setup Up Stages
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
config/deploy.rb
# Change these
server '12.34.56.789', port: 80, roles: [:web, :app, :db], primary: true
set :repo_url, 'my_ssh_github_url'
set :application, 'app_name'
set :user, 'deploy_username'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
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 :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
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
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
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
config/nginx.conf
upstream puma {
server unix:///home/deploy_username/apps/app_name/shared/tmp/sockets/app_name-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy_username/apps/app_name/current/public;
access_log /home/deploy_username/apps/app_name/current/log/nginx.access.log;
error_log /home/deploy_username/apps/app_name/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control 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://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
Update:
I moved a couple lines of code in the Capfile. The process seems to have started but now gives the following error message:
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_linked_dirs (first_time)
** Execute deploy:set_linked_dirs
** Invoke deploy:set_rails_env
** Invoke rvm:hook (first_time)
** Execute rvm:hook
cap aborted!
Net::SSH::Disconnect: connection closed by remote host
New Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
The Git plugin relies on the deploy tasks being present. In your Capfile, you must install the Git plugin after requiring capistrano/deploy, like this:
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
its mainly due to the ruby,rails and ubuntu updated version issues. I got the same problem.
Added in Capfile the below line before the last line and that solved my issue.
install_plugin Capistrano::Puma::Daemon
For reference follow the below tutorial. Would be helpful for someone. Because I spent lot of time to find a solution for this. https://matthewhoelter.com/2020/11/10/deploying-ruby-on-rails-for-ubuntu-2004.html

Nginx rendering 502 error page

I'm trying to configure nginx with puma to server my Ruby on Rails application. Whenever I visit my page I get a 502 bad gateway error.
/etc/nginx/sites-enabled/puma-api
upstream puma_puma-api_production {
server unix:/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock fail_timeout=0;
}
server {
listen 80 default;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
root /home/deploy/apps/puma-api/current/public;
try_files $uri/index.html $uri #puma_puma-api_production;
location #puma_puma-api_production {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma_puma-api_production;
# limit_req zone=one;
access_log /home/deploy/apps/puma-api/shared/log/nginx.access.log;
error_log /home/deploy/apps/puma-api/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location ~ \.(php|html)$ {
return 405;
}
}
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_requests 100;
keepalive_timeout 65;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_vary off;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\.";
variables_hash_max_size 1024;
variables_hash_bucket_size 64;
server_names_hash_bucket_size 64;
types_hash_max_size 2048;
types_hash_bucket_size 64;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
app/config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 5000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'puma-api'
set :repo_url, 'git#github.com:chaione/puma-api.git'
set :user, 'deploy'
# set :nginx_domains, 'localhost'
# set :app_server_socket, "#{shared_path}/sockets/puma-#{fetch :application}.sock"
# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
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 :ssh_options, forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub)
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
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
# # Default value for :linked_files is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/images')
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
namespace :deploy do
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, 'cache:clear'
# end
end
end
end
app recipe
# create www directory
directory '/var/www' do
user node['user']['name']
group node['group']
mode 0755
end
# create shared directory structure for app
path = "/var/www/#{node['app']}/shared/config"
execute "mkdir -p #{path}" do
user node['user']['name']
group node['group']
creates path
end
# create database.yml file
template "#{path}/database.yml" do
source 'database.yml.erb'
mode 0640
owner node['user']['name']
group node['group']
end
You have nginx communicating with your puma server via socket
/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock
You have puma creating this socket at
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
I can't see shared_path defined anywhere in your code though. Make sure that these two match (or just hardcode the path to eliminate variables for now).
I would also first check if /home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock even exists.

Why is Capistrano deploying my project in "development" environment?

I have the following files set up for my capistrano configuration. When I deploy my project with the following command it deploys to the server, but the Rails.env is set to 'deployment'. How can I have it properly set to 'production'?
deploy command
cap production deploy
deploy.rb
set :application, 'project'
set :repo_url, 'git#github.com:AccountName/project.git'
set :deploy_to, '/home/deploy/project'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
config/deploy/production.rb
set :stage, :production
set :branch, 'master'
set :rails_env, 'production'
server '127.0.0.1:4400', user: 'deploy', roles: %w{web app}
nginx.conf
server {
listen 80;
server_name ip_address;
rails_env production;
root /path/to/app/public;
passenger_enabled on;
}
sites-available/project
server {
listen 80 default_server;
server_name ip_address;
passenger_enabled on;
root /path/to/app/public;
}
The problem was that the passenger_app_env was at development. I couldn't figure out which services weren't restarted so I did a reboot of the server. Which presented some other issues, but I'm fairly certain the problem was resolved by changing that variable to passenger_app_env production. Thanks for the help!

Error while connecting to upstream, Nginx and Unicorn, Mina , DigialOcean

I'm following this tutorial http://blog.mccartie.com/2014/08/28/digital-ocean.html to deploy my existing rails app to digital ocean with Mina, but my deploy fails, and looking into the errors logs I've found this error in the var/log/nginx/app_name.log:
connect() to unix:/home/deployer/rahmchicago/shared/sockets/unicorn.sock failed (2: No such file or directory) while connecting to upstream
These are my config files:
/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
etc/nginx/sites-enabled/default
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/home/deployer/YOUR_APP_NAME/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
# Application root, as defined previously
root /home/deployer/YOUR_APP_NAME/current/public;
server_name www.YOUR_APP_NAME.com YOUR_APP_NAME.com;
try_files $uri/index.html $uri #app;
access_log /var/log/nginx/YOUR_APP_NAME_access.log combined;
error_log /var/log/nginx/YOUR_APP_NAME_error.log;
location #app {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
config/unicorn.rb in the app
# Set your full path to application.
app_dir = File.expand_path('../../', __FILE__)
shared_dir = File.expand_path('../../../shared/', __FILE__)
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Fill path to your app
working_directory app_dir
# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
# Loging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{app_dir}/Gemfile"
end
config/deploy.rb
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
require 'mina_sidekiq/tasks'
require 'mina/unicorn'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
set :domain, 'YOUR DROPLETS IP'
set :deploy_to, '/home/deployer/rahmchicago'
set :repository, 'YOUR GIT REPO URL'
set :branch, 'develop'
set :user, 'deployer'
set :forward_agent, true
set :port, '22'
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'log', 'config/secrets.yml']
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
queue %{echo "-----> Loading environment" #{echo_cmd %[source ~/.bashrc]} }
invoke :'rvm:use[ruby-2.0.0-p353#default]'
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .rbenv-version to your repository.
end
# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
queue! %[mkdir -p "#{deploy_to}/shared/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
queue! %[touch "#{deploy_to}/shared/config/database.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
queue! %[touch "#{deploy_to}/shared/config/secrets.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]
# sidekiq needs a place to store its pid file and log file
queue! %[mkdir -p "#{deploy_to}/shared/pids/"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/pids"]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
# stop accepting new workers
invoke :'sidekiq:quiet'
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
to :launch do
invoke :'unicorn:restart'
invoke :'sidekiq:restart'
end
end
end
I found that the deploy fails when mina execute this line invoke :'unicorn:restart' (in the deploy.rb file), all the other commands execute with success. Thanks for the help!

Resources