Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capistrano + Nginx + Unicorn + Sinatra on Ubuntu
require 'bundler/capistrano'
set :application, "net"
set :repository, "git@githost.com:net.git"
set :scm, :git
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}
set :use_sudo, false
default_run_options[:pty] = true
set :user, "deployer"
set :group, user
set :runner, user
set :host, "#{user}@host.com"
role :web, host
role :app, host
set :rack_env, :production
set :deploy_to, "/srv/#{application}"
set :unicorn_conf, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{shared_path}/pids/unicorn.pid"
set :public_children, ["css","img","js"]
namespace :deploy do
task :restart do
run "if [ -f #{unicorn_pid} ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{current_path} && bundle exec unicorn -c #{unicorn_conf} -E #{rack_env} -D; fi"
end
task :start do
run "cd #{current_path} && bundle exec unicorn -c #{unicorn_conf} -E #{rack_env} -D"
end
task :stop do
run "if [ -f #{unicorn_pid} ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
end
end
upstream net_unicorn {
server unix:/srv/net/shared/unicorn.sock fail_timeout=0;
}
server {
server_name example.net;
rewrite ^(.*) http://www.example.net$1 permanent;
}
server {
server_name www.example.net;
root /srv/net/current/public;
location / {
try_files $uri @net;
}
location @net {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://net_unicorn;
}
}
# define paths and filenames
deploy_to = "/srv/net"
rack_root = "#{deploy_to}/current"
pid_file = "#{deploy_to}/shared/pids/unicorn.pid"
socket_file= "#{deploy_to}/shared/unicorn.sock"
log_file = "#{rack_root}/log/unicorn.log"
err_log = "#{rack_root}/log/unicorn_error.log"
old_pid = pid_file + '.oldbin'
timeout 30
worker_processes 2
listen socket_file, :backlog => 1024
pid pid_file
stderr_path err_log
stdout_path log_file
before_fork do |server, worker|
# zero downtime deploy magic:
# if unicorn is already running, ask it to start a new process and quit.
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# already done
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment