First, a disclaimer…
This is not an ideal setup. If I’m really going to be working this much with Ruby and Linux servers, I should probably do what everyone else is doing and start using a Mac at home. But that costs $$$, and until I’ve got enough income to really warrant that side expenditure, I’ll continue working in a Windows environment at home.
There are lots of How-To’s for setting up Capistrano. Here’s my take on the same. Here’s my system configuration…
- My home machine: Windows 7, hereby refered to as “client”.
- My server: A Rackspace Cloud Server running Ubuntu, hereby referred to as the “server”
First of all, get everything working on your home machine. If everything doesn’t work on your home machine, do not pass “Go” and do not collect $200. There are lots of guides on getting it all to work. As long as you’re not afraid of a little machine exploration and the command line, you’ll be fine.
OK, ready to go on?
Install Capistrano
For this example, my project was a simple Sinatra application. When you’re ready to start thinking about deployment, it’s time to install Capistrano. If you’re on Windows, you won’t need the sudo from the line below. You’ll also need to set up your project to use Capistrano. Change to your project root directory and type the next two lines to install the gem and setup your project to support Capistrano.
$ sudo gem install capistrano --no-ri --no-rdoc Successfully installed capistrano-2.5.19 1 gem installed $ capify .
If you’re on Windows, you’ll need to beef up your command line. If you’re on any other operating system, you can skip this part. Go install msysgit. Install it even if you have no desire to use Git for your source control. We’re going to use the built in bash. In my opinion, it’s a lot friendlier than trying to do all of this in Cygwin.
Get Your SSH Keys
If you already have an SSH key, then you can skip this step. Open up your Bash shell and run the following command. This will generate a new RSA SSH key and place it in your ~/.ssh directory. Replace the “you@gmail.com” with your real email address.
$ ssh-keygen -t rsa -C "you@gmail.com"
You’ll now find files called id_rsa and id_rsa.pub in your ~/.ssh directory.
When you capify‘ed your project, it should have added a config folder and a deploy.rb file in that folder. Replace the contents of your config/deploy.rb file with something similar to the following.
default_run_options[:pty] = true
# Give your application a name, and tell us where the source code is.
set :application, "www-myapplication-com"
set :scm, :git
set :repository, "git://my-git-repo.com:www-myapplication-com.git"
# These settings are for the server: application directory and install-as user.
set :deploy_to, "/var/www-data/#{application}"
set :user, "deploy"
# Where is your server?
role :app, "myserver.com"
role :web, "myserver.com"
Set up your deploy user
From your client, ssh to your server and set up the deploy user. Give your deploy user sudo privileges.
$ ssh myserver.com -l root $ adduser deploy $ visudo
At the end of the file add an entry for deploy ALL=(ALL) NOPASSWD: ALL. Type exit to leave ssh. You should now be back at your client.
I also need to create my /var/www-data directory and give my deploy user ownership of that directory. md and chown handle that nicely.
Add your client’s public key to your server’s deploy authorized_keys file
$ scp ~/.ssh/id_rsa.pub deploy@myserver.com: $ ssh deploy@myserver.com
Now you’re back on your server, this time logged on as your deploy user. We just copied our public key from our client to our deploy user’s home directory. We need to put that key in its correct place.
$ mkdir .ssh $ cat id_rsa.pub >> .ssh/authorized_keys $ rm id_rsa.pub $ chmod 600 .ssh/authorized_keys $ chmod 700 .ssh
Viget has an excellent post on setting up custom capistrano tasks. Let’s just say that with the above list, I was able to type cap deploy and everything worked as expected.