Lighting fast, zero-downtime deployments with git, capistrano, nginx and Unicorn

Posted • ~2400 words • ~11 min • last updated
Tagged capistrano rails git unicorn nginx zero-downtime deployment

Everyone who has ever developed a web app has had to deploy it. Back in the day you simply uploaded your files with FTP and everything would be good. Today we have to clone git repositories, restart servers, set permissions, create symlinks to our configuration files, clean out caches and what not.

Doctor, what’s wrong?

In my opinion there are two critical problems with deployments today:

  • They are slow
  • They cause downtime

Both topics have been discussed by the likes of Twitter and Github. They have optimized their deployment process to allow for fast and continuous deployments. But, you are probably stuck with a default Capistrano install. As it turns out, with a little work, it’s quite easy to setup the same deployment process as Github and Twitter use.

For, I’ve managed to get zero-downtime deployments that run in under 10 seconds. Yes, you read that right. ~

Let’s go!

This guide will help you setup your server and Rails 3.1 project for fast, zero-downtime deployments. I’ll be using Nginx+Unicorn to serve the application, git+capistrano for fast deployments. ~

The shopping list

Here’s a list of ingredients you’ll need:

  • A recent Ubuntu server (I used 11.04 Netty)
  • Your Rails 3.1 app
  • A remote git repository that contains your app


I’m making some assumptions about your app:

  • Ruby 1.9.2
  • Rails 3.1 app using Postgres named my_site
  • You want to use RVM and Bundler

Setting up your server

There are a few things you need to setup before diving in. The first bit is run under the root user.

Here’s the full apt-get command list I used.

apt-get update
apt-get upgrade -y
apt-get install build-essential ruby-full libmagickcore-dev imagemagick libxml2-dev \
  libxslt1-dev git-core postgresql postgresql-client postgresql-server-dev-8.4 nginx curl
apt-get build-dep ruby1.9.1

You’ll also need a separate user account to run your app. Believe me, you don’t want to run your app as root. I call my user deployer:

useradd -m -g staff -s /bin/bash deployer
passwd deployer

To allow deployer to execute commands with super-user privileges, add the following to /etc/sudoers. This required deployer to enter his password before allowing super-user access.

# /etc/sudoers
%staff ALL=(ALL) ALL

Ruby and RVM

With that done, you’re ready to install rvm, I performed a system-wide install, so make sure you run this as root.

bash < <(curl -s

Next up install the required ruby, in this case ruby-1.9.2-p290 and rubygems:

rvm install ruby-1.9.2-p290
tar zxvf rubygems-1.8.10.tgz
cd rubygems-1.8.10
ruby setup.rb

Create a ~/.gemrc file, this sets some sane defaults for your production server:

# ~/.gemrc
:verbose: true
:bulk_threshold: 1000
install: --no-ri --no-rdoc --env-shebang
:benchmark: false
:backtrace: false
update: --no-ri --no-rdoc --env-shebang
:update_sources: true

Also create this ~/.rvmrc file to auto-trust your .rvmrc project files:

# ~/.rvmrc

Note: do this for both root and the deployer user to avoid confusion later on.

Because you’ll be running your app in production-mode all the time, add the following line to /etc/environment so you don’t have to repeat it with every Rails related command you use:



I know not everybody uses Postgres, but I do. I love it and it beats the living crap out of MySQL. If you use MySQL, you’ll know what to do. Here are instructions for setting up Postgres. First create the database and login as the postgres user:

sudo -u postgres createdb my_site
sudo -u postgres psql

Then execute the following SQL:

CREATE USER my_site WITH PASSWORD 'password';


Nginx is a great piece of Russian engineering. You’ll need some configuration though:

# /etc/nginx/sites-available/default
upstream my_site {
  # 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_site.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

    root /home/deployer/apps/my_site/current/public;
    access_log /var/log/nginx/my_site_access.log;
    rewrite_log on;

    location / {
        #all requests are sent to the UNIX socket
        proxy_pass  http://my_site;
        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/deployer/apps/my_site/current/public;
      expires max;

All dandy! One more then:

# /etc/nginx/nginx.conf
user deployer staff;

# Change this depending on your hardware
worker_processes 4;
pid /var/run/;

events {
    worker_connections 1024;
    multi_accept on;

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay off;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect 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_vary on;
    gzip_proxied any;
    gzip_min_length 500;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    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/*;

Okay, that’s Nginx for you. You should start it now, although you’ll get a 500 or proxy error now:

/etc/init.d/nginx start


The next part involves setting up Capistrano and unicorn for your project. This is where the real magic will happen.

You’ll be doing cap deploy 99% of the time. This command needs to be fast. To accomplish this I want to utilize the power of git. Instead of having Capistrano juggle around a bunch of release directories, which is painfully slow, I want to use git to switch to the correct version of my app. This means I’ll have just one directory that is updated by git when it needs to be.

Let’s get started by adding some gems to your app. When done run bundle install.

    # Gemfile
    gem "unicorn"

    group :development do
      gem "capistrano"

The next step is adding a configuration file for Unicorn in config/unicorn.rb:

# config/unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "development"

# See 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_site.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/"

# Production specific settings
if env == "production"
    # Help ensure your application will always spawn in the symlinked
    # "current" directory that Capistrano sets up.
    working_directory "/home/deployer/apps/my_site/current"

    # feel free to point this anywhere accessible on the filesystem
    user 'deployer', 'staff'
    shared_path = "/home/deployer/apps/my_site/shared"

    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)

    # Before forking, kill the master process that belongs to the .oldbin PID.
    # This enables 0 downtime deploys.
    old_pid = "/tmp/"
    if File.exists?(old_pid) && != old_pid
    rescue Errno::ENOENT, Errno::ESRCH
        # someone else did our job for us

after_fork do |server, worker|
    # the following is *required* for Rails + "preload_app true",
    if defined?(ActiveRecord::Base)

    # 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)

Okay, as you can see there’s some nice stuff in there to accomplish zero-downtime restarts. Let me tell you a bit more about that.

Unicorn starts as a master process and then spawns several workers (we configured four). When you send Unicorn the ‘USR2’ signal it will rename itself to master (old) and create a new master process. The old master will keep running.

Now, when the new master starts and forks a worker it checks the PID files of the new and old Unicorn masters. If those are different, the new master was started correctly. We can now send the old master the QUIT signal, shutting it down gracefully (e.g. let it handle open requests, but not new ones).

All the while, you have restarted your app, without taking it down: zero downtime!


Now for Capistrano, add the following to your Gemfile.

# Gemfile
group :development do
  gem "capistrano"

And generate the necessary Capistrano files.

capify .

Open up config/deploy.rb and replace it with the following.

This deploy script does all the usual, but the special part is where you reset the release paths to the current path, making the whole release directory unnecessary.

Also not that the update_code is overwritten to do a simple git fetch and git reset - this is very fast indeed!

# config/deploy.rb
require "bundler/capistrano"

set :scm,             :git
set :repository,      ""
set :branch,          "origin/master"
set :migrate_target,  :current
set :ssh_options,     { :forward_agent => true }
set :rails_env,       "production"
set :deploy_to,       "/home/deployer/apps/my_site"
set :normalize_asset_timestamps, false

set :user,            "deployer"
set :group,           "staff"
set :use_sudo,        false

role :web,    "123.456.789.012"
role :app,    "123.456.789.012"
role :db,     "123.456.789.012", :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 }

default_environment["RAILS_ENV"] = 'production'

# Use our ruby-1.9.2-p290@my_site gemset
default_environment["PATH"]         = "--"
default_environment["GEM_HOME"]     = "--"
default_environment["GEM_PATH"]     = "--"
default_environment["RUBY_VERSION"] = "ruby-1.9.2-p290"

default_run_options[:shell] = 'bash'

namespace :deploy do
  desc "Deploy your application"
  task :default do

  desc "Setup your git-based deployment app"
  task :setup, :except => { :no_release => true } do
    dirs = [deploy_to, shared_path]
    dirs += { |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}"

  task :cold do

  task :update do
    transaction do

  desc "Update the deployed code."
  task :update_code, :except => { :no_release => true } do
    run "cd #{current_path}; git fetch origin; git reset --hard #{branch}"

  desc "Update the database (overwritten to avoid symlink)"
  task :migrations do
    transaction do

  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 &&
      ln -sf #{shared_path}/database.yml #{latest_release}/config/database.yml

    if fetch(:normalize_asset_timestamps, true)
      stamp ="%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" }

  desc "Zero-downtime restart of Unicorn"
  task :restart, :except => { :no_release => true } do
    run "kill -s USR2 `cat /tmp/`"

  desc "Start unicorn"
  task :start, :except => { :no_release => true } do
    run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D"

  desc "Stop unicorn"
  task :stop, :except => { :no_release => true } do
    run "kill -s QUIT `cat /tmp/`"

  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}"

    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}"

    desc "Rolls back to the previously deployed version."
    task :default do

def run_rake(cmd)
  run "cd #{current_path}; #{rake} #{cmd}"

Now there is one little thing you’ll need to do. I like to run my apps, even on the server, to use their own gemset. This keeps everything clean and isolated. Login to the deployer account and create your gemset. Next run rvm info and fill the PATH, GEM_HOME and GEM_PATH variables accordingly.

Don’t forget to install bundler in your new gemset

Database configuration

I always like to keep the database configuration out of git. I’ll place it in the shared directory.

# /home/deployer/apps/my_site/shared/database.yml
  adapter: postgresql
  encoding: unicode
  database: my_site_production
  pool: 5
  username: my_site
  password: password

First setup

Now setup your deployment like this:

cap deploy:setup

This will clone your repo and link your database.yml file. Optionally, you may want to run migrations or upload an SQL dump to get started quickly with your app.


Whenever you have a new feature developed in a feature branch, this is the process of deploying it:

  1. Merge feature_branch into master
  2. Run your tests to make sure everything is dandy.
  3. Push master
  4. Run cap deploy

For, step 4 takes less than 10 seconds. Unicorn is restarted with zero-downtime so users don’t even notice the site was updated.

What’s next?

You now have fast, zero-downtime deployments working for your app. There are still some things you should to (which I might cover in some later post):

  • Tweak the nginx and Unicorn settings (especially number of workers); Perform some tests and see what works best for your app/server combination.
  • Add caching to nginx (or add Varnish)
  • Enable some monitoring, maybe Monit
  • Crank up security a notch
  • Integrate other deployment tasks like whenever or prepare your assets