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

Sunday, August 1, 2010

Setting up Ruby on Rails Environment in Windows

Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language. It is intended to be used with an Agile development methodology that is used by web developers for rapid development.
The following things are required in order to set up the ROR environment.
1Ruby Installation
2Testing the Ruby environment
3Rails Installation
4Setting up the server
5Setting up the database
6Testing the ROR environment
Ruby Installation

Rails framework is created using Ruby language and hence setting up the Ruby environment is the first thing to proceed. Download the latest ruby file ruby186-26.exe for windows and install it.
Testing the Ruby environment
Before moving on to rails, we need to test whether our ruby environment is set up properly or not. There are many tutorials available for testing it and you can check this blog Execute RUBY program on Command Prompt which is very simple to check at this stage.
Rails Installation
Once the ruby enviroment is ready, it is the time to install rails.Type the following in command prompt

gem update --system [To update the system with latest gems for ruby]
Ex: C:\ruby\prg>gem update --system
gem install rails --include-dependencies [Installation of rails along with all dependencies]
Ex: C:\ruby\prg>gem install rails --include-dependencies
The above installation is completed by automatically downloading from ruby site.
Setting up the server
Once the application is installed, we need to check whether the server setup is completed.By default, WEBrick server is coming along with rails installation. There is no seperate installation required.
In order to check the server, a sample application is required.
Type the following in command prompt
rails MyRails [creating an application called MyRails]
Ex: c:\rails>rails MyRails
Ex: c:\rails>ruby script/server [start the server & You will get a message console as given below....]
=> Booting WEBrick
=> Rails 2.3.8 application starting on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-07-24 00:12:41] INFO WEBrick 1.3.1
[2010-07-24 00:12:41] INFO ruby 1.8.6 (2007-03-13) [i386-mswin32]
[2010-07-24 00:12:41] INFO WEBrick::HTTPServer#start: pid=14200 port=3000
Now you can launch the application, http://localhost:3000 and a default welcome page will be displayed.
If you have done the above process, then server setup is completed.
Setting up the database
Rails can be easily configured with SqlLite database.We can install the database by remote using the below command.
install -r sqlite3-ruby
Ex: c:\rails>install -r sqlite3-ruby [remote installation of sqlite3 database]
In order to run the database, dll file is required and you can download here sqlit3 DLL Download
Download and extract the file[sqlite3.dll, sqlite3.def] in C:\WINNT\system32 folder.

Now your system is ready for working with database.
There are some firefox Add-Ons available in order to view the SQLite database.
Testing the ROR environment

You can test the above environment by writing sample applications.One of the simple tutorial to get started with is given in the blog, Ruby On Rails for Beginners
Now the ROR environment is ready you can successfully run any type of applications in Rails.
1Rails Forum
2Derek Anderson's blog
3Ruby Forum
4Execute Ruby Program on Command Prompt
5SqLite Downloads
6Ruby on Rails for Beginners