How to deploy Ruby on Rails Application on Amazon EC2

Hello guys,

I am using Ubuntu 10.04, Rails 2.3.8 and Ruby 1.8.7. I want to deploy my rails application on Amazon EC2. But before that we have to create instance on Amazon and configure it. So I will show you how to do this.

First of all I am not using ec2onrails gem because it isn’t maintained regularly and supports old AMI . Then I assume that you are somewhat familiar with Amazon terms and you have account there. If not then please do it first.

After creating account and undestanding Amazon terms you need to have following things which you can find from Security Credentials tab from Here.

1) Amazon Account Number
2) Access Key ID
3) Secret Acsess Key
4) Amazon x.509 Certificate
5) Private Key Certificate

Now download developer tools from Here and then extract it and put it in some folder.

I put certificates and developer tools in /home/pulkit/cert folder.

now install Java if you don’t have.

So after downloading all necessary things, let’s start for setup.

# Set Java Path
1) export JAVA_HOME=/usr/lib/jvm/java-6-openjdk/
2) export PATH=$PATH:$JAVA_HOME/bin

# Set Private Key
3) export EC2_PRIVATE_KEY=/home/pulkit/cert/pk-PUL4MOWVQI4IOOKRV3BRJC2MHXJ2KKIO.pem

# Set Certificate
4) export EC2_CERT=/home/pulkit/cert/cert-PUL4MOWVQI4IOOKRV3BRJC2MHXJ2KKIO.pem

# Set api-tools
5) export EC2_HOME=/home/pulkit/cert/ec2-api-tools-1.3-62308
6) export PATH=$EC2_HOME/bin:$PATH

# add keypair to Amazon
# Save it in /home/pulkit/.ssh folder with name “id_rsa-rails-keypair”
7) ec2-add-keypair rails-keypair

# create instance by choosing AMI
# There are lots of AMI available in the market. Choose it as per your requirement. To see list of AMI and it’s details, visit Here.
8 ) ec2-run-instances ami-66e2130f -k rails-keypair

# See details of instance
# change i-9f57h52f3 with your instance id. You will get it when you will run above command.
9) ec2-describe-instances i-9f57h52f3

# Allow Port 22 and 80
# Unfortunately If you will get error
# “Client.InvalidPermission.Duplicate: The permission ’0.0.0.0/0-1-22-22′ has already been authorized on the specified group” for ec2-authorize default -p 22
# OR
# “Client.InvalidPermission.Duplicate: The permission ’0.0.0.0/0-1-80-80′ has already been authorized on the specified group” for ec2-authorize default -p 80
# it means that your ports are already allowed. You can move to next steps.
10) ec2-authorize default -p 22
11) ec2-authorize default -p 80

# If you are using git repository then copy your id_rsa.pub(github key) to Amazon Instance.
# As per your instance, username can be different(root or ubuntu). Make sure that you set name as “authorized_keys2″ not “authorized_keys”. Because “authorized_keys” is already there.
12) scp -i ~/.ssh/id_rsa-rails-keypair ~/.ssh/id_rsa.pub ubuntu@ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com:.ssh/authorized_keys2

# login to Amazon Instance using SSH command.
13) ssh -i /home/pulkit/.ssh/id_rsa-rails-keypair ubuntu@ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com

Now you should be in Amazon Instance. Install all necessary packages and gems as per your requirements.

In next post I will show you how to deploy rails application using capistrano on Amazon Instance.

Thanks & Regards,
Pulkit J. Pancholi

Reference : http://blog.dhavalparikh.co.in/2011/01/deploying-rails-application-on-amazon-ec2/

Populate data

Hello guys,

Let’s take a look below situations:

  • During development mode many times we run command “rake db:migrate VERSION=0″ (so all data will be lost!)
  • Our rails application is ready to run and we give it someone to test it. (without data???)
  • Client wants that developer will test on his test data so both can understand it very well. (will developer enter data manually?)

In above all situations we want development data again(of course easily! ). How can we get it? There are some options for this given below:

Seeds is nice option but it will be boring to write all data and populator and faker gem will populate fake data not development data.

So now what to do?

Let’s introduce yaml_db!!!

It’s very easy to use.

  • Install yaml_db as a plugin or gem
  • If you want to give your development data to someone else then run command “rake db:data:dump”
  • This command will create one file named “data.yml” at RAILS_ROOT/db folder. This file contains your all development data.
  • Now if someone wants to use this file then user will put this file at above location and will run command “rake db:data:load”
  • So development database will populate with all data and that’s it!

Isn’t it simple then you thought?

Generate Reports, Graphs, and Documents with Ruby on Rails

Hi guys,

I am sure that you see sometimes various graphs and charts on sites.

Have you ever think to integrate those type of beautiful graphs and charts with your Ruby on Rails sites?

I found some gems and a plugin that can fulfill your wish.

  • Gruff: This gem is very good and very old. So you can find very much info. about it. I suggest you to read book “Practical Reporting with Ruby and Rails: Create and present attractive reports, graphs, and documents using Ruby on the Web, on the desktop, and on the server by David Berube

    For online help please visit http://nubyonrails.com/pages/gruff

  • Scruffy: This gem can be your second choice if you don’t like Gruff. To know much about it please visit http://scruffy.rubyforge.org/
  • Open Flash Chart 2: This plugin is the best that I found till now for creating graphs and charts. To know more about it please go through http://pullmonkey.com/projects/open_flash_chart2/

Please keep in mind that above two gems will create image and Open Flash Chart 2 plugin will create Flash Object so you are supposed to deal with .swf file. But Don’t scare if you don’t know much about it because you can find online free source on it’s  site.

Rails API Offline???

Have you ever think or have you find youself in a condition that you want to use Rails API but net is down or http://api.rubyonrails.org/ is not working!!!

Well than cheers!!!

because now you can get latest Rails API offline.

How???

Just follow the below steps:

Go to  http://railsapi.com/ and Download the zip file.

Extract it and open index.html file in your favourite browser.

It gives API with awsome CSS and fine search functionality.

So, that’s it!!!

So what are you waiting for?

Go and download the API before your net will go down.

Simple Captcha in Rails Application

Hello,

Today I will tell you about Captcha.

You must saw different kinds of captcha on the registration page.Well that is for security purpose so any hacker can’t create fake users using any program or script.This is very serious problem on the website.But the solution is much simpler.

USE CAPTCHA!!!

Now, which is the best option to implement captcha in rails?

I found a plugin for captcha named “Simple Captcha”.It has quality same as it’s name.It’s damn easy and you supposed to add only one line of code in your view file(Yes!!! Only one line of code. Believe me!)

You can find it here: http://expressica.com/simple_captcha

After installing this plugin write only one line in view file as below:

<%= show_simple_captcha %>

That’s it.

Keep in mind that to use this plugin you have to install RMagick first.
To know more about RMagick click here: http://rmagick.rubyforge.org

You can do custom changes as per your requirements in the plugin.Go through readme file for more details.

So add captcha in your application and have a safe application!

Rails AJAX Pagination with will_paginate plugin

Hi Guys,

If you are using will_paginate plugin for pagination then sometimes you may have a requirement for AJAX pagination.

I am damn sure that it is much simple instead of your thinking that how hard to do that.

First go through this link: http://weblog.redlinesoftware.com/2008/1/30/willpaginate-and-remote-links

It describes very well AJAX Pagination.

However I did some custom changes for my application.

In my view file I wrote:

<%= will_paginate must_have_item, :renderer => ‘RemoteLinkRenderer’, :remote => {:with => “‘must_have_items=true'”, :update => ‘must_have_gallery_whole’} %>

In above code,

  • must_have_item is an item array.
  • RemoteLinkRenderer is a file that you are supposed to include it in helper file(DO NOT CHANGE ANY CONTENT OF IT.)
  • must_have_items=true is a parameter that I passed with this request
  • must_have_gallery_whole is an id of div that updates after every AJAX request

Now In controller I wrote,

if request.xml_http_request?()
if params[:must_have_items]

In above code,

  • request.xml_http_request?() will get the AJAX request
  • params[:must_have_items] will be true as I passed “must_have_items=true” in view file.

So, write whatever you want after this two if conditions.

This is the way AJAX Pagination is working.

As I told you earlier  it is very easy.

Now cheerssssss…….

Currency Converter

Hi Guys,

Some days before in my project there was a requirement for currency converter.

I did RND and found only one good plugin for it.

It works fine in my application so it is reliable. (though it is little bit slow as it fetches latest currency rate from the net everytime you make a request.)

The name of plugin is cash_handler.

You can find it here: http://jamesbrooks.net/2009/01/05/rails-cash-handler-plugin/

Believe me it’s very easy to implement and best one.

It provides also facility to update currency rates as you want after some specific time.

by Default it is 24 Hours.

Don’t forget to install hpricot gem before installing this plugin.

So Use this plugin and contact me if you have any problems or suggestions.

Have a Great Day!!!

Follow

Get every new post delivered to your Inbox.