Skip to content

Instantly share code, notes, and snippets.

@bruno-campos
Last active December 21, 2015 13:39
Show Gist options
  • Save bruno-campos/6313750 to your computer and use it in GitHub Desktop.
Save bruno-campos/6313750 to your computer and use it in GitHub Desktop.
Files to configure puma + nginx for simple rails deployment. Put them under config folder.
require "bundler/capistrano"
require "puma/capistrano"
server "<server_ip>", :web, :app, :db, primary: true
set :application, "<app_name>"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, "git"
set :repository, "git@github.com:bruno-campos/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
#ssh_options[:auth_methods] = ["publickey"]
ssh_options[:forward_agent] = true
ssh_options[:keys] = ["~/.ssh/formfookey.pem"]
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/puma_init.sh /etc/init.d/puma_#{application}"
sudo "chmod +x /etc/init.d/puma_#{application}"
run "mkdir -p #{shared_path}/config"
run "mkdir -p #{shared_path}/sockets"
run "mkdir -p #{shared_path}/pids"
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/puma.rb #{release_path}/config/puma.rb"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}`
puts "WARNING: HEAD is not the same as origin/#{branch}"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
upstream puma {
server unix:/home/deployer/apps/<app_name>/shared/sockets/puma.sock;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/deployer/apps/<app_name>/current/public;
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 4G;
keepalive_timeout 10;
}
rails_env = Rails.env || 'production'
threads 1,16
bind "unix:///data/apps/<app_name>/shared/sockets/puma.sock"
pidfile "/data/apps/<app_name>/shared/pids/puma.pid"
state_path "/data/apps/<app_name>/shared/sockets/puma.state"
activate_control_app
set -e
# Feel free to change any of the following variables for your app:
APP_ROOT=/home/deployer/apps/<app_name>/
CMD="cd $APP_ROOT/current; bundle exec puma -C $APP_ROOT/shared/config/puma.rb -b unix://$APP_ROOT/shared/sockets/puma.sock -e production --control unix://$APP_ROOT/shared/sockets/pumactl.sock --state $APP_ROOT/shared/sockets/puma.state --pidfile $APP_ROOT/shared/pids/puma.pid 2>&1 >> $APP_ROOT/shared/log/puma.log &"
CTL="cd $APP_ROOT/current; bundle exec pumactl -S $APP_ROOT/shared/sockets/puma.state"
AS_USER=deployer
set -u
run () {
if [ "$(id -un)" = "$AS_USER" ]; then
eval $1
else
su -c "$1" - $AS_USER
fi
}
case "$1" in
start)
if [ ! -e "$APP_ROOT/shared/sockets/puma.sock" ]
then
run "$CMD"
exit 1
else
echo >&2 "Already running"
fi
;;
stop)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL stop" && exit 0
echo >&2 "Not running"
;;
force-stop)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL halt" && exit 0
echo >&2 "Not running"
;;
restart|reload)
[ -e "$APP_ROOT/shared/sockets/puma.sock" ] && run "$CTL restart" && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
*)
echo >&2 "Usage: $0 <start|stop|restart|force-stop>"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment