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!
Related
I have a rails application that is not rendering assets. It was previously working fine. When I added linked files it not only uploaded the files I specified but also precompiled assets. Even after clobbering all assets and recompiling it fails to render them even though that exist on the box.
error
Caddyfile
mydomain.com {
gzip
log stdout
root /home/deploy/apps/rails-app/current/public
proxy / unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock {
except /assets # this is /public/assets directory
except /solr
transparent
websocket
policy round_robin
}
errors stdout
header / {
Strict-Transport-Security "max-age=31536000"
}
proxy /solr localhost:8983 {
transparent
}
}
deploy.rb
# frozen_string_literal: true
# config valid only for current version of Capistrano
# lock '3.13.0'
# Change these
server '...', port: 2221, roles: %i[web app db], primary: true
set :repo_url, '...'
set :application, '...'
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 :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], auth_methods: %w(publickey) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :bundle_flags, '--no-cache'
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.6.6'
set :rails_env, 'production'
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)
## Defaults:
set :branch, 'master'
set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# set :linked_dirs, %w{data_files}
set :linked_dirs, fetch(:linked_dirs, []).push('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
I was able to get this working but using the fileserver directive.
https://example.com {
encode zstd gzip
root * /home/deploy/apps/rails-app/current/public
file_server
tls user#email.com
#notStatic {
not file
}
reverse_proxy #notStatic unix//home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock
header / {
Strict-Transport-Security "max-age=31536000"
}
}
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.
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?
I try to deploy my rails application on local virtual machine which runs ubuntu 14 LTS. I use nginx and phusion passenger. Also, I use capistrano gem for deployment.
I added my local ssh key to virtual machine's authorized keys.
Also i have no problem connecting to my virtual machine via ssh like that:
ssh neil#192.168.0.8
However when I write:
bundle exec cap production deploy
I get the following error:
cap aborted!
Net::SSH::Disconnect: connection closed by remote host
EOFError: end of file reached
Tasks: TOP => rbenv:validate
My deploy.rb file:
server "192.168.0.8", port: 80, roles: %i(:web :app :db), primary: true
set :log_level, :debug
set :application, "mySimpleBlog"
set :repo_url, "git#github.com:NeilAlishev/mySimpleBlog.git"
set :user, "neil"
set :linked_files, fetch(:linked_files, []).push("config/database.yml", "config/secrets.yml")
set :linked_dirs, fetch(:linked_dirs, []).push("log", "tmp/pids", "tmp/cache", "tmp/sockets",
"vendor/bundle", "public/system", "public/uploads")
set :deploy_to, "/home/#{fetch(:user)}/src/#{fetch(:application)}"
set :rbenv_type, :user
set :rbenv_ruby, "2.2.4"
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} "\
"RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w(rake gem bundle ruby rails)
set :rbenv_roles, :all
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
My deploy/production.rb file:
set :stage, :production
server "192.168.0.8", user: "neil", roles: %w(web app db)
role :app, %w(neil#192.168.0.8)
role :web, %w(neil#192.168.0.8)
role :db, %w(neil#192.168.0.8)
My nginx.conf file:
server {
listen 80;
server_name 192.168.0.8;
charset utf-8;
rails_env production;
passenger_enabled on;
root /home/neil/mySimpleBlog/current/public;
}
Change production.rb like this,
role :app, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}
role :web, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}
role :db, 'address_of_the_server', {
:ssh_options => {
:keys => 'key_path(like ~/.ssh/key_name.pem)'
}
}
i am deploying with capistrano and I am strugg;ling to find out why it will not run migrations when I try to deploy the site.
here is the whole error:
WARN [SKIPPING] No Matching Host for /usr/bin/env if test ! -d /home/deploy/forge_staging/releases/20140319132005; then echo "Directory does not exist '/home/deploy/forge_staging/releases/20140319132005'" 1>&2; false; fi
WARN [SKIPPING] No Matching Host for bundle exec rake db:migrate
heres my setup:
deploy.rb
lock '3.1.0'
server "xxx.xxx.xxx.xxx"
set :application, "ForgeAndCo"
set :scm, "git"
set :repo_url, "my-repo"
# set :scm_passphrase, ""
set :user, "deploy"
set :use_sudo, false
set :ssh_options, {
forward_agent: true,
port: 14439
}
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
SSHKit.config.command_map[:rake] = "bundle exec rake" #8
SSHKit.config.command_map[:rails] = "bundle exec rails"
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
set :keep_releases, 20
namespace :deploy do
desc 'Restart passenger without service interruption (keep requests in a queue while restarting)'
task :restart do
on roles(:app) do
execute :touch, release_path.join('tmp/restart.txt')
unless execute :curl, '-s -k --location localhost | grep "Forge" > /dev/null'
exit 1
end
end
end
after :finishing, "deploy:cleanup"
end
# start new deploy.rb stuff for the beanstalk repo
staging.rb
role :app, %w{deploy#xxx.xxx.xxx.xxx}
role :web, %w{deploy#xxx.xxx.xxx.xxx}
role :db, %w{deploy#xxx.xxx.xxx.xxx}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
# server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value
set :stage, :staging
server "xxx.xxx.xxx.xxx", user: "deploy", roles: %w{web app db}
set :deploy_to, "/home/deploy/forge_staging"
set :rails_env, 'staging' # If the environment differs from the stage name
set :migration_role, 'migrator' # Defaults to 'db'
set :branch, ENV["REVISION"] || ENV["BRANCH_NAME"] || "master"
Is this an issue with roles?
remove from deploy.rb: (add capistrano-bundler and require it in the Capfile)
server "xxx.xxx.xxx.xxx"
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"
set :user, "deploy" # you have it in staging.rb
set :use_sudo, false # not used in cap3
remove from staging.rb:
role :app, %w{deploy#xxx.xxx.xxx.xxx}
role :web, %w{deploy#xxx.xxx.xxx.xxx}
role :db, %w{deploy#xxx.xxx.xxx.xxx}
set :migration_role, 'migrator' # <= this is why you got the error
PS: edit your question and replace the ip with xxx.