# 14 Mar 2011
Capistrano makes deploying Ruby on Rails applications easy. Having experienced how easy, I was eager to use Capistrano to make deploying non-Rails applications just as easy. I wasn’t able to find comprehensive info on actually making this work from start to finish. Here’s how to do it.
Your version control system of choice is Mercurial, stored at Bitbucket. You want to deploy to a server running Apache. Your development machine is running Mac OS X and you have Capistrano installed.
Capifying Your Project
Capistrano has fairly good documentation for this step, and I don’t see this changing much in the future. Open Terminal.app and run:
Capistrano will create a couple of files for you. The important one is config/deploy.rb
. Open it. Then replace its contents with the following:
Now, update deploy.rb
with your specific information. You have been Capify’d!
You will notice that in deploy.rb
, we have only told Capistrano about Bitbucket via the repository parameter, and that we are using a URL that begins with SSH. This means that authentication with the Bitbucket server will be via SSH, which will require that we have a public-private key pair. We will set that up now.
Bitbucket has a helpful page which covers how to access your Bitbucket repository via SSH. The important part of it for this discussion is to set up your public key with Bitbucket.
If you don’t have a public-private key pair, follow that guide to set one up. In Terminal.app, run:
ssh-keygen
And follow the prompts. Make sure to note the location of your public key so that you can get to it later. It is probably at ~/.ssh/id_rsa.pub
or ~/.ssh/id_dsa.pub
.
Now, you’ll need to give Bitbucket your public key (ends in .pub). Look in your Bitbucket Accounts page to find where to do that.
Also, update your local repository’s parent by updating .hg/hgrc
in your repository’s root with:
[paths]
default = ssh://hg@bitbucket.org/bitbucket_username/project
You should be able to connect to Bitbucket without typing a password.
Just copy your public key into ~/.ssh/authorized_keys
on your deployment server. Assuming that you don’t already have any public keys set up already then run:
You should now be able to authenticate with your server without typing a password.
You’ll also want to change your Apache HTTPD document root to point to the current alias that Capistrano will create in your server’s www root. Different web hosts have different ways of allowing you to do this. Mine provides a web interface that can adjust this setting. Check yours.
This was, for me, the trickiest part. What Capistrano is going to do when you deploy your application is connect to your deployment server (authenticating using your private key on your development machine and your public key in ~/.ssh/authorized_keys
on your deployment server), and then, from your deployment server, connect to your Bitbucket server to access your repository.
In order for this to work, SSH Agent Forwarding must be enabled on your development machine. Add this to ~/.ssh/config
, creating the file if it doesn’t already exist:
Host *
ForwardAgent yes
Open up Terminal.app, navigate to your project’s root directory, and run:
cap deploy
The most recent version of your application in Bitbucket should now be deployed to your server.