Skip to content

Instantly share code, notes, and snippets.

@westonplatter
Last active December 20, 2015 22:08
Show Gist options
  • Select an option

  • Save westonplatter/6202297 to your computer and use it in GitHub Desktop.

Select an option

Save westonplatter/6202297 to your computer and use it in GitHub Desktop.
load 'deploy/assets'
set :stages, %w(production staging)
set :default_stage, 'staging'
require 'capistrano/ext/multistage'
require 'capistrano-unicorn'
require 'bundler/capistrano'
require 'rvm/capistrano'
set(:unicorn_env) { rails_env }
set(:app_env) { rails_env }
role(:web) { domain }
role(:app) { domain }
role(:db, :primary => true) { domain }
set :user, 'deploy'
set :application, 'sam_skates'
set :repository, '[email protected]:westonplatter/sam_skates.git'
set :branch, 'wizard'
set :scm, :git
set :use_sudo, false
set :ssh_options, { forward_agent: true }
default_run_options[:pty] = true
require 'hipchat/capistrano'
set :hipchat_token, ''
set :hipchat_room_name, 'notifications'
set :hipchat_announce, true # notify users?
set :hipchat_env, rails_env
set(:deploy_to) { "/home/#{user}/#{application}/#{fetch :app_env}" }
set(:current_path) { File.join(deploy_to, current_dir) }
# clean up old releases on each deploy
after 'deploy:restart', 'deploy:cleanup'
task :symlink_database_yml do
run "ln -sfn #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -sfn #{shared_path}/config/application.yml #{release_path}/config/application.yml"
end
after 'bundle:install', 'symlink_database_yml'
#
# unicorn operations defined via 'capistrano-unicorn'
# https://rubygems.org/gems/capistrano-unicorn
#
after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded
after 'deploy:restart', 'unicorn:restart' # app preloaded
after 'deploy:update_code', 'deploy:migrate'
preload_app true
worker_processes 2
timeout 30
app_path = '/home/deploy/sam_skates/staging/current'
shared_path = '/home/deploy/sam_skates/staging/shared'
listen "#{app_path}/tmp/pids/unicorn.socket", backlog: 64
pid "#{app_path}/tmp/pids/unicorn.pid"
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory app_path
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
# Before forking, kill the master process that belongs to the .oldbin PID.
# This enables 0 downtime deploys.
old_pid = "#{app_path}/tmp/pids/unicorn.id.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill('QUIT', 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|
# the following is *required* for Rails + "preload_app true"
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
end
upstream samskates.think602.com {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/home/deploy/sam_skates/staging/shared/pids/unicorn.socket fail_timeout=0;
}
server {
listen 443 ssl;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
listen 80 default;
server_name samskates.think602.com;
root /home/deploy/sam_skates/staging/current/public;
access_log /var/log/nginx/staging_samskates_access.log;
rewrite_log on;
location / {
#all requests are sent to the UNIX socket
proxy_pass http://samskates.think602.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
# if the request is for a static resource, nginx should serve it directly
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
location ~ ^/(system|assets|spree)/ {
root /home/deploy/sam_skates/staging/current/public;
expires max;
break;
}
}
user deploy;
# change this depending on your hardware
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
types_hash_bucket_size 512;
types_hash_max_size 2048;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 500;
gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript;
## # Virtual Host Configs ##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment