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 Ariejan.net, 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
## Assumptions
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.
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`:
``` shell
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.
``` shell
# /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.
Create a `~/.gemrc` file, this sets some sane defaults for your production server:
``` ruby
# ~/.gemrc
---
:verbose: true
:bulk_threshold: 1000
install: --no-ri --no-rdoc --env-shebang
:sources:
- http://gemcutter.org
- http://gems.rubyforge.org/
- http://gems.github.com
: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:
``` shell
# ~/.rvmrc
rvm_trust_rvmrcs_flag=1
```
_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:
``` shell
RAILS_ENV=production
```
## Postgres
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:
``` shell
sudo -u postgres createdb my_site
sudo -u postgres psql
```
Then execute the following SQL:
``` sql
CREATE USER my_site WITH PASSWORD 'password';
GRANT ALL PRIVILEGES ON DATABASE my_site TO my_site;
```
## Nginx
Nginx is a great piece of Russian engineering. You'll need some configuration though:
``` nginx
# /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
Okay, that's Nginx for you. You should start it now, although you'll get a 500 or proxy error now:
``` shell
/etc/init.d/nginx start
```
## Unicorn
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`.
``` ruby
# Gemfile
gem "unicorn"
group :development do
gem "capistrano"
end
```
The next step is adding a configuration file for Unicorn in `config/unicorn.rb`:
``` ruby
# config/unicorn.rb
# Set environment to development unless something else is specified
env = ENV["RAILS_ENV"] || "development"
# 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_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/unicorn.my_site.pid"
# Production specific settings
if env == "production"
# Help ensure your application will always spawn in the symlinked
# 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_site.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
```
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!
## Capistrano
Now for Capistrano, add the following to your `Gemfile`.
``` ruby
# Gemfile
group :development do
gem "capistrano"
end
```
And generate the necessary Capistrano files.
``` shell
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!
``` ruby
# config/deploy.rb
require "bundler/capistrano"
set :scm, :git
set :repository, "git@codeplane.com:you/my_site.git"
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
```
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.
``` yaml
# /home/deployer/apps/my_site/shared/database.yml
production:
adapter: postgresql
encoding: unicode
database: my_site_production
pool: 5
username: my_site
password: password
```
## First setup
Now setup your deployment like this:
``` shell
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.
## Deployments
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 Ariejan.net, 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