Continuous Integration and Deployment is in todays development cycle very important. You should be able to easily test and deploy your application. I have some other Posts about how to install a CI Server on EC2, see here: Install Jenkins on Amazon EC2 (Ubuntu 12.04.1 LTS) and how you have to setup Jenkins for a Rails project and Git: Setup Jenkins for a Rails project with Github.

This makes your life easier in your development process. Setup it once at the beginning and then just modify your needs as they come up. This is also an Agile process to continuous inspect and adapt your Continuous Delivery strategy. My next post will cover how to setup Jenkins to deploy automatically after a successful build.

This post will cover and extend the following post from beanstalk: This post works well, but we will need some more to deploy to an Amazon EC2 Server.

First be sure you have on your EC2 Instance Git and Ruby on Rails installed so you are able to deploy and run. You also must have ssh connection to your EC2 instance

This post is just a shorter version with extension from the beanstalkapp blog.


Install Capistrano and Capistrano-ext

Add the following to your Gemfile and execute “bundle update” or “bundle install”


Prepare your Application for Capistrano

Navigate to your Root folder of your application and execute following command that creates a special file called Capfile and ads an template deployment recipe at config/deploy.rb. Delete the content of the deploy.rb file.


Create a Capistrano Recipe

In our empty deploy.rb file we will add the name of our application

Also add the access to the repository

Add the following SSH option to connect to your EC2 instance:


add the user on our server that we want Capistrano to run commands with

i left this empty and it worked.


So, because we maybe have different Servers, example for Testing and Production, or different settings to deploy from local and to deploy from a remote server, lets add the Capistrano Multistage function.

Add this to top of your deploy.rb file


Then lets specify our “stages” or “settings” and say that staging is our default setup.


Now we have to create files for each staging. Create for that a folder called “deploy” in your config directory and add the two files “production.rb” and “staging.rb” to it. To make it easier, add the following to both files:

and you need to tell Capistrano where your pem file lies to connect to your server and also say him which user it is to connect to your server.


Validate your Recipe

Lets create an initial directory structure on your server for future deployments. Run the following command:


Lets check if everything is setup correctly

if you have some errors or problems, fix it and then go to next chapter.



Now, if you want to deploy to our staging server, simply type the following command. Remember, staging is our default setup.

if you want to deploy to production server:


So that’s it. In the next post, i will explain how to setup Jenkins to deploy after a successful build.


Also visit the Tips & Tricks section on Beanstalkapp blog.


Deploy Rails Project with Capistrano and Git to AWS EC2
Tagged on:                 

Leave a Reply


Get every new post on this blog delivered to your Inbox.

Join other followers:

Welcome Damir Kusar

Log in

Lost your password?
%d bloggers like this: