Capistrano: Deploying multiple sites to the same server

I work for a digital agency that tends to deal with multi-market sites a lot. Most of the sites aren’t particularly high traffic and sit quite happily together on a large EC2 instance with nginx. However, a persistent problem for us was deployment. In our day to day work we generally use Capistrano to deploy sites to both staging and production (very few of our projects warrant build servers) and for the most part it works quite happily, except for when we need to deploy the same repository multiple times to a single server.

For example, one of the sites we manage currently exists in 10 markets and is planning on adding more. They are all served from different domains using separate databases but use the same repository and are hosted on the same server, (config files differ of course). Unfortunately, and after a lot of research, we discovered there is no easy way to do this in Capistrano, indeed it’s talked about quite a lot but there doesn’t seem to be an accepted solution,  so we put our thinking caps on and came up with one.

The goal

We decided that in an ideal world we’d like to be able to achieve the following:

  • Deploy all sites in an environment at the same time, (the most important).
  • Deploy a single site in an environment, (we may want to update just one site, no point in re-deploying all of them).

So not only can we deploy all of them but we still retain the ability to re-deploy an individual one should the need arise. Simple!!

The Solution

Firstly create your Capistrano setup as normal. I don’t really want to get into how to do that here but there are loads of guides available on google, this one for example.

Once Capistrano is installed, setup and configured ensure that the config/staging.rb and config/production.rb (or whatever environments you use) files exist. The content isn’t that important (note: it will be ignored) but they do have to be there. Now for each market/site/locale you wish to deploy for create new environment files for with the naming convention “{environment},{identifier}.rb”. So for example we have:

  • staging.rb
  • production.rb

In each file you set the stage, branch, deploy_to, role, server and anything else specific to that , anything you wish to be globally set can be put in your config/deploy.rb file.

set :stage, :staging

role :web, %w{}

ask :branch, 'develop'

set :deploy_to, "/var/www/"

server '', user: 'deploy', roles: %w{web}

Now at the bottom of your config/deploy.rb file add the following task:

namespace :deploy_all do
  task :deploy do
    on roles(:all) do

      files = Dir.glob("config/deploy/#{fetch(:stage)}.*.rb")
      files.each do | file |
        file = file.sub('config/deploy/', '').sub('.rb', '')
        info "Deploying #{file} to #{:stage}"
        system("cap #{file} deploy")


task :deploy_all => 'deploy_all:deploy'

This globs the deploy directory looking for config files depending on the environment variable passed, it then executes Capistrano for each file found. You can now deploy every site with a single command:

cap staging deploy_all

Or to deploy an individual site do:

cap deploy

Things to improve

This is were we’ve got to so far though we admit it isn’t perfect. Given time we’d quite like to add the following improvements:

  1. A slightly better way of managing the Capistrano settings so they’re a bit DRY-er not repeated in each config file, but I haven’t dug into it any further.
  2. This method is also annoyingly synchronous. I’m fairly sure that with a bit of time and Ruby knowledge the each loop could be re-written to make asynchronous calls, though this may convolute the terminal output somewhat.