Deploying Magento with Capistrano

I recently found this little gem called capistrano-ash which includes a recipe for deploying Magento to both staging and production servers. 

This walkthrough will show you how to deploy Magento with Git (and Gitosis) to both staging and production servers. Be sure to check out the full documentation on capistrano-ash.

As you’re following this, if you’re bonking out on something, check the permissions for the deploy user. It may not have sudo or write permissions.

Why do I want to do this?

Capistrano is a great solution for deploying code. In fact, you don’t even have to use it to deploy code; it doesn’t care what you are moving. Capistrano works in conjunction with your version control system to make deploying and updating your production application much easier and reliable. Rollbacks are super easy in case something goes wrong, and you can deploy to multiple environments or clusters in a snap. Less stress == happier people.

Pre-setup

You’ll need to install a few gems before we get started.

  gem install capistrano
  gem install capistrano-ext
  gem install capistrano-ash

Setup your project

Run the following commands to setup your local Magento project

  capify .
  rm -Rf config/deploy.rb; mkdir -p config/deploy
  touch config/deploy/staging.rb config/deploy/production.rb

By default this creates both staging and production deploy scripts.

Edit your deploy environment files

Your environment file should looks something like the following. Edit as needed. If you’re only deploying to a single server (versus a cluster), you can remove the role :db bit and just keep the :web bit.

  # servers
  role :web, "aaiserver.net" 
  role :db,  "aaiserver.net" 

  # file location
  set :deploy_to, "/var/www/#{application}/staging/" 

  # SSH user
  set :user, "augash" 

Edit your Capfile

This may not be necessary for you, depending on your setup. Capistrano Ash defaults to using SVN, but we use git. Simply add a line to your Capfile

  set :scm, :git

Prep your environment

This will create the necessary structure as defined in your environment deploy files. Obviously substitute <environment> for either ‘staging’ or ‘production’.

  $ cap <environment> deploy:setup

Don’t forget to commit any specifics in the following files that were created.

  htaccess.dist
  app/etc/local.xml.staging
  app/etc/local.xml.production

Setup your SSH user

I’m assuming Gitosis here, but all you’re really doing is copying the public key from your deploy environment to your repository environment (passwordless SSH login). Your needs may be slightly different.

Generate your key (if you haven’t already)
  $ ssh-keygen
Copy your public key to the repo server
  $ cat ~/.ssh/id_rsa.pub | ssh user@remote-server 'sh -c "cat - >>~/.ssh/authorized_keys"'

Setup your server

Capistrano will copy files into a timestamped folder and symlink that to the current folder. You just need to point your Document Root at current.

  <VirtualHost *:80>
    DocumentRoot /srv/www/example1.com/staging/current
    ServerName www.example1.com

    # Other directives here
  </VirtualHost>

Browse to your new Document Root and create the following folders. On deploy, these folders will be symlinked to your current release.

  includes/
  media/
  sitemap/
  var/

Let ‘er rip

At this point (assuming you’ve worked out any specific environment issues) you should be able to deploy tout de suite.

On deploy the cache is cleared, shared folders are symlinked, and all is right with the world.