How to deploy your web app to Amazon EC2 using Capistrano

by

What is Capistrano?

Capistrano is an open source tool mainly used to deploy web applications from source code management (SCM) to one or more servers. The aim of this guide is showing how to easily deploy your app to amazon EC2 using Capistrano. We can leverage its multi-stage extension to provide a different deployment strategy in different scenarios.

Install Capistrano and Capistano-ext

The first step to install Capistrano is getting the last ruby version. My suggestion is installing ruby through the Ruby Version Manager. Below you can find the command to do it:

 curl -L get.rvm.io | bash -s stable --ruby 

if you want to have more information about rvm visit: https://rvm.io/.
Once ruby is installed in your system, getting Capistrano and Capistrano-ext will be as easy as entering the following command:

sudo gem install capistrano capistrano-ext

Configure Capistrano

To configure Capistrano, go to your project’s directory and enter:

capify .

This command will create in your current directory the Capfile and the folder config containing the deploy.rb file. To create also Capistrano-ext required files enter:

cd config/; mkdir deploy; cd deploy; touch production.rb staging.rb

Now it’s time to open the file deploy.rb with your favorite editor and put in the right configurations for your app. Below is an example on what your final deploy.rb might look like:

set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'

set :application, "myapp"
set :repository, "git@github.com:myuser/myapp.git"
set :scm, :git

set :deploy_to, "/var/www/myapp"

desc "check production task"
task :check_production do

if stage.to_s == "production"

puts " \n Are you REALLY sure you want to deploy to production?"
puts " \n Enter the password to continue\n "
password = STDIN.gets[0..7] rescue nil
if password != 'mypasswd'

puts "\n !!! WRONG PASSWORD !!!"
exit

end

end

end

before "deploy", "check_production"


the check_production task is an example that shows what Capistrano tasks look like. Its purpose is prompting the user for a password before the app is deployed in production. The instruction to trigger the task is the one at the very bottom of the file: before “deploy”, “check_production”.

To write more tasks, some basic knowledge of ruby and bash scripting is required. A typical task would be for example running all the tests and make sure they all pass before deploying.

Let’s now take a look at the staging.rb and production.rb files. Their main purpose is to define where the servers are located (:server) and how to access them (:user and ssh_options[:keys]).

Here you can find an example of the two files:

#production.rb
set :user, "production_user"
server "ec2-xxx-xxx-xxx-xxx.us-west-1.compute.amazonaws.com", :app, :web, :db, :primary => true
ssh_options[:keys] = ["#{ENV['HOME']}/Path/To/ProdKey/ec2-prod-key.pem"]


and

#staging.rb
set :user, "staging_user"
server "ec2-yyy-yyy-yyy-yyy.us-west-1.compute.amazonaws.com", :app, :web, :db, :primary => true
ssh_options[:keys] = ["#{ENV['HOME']}/Path/To/StagKey/ec2-stag-key.pem"]

Of course, you need to replace the content of the files with your actual server addresses and the paths to your keys to access those servers.
The hard part is now taken care of. To deploy your app, first enter:

cap deploy:setup

which will ssh to the remote machine and create in it the required folders where you specified (make sure the user you are ssh-ing with, has the right write permissions).
To check that the setup was correctly executed enter the command:

cap deploy:check

if the output message looks like “You appear to have all necessary dependencies installed”, you’re ready to deploy your app by simply entering:

cap deploy staging

(or cap deploy production if that’s the server you want to deploy to) and Capistrano takes care of the process of deploying a new version of the application that includes linking these files into the current-version tree in the right spots.

For further reading on the topic, here are some useful links:

https://github.com/capistrano/capistrano/wiki
https://github.com/jamis/capistrano-ext

4 Comments

  1. Tom Harrison on said:

    For those of us finding this excellent post in 2015, please note that new applications should most definitely use Capistrano 3, which is a little different (and better) than the older version. There’s a nice gem called Cap EC2 (https://github.com/forward3d/cap-ec2) can help if you have a complex EC2 cluster and multiple environments to manage.

  2. Evgeny Gurin on said:

    Good capistrano tutorial but i didn’t see anything special about EC2

  3. Kushal on said:

    Useful , Thks a lot .

  4. Bernardo Simoes on said:

    Thanks for the tutorial, but for what reason did you create two different users?

Leave a Reply

Your email address will not be published. Required fields are marked