Tuesday, 3 January 2012

Ruby on Rails : Hosting your Ruby on Rails application in Heroku

In my previous post,i have mentioned about creating a Ruby on Rails application using devise,Now in this tutorial we are going to host Ruby on Rails database backed application (Rails community web site) to Heroku.
you all know that Heroku is a polyglot cloud application platform. With Heroku, you don’t need to think about servers at all. You can write apps in the programming language of your choice, back it with add-on resources such as SQL and NoSQL databases.You can manage your app using the Heroku command-line tool and you deploy code using the Git revision control system, all running on the powerful Heroku infrastructure.
We have created our Rails community website application with a MySQL database support.The only problem that you have to face while hosting your application in Heroku is that Heroku doesn’t support MySQL , Heroku only supports PostgreSQL database for your application.
Sign up : 
Before all, you have to create an account in Heroku,
Install the Heroku Toolbelt on your system :
If you are using a Ubuntu/Linux machine,follow the steps below.
$ apt-add-repository 'deb http://toolbelt.herokuapp.com/ubuntu ./'

$ curl http://toolbelt.herokuapp.com/apt/release.key | apt-key add -

$ apt-get update

$ apt-get install heroku-toolbelt

Login :
After installing the Toolbelt, you’ll have access to the heroku command from your command shell. Log in using the email address and password you used when creating your Heroku account:
$ heroku login

Enter your Heroku credentials.
Email: adam@example.com
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Install the Heroku Command-line Client :
Install the Heroku client by running the below command.
$ gem install heroku
Change the directory to where your application resides.
$ cd rails-club
Since Heroku doesnt support MySQL,You have to edit your gem file and the change the line
gem 'mysql2', '0.3.6'
to this,
gem 'pg'
Before that you should have to install the PostgreSQL in your system.
Once you have done all steps detailed above,then run the command “bundle install” to re-install your dependencies (to generate a new Gemfile.lock)
$ bundle install
Initialize git to store your application in git :
$ git init
$ git add .
$ git commit -m "init"
Deploy to Heroku/Cedar :
Create the app on the Cedar stack:
$ heroku create --stack cedar                                        
Creating fierce-fog-5356... done, stack is cedar
http://fierce-fog-5356.herokuapp.com/ | git@heroku.com:fierce-fog-5356.git
Git remote heroku added
Deploy your code:
$ git remote add heroku git@heroku.com:fierce-fog-5356.git
If you found an error reporting like,
fatal: remote heroku already exists.
run the following command,
$ git remote rm heroku
then you have to run the above command again.

$ git remote add heroku git@heroku.com:fierce-fog-5356.git
and finally run the following command to deploy your application in Heroku
$ git push heroku master
If the application hosted successfully in Heroku you will see something like
Launching... done, v5
       http://fierce-fog-5356.herokuapp.com deployed to Heroku
on your console.
After that you have to run the rake db:migrate command in Heroku, Rake can be run as an attached process exactly like the console.
$ heroku run rake db:migrate
copy and paste the Heroku application URL (“ http://fierce-fog-5356.herokuapp.com “) in your browser search bar.Now  you have successfully hosted your Rails application in Heroku.
If you have just pushed to Heroku and want to make sure that you are up-to-date, just run,
$ git remote show heroku         
then you will get output like this in your console.

* remote heroku
  Fetch URL: git@heroku.com:xxxx.git
  Push  URL: git@heroku.com:xxxx.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local ref configured for 'git push':
    master pushes to master (up to date)

No comments:

Post a Comment