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.
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.
_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:
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`.
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`.
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!
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.
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.