Rails App Deployment with the stack: Ubuntu + rbenv + nginx + unicorn
. It's based on the article by Ariejan de Vroom blog and I've adapted it where needed.
##Assumptions
- App: my_app
- deploy_path: /home/minuscode/deploy
- deployment user: minuscode
##Screen + SSH
First thing I like to do is to open up a screen session in the remote machine. The main commands you'll be needing are
- Create a session:
$ screen some_session
- Create new session or force attach to existing session
$ screen -DRS some_session
(useful when the ssh session hangs up and you need to force reattach) - list sessions
$ screen -list
- Detach session
crtl + a + d
- reattach to last session
$ screen -r some_session
Usually, when I'm working over a ssh connection that keeps hanging up, I just this one liner
ssh -t remote_server 'screen -DRS deploy_screen'
##Dependencies This may vary between systems, but make sure you have
sudo apt-get install nodejs
(For Compiling assets)
##Rbenv and Ruby Rbenv it's an alternative to RVM and let's you manage ruby versions in you production environment. It does this by intercepting ruby commands with executables found in you PATH. It then selects the correct ruby version according to you rails application version.
First you'll have to install rbenv and the plugin ruby-build. You can install both following this article, or:
cd ~/
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
#get the shims in you PATH
echo 'export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"' >> ~/.bashrc
source ~/.bashrc
You're now ready to install a ruby version. You can get the list of ruby versions with:
rbenv install -l
We be using ruby 2.0.0 so we'll do
rbenv install 2.0.0-p451
rbenv local 2.0.0-p451
rbenv rehash
Now you'll need to install bundle with gem install bundle
.
##NGINX
This is the Nginx config file for /etc/nginx/sites-available/default
. Put it where it suits you.
upstream my_app {
# 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).
# for UNIX domain socket setups:
server unix:/tmp/my_app.socket fail_timeout=0;
}
server {
# if you're running multiple servers, instead of "default" you should
# put your main domain name here
listen 80 default;
# you could put a list of other domain names this application answers
server_name myapp.com;
root /home/minuscode/deploy/my_app/current/public;
access_log /var/log/nginx/my_app_access.log;
rewrite_log on;
location / {
#all requests are sent to the UNIX socket
proxy_pass http://my_app;
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;
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
# this probably needs some work with Rails 3.1's asset pipe_line
location ~ ^/(images|javascripts|stylesheets|system)/ {
root /home/minuscode/deploy/my_app/current/public;
expires max;
break;
}
}
##Unicorn
Add to your Gemfile:
gem 'unicorn', :group => [:staging, :production]
Create the configuration file in config/unicorn.rb
# config/unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "development"
#Verbose startup
p Time.now.to_s + ' Starting Unicorn in ' + ENV["RAILS_ENV"].to_s + ' ... '
# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
worker_processes 4
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/my_app.socket", :backlog => 64
# Preload our app for more speed
preload_app true
# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
pid "/tmp/unicorn.my_app.pid"
# Production specific settings
if env == "production"
listen 3000, :tcp_nopush => true
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/home/minuscode/deploy/my_app/current"
# feel free to point this anywhere accessible on the filesystem
user 'minuscode'
shared_path = "/home/minuscode/deploy/my_app/shared"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"
end
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 = "/tmp/unicorn.my_app.pid.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
##Capistrano
We'll be using Capistrano 2x. Add to the Gemfile:
group :development, :test do
gem 'capistrano', '2.0.0'
end
After running the bundle command, run bundle exec capify
. In config/deploy.rb add
require "bundler/capistrano"
set :application, 'my_app'
set :scm, :git
set :repository, "git@bitbucket.org:baud/my_app.git"
set :branch, "origin/master"
set :migrate_target, :current
set :ssh_options, { :forward_agent => true }
set :rails_env, "production"
set :deploy_to, "/home/minuscode/deploy/my_app"
set :normalize_asset_timestamps, false
set :user, "minuscode"
set :group, "root"
set :use_sudo, false
role :web, "webhosting-my_app.com"
role :app, "webhosting-my_app.com"
role :db, "webhosting-my_app.com", :primary => true
set(:latest_release) { fetch(:current_path) }
set(:release_path) { fetch(:current_path) }
set(:current_release) { fetch(:current_path) }
set(:current_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:latest_revision) { capture("cd #{current_path}; git rev-parse --short HEAD").strip }
set(:previous_revision) { capture("cd #{current_path}; git rev-parse --short HEAD@{1}").strip }
set :default_environment, { 'PATH' => '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }
default_environment["RAILS_ENV"] = 'production'
default_environment["RUBY_VERSION"] = "2.0.0"
default_run_options[:shell] = 'bash'
namespace :deploy do
desc "Deploy your application"
task :default do
update
restart
end
desc "Setup your git-based deployment app"
task :setup, :except => { :no_release => true } do
dirs = [deploy_to, shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
run "git clone #{repository} #{current_path}"
end
task :cold do
update
migrate
end
task :update do
transaction do
update_code
end
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"
finalize_update
end
desc "Update the database (overwritten to avoid symlink)"
task :migrations do
transaction do
update_code
end
migrate
restart
end
task :finalize_update, :except => { :no_release => true } do
run "chmod -R g+w #{latest_release}" if fetch(:group_writable, true)
# mkdir -p is making sure that the directories are there for some SCM's that don't
# save empty folders
run <<-CMD
rm -rf #{latest_release}/log #{latest_release}/public/system #{latest_release}/tmp/pids &&
mkdir -p #{latest_release}/public &&
mkdir -p #{latest_release}/tmp &&
ln -s #{shared_path}/log #{latest_release}/log &&
ln -s #{shared_path}/system #{latest_release}/public/system &&
ln -s #{shared_path}/pids #{latest_release}/tmp/pids
CMD
if fetch(:normalize_asset_timestamps, true)
stamp = Time.now.utc.strftime("%Y%m%d%H%M.%S")
asset_paths = fetch(:public_children, %w(images stylesheets javascripts)).map { |p| "#{latest_release}/public/#{p}" }.join(" ")
run "find #{asset_paths} -exec touch -t #{stamp} {} ';'; true", :env => { "TZ" => "UTC" }
end
run "cd #{current_path}; bundle exec rake assets:precompile"
end
desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
run "kill -s USR2 `cat /tmp/unicorn.my_app.pid`"
end
desc "Start unicorn"
task :start, :except => { :no_release => true } do
run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"
end
desc "Stop unicorn"
task :stop, :except => { :no_release => true } do
run "kill -s QUIT `cat /tmp/unicorn.my_app.pid`"
end
namespace :rollback do
desc "Moves the repo back to the previous version of HEAD"
task :repo, :except => { :no_release => true } do
set :branch, "HEAD@{1}"
deploy.default
end
desc "Rewrite reflog so HEAD@{1} will continue to point to at the next previous release."
task :cleanup, :except => { :no_release => true } do
run "cd #{current_path}; git reflog delete --rewrite HEAD@{1}; git reflog delete --rewrite HEAD@{1}"
end
desc "Rolls back to the previously deployed version."
task :default do
rollback.repo
rollback.cleanup
end
end
end
def run_rake(cmd)
run "cd #{current_path}; #{rake} #{cmd}"
end
First thing is to run the setup task
bundle exec cap deploy:setup
Then, update the repository and start for the first time
bundle exec cap deploy:update
bundle exec cap deploy:start
If capistrano complains about a pids
folder, you'll have to create it manually at /home/user/path/to_app/shared/pids
Finaly, Capistrano works by committing changes to a repository and then running
bundle exec cap deploy
other important commands we have just defined are:
- Restart:
bundle exec cap deploy:restart
- Stop:
bundle exec cap deploy:stop
- Rollback :
bundle exec cap deploy:rollback
(Rollback to the last but one commit)
###Env Variables
You'll need to add this to your ~/.bashrc file so you'll have all the environment variables you need
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
export RAILS_ENV=production
##Finalizing
Test nginx config file with
sudo nginx -t
start nginx and make sure everything is ok
sudo /etc/init.d/nginx start
Test you app locally (port is defined in unicorn config file)
curl localhost:3000
##After the deployment ###deployment workflow
- Edit your code
- commit to the master branch
- push to the remote repository.
- run
bundle exec cap deploy
. This deploys the code and restarts the server
###logs
- stderr log:
/home/minuscode/deploy/my_app/shared/log/unicorn.stderr.log
- stdout log:
/home/minuscode/deploy/my_app/shared/log/unicorn.stdout.log
- nginx log:
/var/log/nginx/my_app_access.log