Deploying a Rails app to Amazon EC2 with Capistrano
Tuesday, April 27th, 2010@esilverberg originally posted this article that I've been referring to for the past year and now wanted to contribute some updates of my own.
Get the code
Arr! There are some problems with checking out code from github with SVN. I'll fix this soon but if your reading today modify deploy.rb to deploy from git.
We're going to deploy my stub site from github and we'll check it out with SubVersion
svn checkout http://svn.github.com/jspooner/authlogic_cucumber_rspec_example.git basic
cd basic
You probably want to get the project running locally. Hopefully you have the mysql driver installed or you can edit the database.yml file to use sqlite.
rake db:create rake db:migrate rake db:seed script/server
EC2 setup
- Read the getting started guide here at amazon.
- Install the developer tools. I have them located at
ec2-ami-tools-1.3-45758
- Make sure you have downloaded the cert- and pk- .pem files. I have them in ~/.ec2/
Setup environment variables
Probably want to put these in your .bashrc or .bash_profile with the 'export' command in front.
EC2_CERT=/home/USER/.ec2/cert-EZC7LAIYSKPC546UKT7E3PI.pem
EC2_HOME=/home/USER/ec2-api-tools-1.3-30349
EC2_PRIVATE_KEY=/home/USER/.ec2/pk-EZC7LAIYSKPC546UKT7EUEOOI.pem
PATH=$PATH:$EC2_HOME/bin
Verify dev tools
Run this to make sure it works:
ec2-describe-images ami-5394733a
Start up an instance
- Click "Launch Instance" https://console.aws.amazon.com/ec2/home
- Click "Community AMI's" and select one of the AMI's listed when you ran cap ec2onrails:ami_ids. I'm using ami-5394733a
- Select an instance size and continue.
- Advanced Instance Options. Click Continue.

- Create a new Key Pair named testkey and click download. Move this file to ~/.ssh/testkey.pem and chmod the key to 600.
mv testkey.pem ~/.ssh/testkey.pem cd ~/.ssh/ chmod 600 testkey.pem ls -al
You should see the file permissions set to -rw-------
This key is used to SSH into your instance and is used set on line 13 of deploy.rb.ssh_options[:keys] = ["#{ENV['HOME']}/.ssh/testkey.pem"] - Create a new Security Group and rules for HTTP HTTPS & SSH.
- Verify your settings and Launch the Instance

- After you instance has launched find the Public DNS that looks something like this ec2-xxx-xxx-xx-xxx.compute-1.amazonaws.com and add that to you deploy.rb file on lines 18-21
Update tools on the image
ssh into the machine and run the following
ssh -i ~/.ssh/testkey.pem root@ec2-xxx-xxx-xx-xxx.compute-1.amazonaws.com sudo apt-get update sudo apt-get -y install build-essential sudo apt-get -y install emacs22 sudo gem update --system
The original post had you installing imagemagick with apt-get but that wan't pulling the latest version; which the rmagick gem requires. So here we will pull down the latest version and install from source.
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz tar -zxvf ImageMagick.tar.gz cd ImageMagick-6.6.1-5/ ./configure make install gem install rmagick ldconfig /usr/local/lib
Set up capistrano and ec2onrails
Capistrano is a tool for automating tasks on one or more remote servers. We are going to use it to push our site up to EC2 with the help of the ec2onrails gem.
capify . gem install ec2onrails
Open Capfile and require ec2onrails.
require 'ec2onrails/recipes'
Open deploy.rb and replace with this.
# This is a sample Capistrano config file for EC2 on Rails. # It should be edited and customized. set :application, "basic" set :user, "root" set :use_sudo, true set :repository, "http://svn.github.com/jspooner/authlogic_cucumber_rspec_example.git " # NOTE: for some reason Capistrano requires you to have both the public and # the private key in the same folder, the public key should have the # extension ".pub". ssh_options[:keys] = ["#{ENV['HOME']}/.ssh/testkey.pem"] # Your EC2 instances. Use the ec2-xxx....amazonaws.com hostname, not # any other name (in case you have your own DNS alias) or it won't # be able to resolve to the internal IP address. role :web, "ec2-xx-xx-xx-xx.compute-1.amazonaws.com" role :app, "ec2-xx-xx-xx-xx.compute-1.amazonaws.com" role :db, "ec2-xx-xx-xx-xx.compute-1.amazonaws.com", :primary => true role :memcache, "ec2-xx-xx-xx-xx.compute-1.amazonaws.com" # Whatever you set here will be taken set as the default RAILS_ENV value # on the server. Your app and your hourly/daily/weekly/monthly scripts # will run with RAILS_ENV set to this value. set :rails_env, "production" # EC2 on Rails config. # NOTE: Some of these should be omitted if not needed. set :ec2onrails_config, { # S3 bucket and "subdir" used by the ec2onrails:db:restore task # :restore_from_bucket => "your-bucket", # :restore_from_bucket_subdir => "database", # S3 bucket and "subdir" used by the ec2onrails:db:archive task # This does not affect the automatic backup of your MySQL db to S3, it's # just for manually archiving a db snapshot to a different bucket if # desired. # :archive_to_bucket => "your-other-bucket", # :archive_to_bucket_subdir => "db-archive/#{Time.new.strftime('%Y-%m-%d--%H-%M-%S')}", # Set a root password for MySQL. Run "cap ec2onrails:db:set_root_password" # to enable this. This is optional, and after doing this the # ec2onrails:db:drop task won't work, but be aware that MySQL accepts # connections on the public network interface (you should block the MySQL # port with the firewall anyway). # If you don't care about setting the mysql root password then remove this. :mysql_root_password => "blamblam12343", # Any extra Ubuntu packages to install if desired # If you don't want to install extra packages then remove this. :packages => ["logwatch"], # Any extra RubyGems to install if desired: can be "gemname" or if a # particular version is desired "gemname -v 1.0.1" # If you don't want to install extra rubygems then remove this :rubygems => ["rails -v 2.3.5"], # Set the server timezone. run "cap -e ec2onrails:server:set_timezone" for # details :timezone => "Canada/Eastern", # Files to deploy to the server (they'll be owned by root). It's intended # mainly for customized config files for new packages installed via the # ec2onrails:server:install_packages task. Subdirectories and files inside # here will be placed in the same structure relative to the root of the # server's filesystem. # If you don't need to deploy customized config files to the server then # remove this. # :server_config_files_root => "../server_config", # If config files are deployed, some services might need to be restarted. # If you don't need to deploy customized config files to the server then # remove this. :services_to_restart => %w(apache2 postfix sysklogd), # Set an email address to forward admin mail messages to. If you don't # want to receive mail from the server (e.g. monit alert messages) then # remove this. :admin_mail_forward_address => "me@gmail.com", # Set this if you want SSL to be enabled on the web server. The SSL cert # and key files need to exist on the server, The cert file should be in # /etc/ssl/certs/default.pem and the key file should be in # /etc/ssl/private/default.key (see :server_config_files_root). :enable_ssl => false }
Be sure to customize those files and read the comments.
Now run cap -T and you should see all the ec2onrails tasks like this.
Also, use the hostname “db_primary” in your database.yml file. After running “cap ec2onrails:server:set_roles” it will resolve to the instance defined in your Capistrano “db” role.
Deploy the site
cap ec2onrails:setup cap ec2onrails:db:set_root_password cap deploy:cold
That should be it! Now goto http://ec2-xxx-xxx-xx-xxx.compute-1.amazonaws.com and your site should be running.
Extended Set Up
This was a very basic set up and you will want to customize your installation.
Cron services
EC2onrails lets you do hourly and daily cron tasks, but not more frequent
than that. I installed craken and have this in my deploy.rb to compensate:
namespace :craken do desc "Install raketab" task :install, :roles => :cron do set :rails_env, "production" unless exists?(:rails_env) set :env_args, (exists?(:env_args) ? env_args :"app_name=#{application} deploy_path=#{current_path}") run "cd #{current_path} && rake #{env_args} RAILS_ENV=#{rails_env} craken:install" end task :uninstall, :roles => :cron do set :rails_env, "production" unless exists?(:rails_env) set :env_args, (exists?(:env_args) ? env_args :"app_name=#{application} deploy_path=#{current_path}") run "cd #{current_path} && rake #{env_args} RAILS_ENV=#{rails_env} craken:uninstall; true" end end before "deploy:symlink", "craken:uninstall" after "deploy:symlink", "craken:install"
You can see more about the capistrano events here. The recipe was taken from here.
Gotchas
- Don't forget to open up port 443 on Amazon's management console if
you need ssl support - Follow these instructions to configure your SSL certs
- Setting up cron is described here
- If you include submodules you can't do the git shallow copy
Useful links
- EC2 on Rails
- Getting
it working with github (has some useful stuff about capistrano)
href="http://kumaria.blogspot.com/2008/12/rails-deployment-capistrano-github.html">Random
guy talking about what he did- The amazon ec2
console
href="http://www.zabada.com/tutorials/deploying-a-rails-application-to-production-on-amazon-ec2.php">Another
guy deploys ec2 on rails- Starling and workling capistrano tasks
Advanced - Setting up a db server
- Setup a cool mysql server backed by EBS here:
href="http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=100&externalID=1663">http://developer.amazonwebservices.com/connect/entry.jspa?categoryID=100&externalID=1663 - This server only has a root user, but capistrano needs an admin user
that is capable of sudoing...d'oh! You have to do the
following:
adduser admin
Now, add your public keypair via these instructions here:
blogs.techrepublic.com.com
I think you have to manually copy the keypair; who knows where it is on
the system??
Now, add the admin user to the /etc/sudoers file so it is what capistrano
is expecting.
# The 'admin' user can run sudo without a password
admin ALL=(ALL) NOPASSWD: ALL

