Deploy Rails application to Apache + Passenger on Amazon EC2

I recently deployed a rails application to Amazon EC2. Here are some of notes which maybe helpful to others doing similar tasks.

Assume ruby and rails are already installed in your amazon EC2 instance, below is the steps to deploy your rails application to Apache:

1. Install Apache
sudo -s
yum install httpd
yum install httpd-devel

2. Install Passenger:
gem install passenger

3. Configure Passenger with apache:
rvmsudo passenger-install-apache2-module
(then follow the steps as indicated in the command line window)

4. Deploy your application to the server and change the configuration file to point to the correct direction. (/etc/httpd/conf/httpd.conf)
(Important Note: if you are trying to deploy a development version of your application, you must specify RailsEnv as development in your Apache configuration)

Below is an example of the Apache configuration:

ServerName http://www.yourdomain.com
DocumentRoot /home/ec2-user/your_rails_app/public
RailsEnv development

Allow from all
Options -MultiViews

Deploy rails app to Amazon EC2 with capistrano and Git
After several days of coding, I have a working prototype on my local machine. Now it’s time to deploy the application to my EC2 server. I decided to use Capistrano as my continues delivery framework and here are the steps:
Step1: Install Capistrano and capity your application
gem install capistrano
in your application’s root folder, run
captify .
step2: configure the generated deploy.rb file:
Below is an sample of the deploy.rb
set :application, “youappname”
set :repository, “https://github.com/your/repository/path”

set :scm, :git

set :ssh_options, {:forward_agent = true}
set :use_sudo, false
set :deploy_to, “/your/deploy/path”
set :user, “username_in_ec2”

role :web, “www.yourserver.com” # Your HTTP server,
role :app, “XXX.XXX.XXX.XXX” # This may be the same as your `Web` server
role :db, “XXX.XXX.XXX.XXX”, :primary => true # This is where Rails migrations will run

namespace :deploy do

task :start do ; end

task :stop do ; end

task :restart, :roles => :app, :except => { :no_release => true } do

run “#{try_sudo} touch #{File.join(current_path,’tmp’,’restart.txt’)}”

end

end

The trick here for deploying to EC2 is that normally you remote to your EC2 instance using ssh, while capistrano asks you for password. To enable ssh, we need to set :ssh_options “{:forward_agent = true}” and also add the private key identities to the authentication agent. To do that, copy your EC2 private key to ~/.ssh/keyname.pem

Then go to ~/.ssh and run the command:
ssh-add keyname.pem

Now capistrano will use the private key to log on to EC2 instance.

Another thing to notice is “set :use_sudo, false”.

This is because Amazon assigns you an user with root privilege. If you use sudo, all the folders capistrano create will belong to “root” thus you have to manually change owner. Setting use_sudo to false will avoid all these hassles.
Step 3: prepare your server
Basically you need to prepare your server with rails environment and the data base, to create database for production, run the following:
RAILS_ENV=production rake db:create db:migrate
Setp 4: set-up server and get code from Git:
cap deploy:setup
This will create necessary folders in your server. And
cap deploy:check
This will check both the local and remote environments for dependencies. If something missed, fix them as promopted.
cap deploy:update
This will get the latest code from git

Setp 5: start, update, restart,
After everything is done, you start the application by running:
cap deploy:start
When you have new code push to Git, update server and restart passenger
cap deploy:update
cap deploy:start

Advertisements
Leave a comment

Leave your opinion

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: