Deploying with Capistrano VPS
It has taken me a while to move from the custom checkout scripts that I use for rails deployment to move to the capistrano way of doing things. I am so excited about it that I have decided to write this blog post.
This is not a definitive guide to rails deployment with capistrano but is the way I do things when deploying to slicehost for an application which we will be releasing in the not too distant future. This should work out for other vps servers and can be modified for shared hosting where you dont have as many permissions such as sudo access. Also I am basically a capistrano beginner so take what I say with a pinch of salt.
Firstly, if you are not familiar with capistrano I recommend purchasing the peepcode episode titled Capistrano Concepts
Your using source control right?
The first step is to make sure that you are using source control. The source control system of choice in the rails world is subversion. Capistrano works with quite a few other scm applications but subversion is by far the most common.
So you need to have your application in a repository somewhere which your deployment box can access. You should also setup the repository to work with the rails app (ignore log files, ignore database.yml, ignore *.pid files etc.) There is a great rake task for setting up subversion on rails
You have installed and setup your vps?
Lets say you have setup your vps and plan to deploy with an nginx / mongrel_cluster rails stack. There is a good guide at usefuljaja for doing this.
You have also setup ssh key authentication to avoid typing in passwords all the time. You are also just deploying to a single server and only have one mongrel process to start with.
For installing and setting up nginx for your deployment its best just to install nginx from source and use the nginx config generator to configure nginx for your deployment. I am also loving the no_www option which redirects requests to www.example.org→example.org. Is it just me or do other people hate www.*
Setting up a deploy user on the VPS is also recommended for cleanliness.
You have setup a database and there is a domain you can run this off?
You need to setup the database in your production environment. A url to deploy to would also be nice, maybe you are still in development and you can use a static dyndns.org free domain to get started.
Install capistrano and railsmachine
sudo gem install railsmachine —include-dependencies
This is a great little collection of capistrano tasks to organise the deployment by convention over configuration.
Setup your rails app and deploy
cd {RAILS_ROOT}
cap —apply-to .
This creates a deploy.rb in your /config directory where you can setup the deployment. My deployment script can be found over at pastie
The tasks defined in the two bottom tasks are defined in a mycaptasks.rb file which I can move around projects. You could also set it up as an svn:external in your project if you want to be more DRY. It is also up on pastie
Now run:
cap setup
This sets up the folder structure that capistrano uses.
Now:
cap cold_deploy
To check everything is working ok.
Now:
cap deploywithmigrations
This will build up your new database assuming you work withe database migrations.
From now on
cap deploy
To deploy your app.
Its in source control
Because the deploy scripts are contained within the source control it works great in a distributed work environment so other developers can work with your project and deploy in a common way. It brings deployment into a convention rather than custom built and often nasty deployment methods which exist out there.
Cheers for now
This has been a skim at deploying with capistrano and I urge you to check out the other and probably a lot better learning resources out there.