Friday, August 6, 2010

Deploy Rails on Heroku

Heroku is one of the popular web based ruby hosting platform to launch our rails application. We can host our ROR demo applications free of cost to a certain level. Features like repository support, database support,etc are available. Using this we can host our application, work on that application from anywhere and update the application at anytime.
The following things are required in order to deploy the ROR application on Heroku.
1Heroku Account Setup
2GitHub Account Setup
3Repository Setup - Git Installation
4Public(ssh) key generation
5Repository Authentication
6Heroku Installation
7ROR Application setup
8Launch Application in Heroku
Heroku Account Setup

An account is needed in order to work with Heroku.create an account from the Heroku site. Your username and password credentials are required in order to deploy the application. You can also create an application inside the heroku account for checking purpose.

GitHub Account Setup

GitHub account is required in order to authenticate the public keys in repository.Create a free account in the Git Hub site
After you logged in to the account, you need to upload the SSH public keys generated in the Public(ssh) key generation.

Repository Setup - Git Installation
In order to work with heroku, git repository is required.Download the latest Git version and install it.
Public(ssh) key generation
A public SSH key is required in order to authenticate the remote servers.
There are different ways to create SSH public key.
If your Git repository installation is already done, you can create as like below.
Type the following in command prompt
set path=%path%;C:\Program Files\Git\bin; [setting the git path]
cd C:\Program Files\Git\bin [Move to the bin folder]
ssh-keygen -t rsa -C "emailid" [generating a key pair with your email id] and id_rsa files will be created. Use these two files wherever, ssh authentication is required.Copy the key from and paste it in the public key of github account.
For more information, refer Generating SSH keys
Repository Authentication

In order to test your git remote repository, you need to copy the SSH key files( and id_rsa) and paste it in .ssh folder in [C:\Program Files\Git\.ssh].

Type the following in the commmand prompt
if Successfully authenticated message comes, then your remote repository setup will be working fine.
Heroku Installation
In order to deploy rails on heroku, Heroku gem needs to be installed in your system.
Type the following in the commmand prompt
set path=%path%;c:\ruby\bin; [set the ruby path]
gem install heroku [Remote installation of heroku gem]
heroku list[To list down the deployed applications]
It will ask to enter your heroku user crendentials.If, No public ssh key found error will be displayed, it means that, ssh authentication key is required. For that, you need to copy the SSH key files( and id_rsa) and paste it in .ssh folder in C:\Documents and Settings\[user]\.ssh.
Now if you run heroku list command again, successful authetication occurs by entering the user credentials and it will list down the applications that are currrently running in your heroku account.
ROR Application setup

Before starting with heroku, we need to ready with our ROR application. If it is not ready yet, refer the blogs, Setting up Ruby on Rails Environment and Ruby on Rails for Beginners in order to get ready with the application.

Launch Application in Heroku
Now, all the setup is ready and we are ready to launch the application.
As a first step, move to rails application directory in the command prompt and initialise the git repository. Type the following in command prompt
git init [initialise the application directory]
git add . [add the files to the repository]
git commit -m "your comment" [commit the files in the directory]
Now your application is initialized and added to git repository.
As a second step, an application needs to be created on heroku and it has to be added to the remote repository.
heroku create app-name [creating the application on heroku with the specified name]
git remote add heroku [initialise the application to remote repository]
As a third step, push the code to the master repository.
git push heroku master [push the code to master repository on heroku server]
If it is first application to launch in heroku, then create .gems file with the rails version as shown below and put it in root folder to rectify the warning.
rails -v 2.3.8
Once done, run the following command for db creation in heroku
heroku db:migrate [creating db structure in heroku server]
Now the ROR application on heroku is ready and you can access it from anywhere.
1Heroku site
2Git Hub Site
3Ruby On Rails for Beginners
4Setting up Ruby on Rails Environment in Windows
5Hosting your Rails app – first look at Heroku
6Getting Started with Heroku on Windows

No comments:

Post a Comment