1. Computing
Send to a Friend via Email

Part 5: Deploying

By

Part 5: Deploying

The following article is part of a series, for more article in this series see Creating a URL Shortener in Rails 3.

Now that we've finished and styled our application, it's time to deploy it. Deploying your web application on Heroku is much easier than it sounds. In fact, there's really only one hurdle to clear: porting your application to PostgreSQL. And even that is relatively simple, especially with a simple web application like this. Mostly you'll be setting up Git, installing the Heroku command line tools and then just pushing up to Heroku, very simple.

Heroku is a cloud for web applications. It's basically a giant web host that allows anyone to deploy their web applications there for free. But of course you don't get the whole package for free, just a minimum of features. The rest can be paid for depending on the usage though, so Heroku can be a very efficient and cheap way to deploy small and low traffic web applications.

Port to PostgreSQL

Heroku does not support SQLite out of the box. SQLite isn't really meant to be a high traffic web application store anyway. It's something quick, easy and lightweight that's ideal for development, not necessarily efficient or easy to manage on an environment like Heroku. In order to deploy your web application to Heroku, you're going to need to port your web application to PostgreSQL, a much faster and more manageable database server Heroku uses. But don't worry, for most simple web applications this is trivial.

In a more demanding web application, this would be more difficult. Heroku has some basic information about installing a local copy of PostgreSQL on your development machine. However, it's really not necessary to have PostgreSQL on your local machine for an application so simple. We're using a single table with a single meaningful column, I don't think anything is going to go wrong. So instead, we'll tell Rails to use PostgreSQL only when on Heroku, and use SQLite on the development machine.

To do this, open up your Gemfile. Here you'll see all the gems your project relies on. Near the top, you'll see the line for sqlite3, this is where we want to make our change. We want to tell Rails to load SQLite 3 on the development machine and PostgreSQL on Heroku. In other words, when using the development or test profiles, use SQLite and when using the production profile, use PostgreSQL. So replace the SQLite3 line with the following.


group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

And then run bundler to update your gemfile lock. Since we don't have PostgreSQL on this system, or the pg gem, we're going to tell it not to bundle for development by running bundle install --without production. You'll have to remember to do this every time on your development machine now.

And don't worry about the actual database configuration, this is something that Heroku does for you. You don't need to touch your config/database.yml.

Git

Heroku is based on Git. You can't do anything on Heroku without Git so if you haven't been using Git thus far, you'll need to install it and start. But all you really need are a few commands, if you don't want to use git for your normal workflow you don't have to. First, create the git repository and add your files.


$ git init
$ git add .
$ git commit -am 'initial commit'

Even if you don't plan on using Git for version control, you're going to need to do this. After making any changes, make sure you commit. And after adding any new files, make sure you add them. If you ever get confused about what git wants you to do, use the command git status.

Deploy It

Sign up on Heroku.com. Don't worry, it's free to start with. Once that's done, you'll need to install the Heroku Toolbelt via the directions on their site. This will install the heroku command line client you'll need to interact with Heroku. You're almost there, I told you this was easy.

You need to log in using the Heroku command line client now.


$ heroku login

Next, using the Heroku command client client, we're going to create a new application using the command heroku create. This will create a virtual machine on the Heroku cloud for your application (really just an instance of Ubuntu Linux) and add a git remote to your local git repository. Git remotes are what allow you to push your application up to Heroku.

Really, this is it. Just push the application up to Heroku and run the database migration. After that, you can visit your now deployed web application with the URL it gives you.


$ git push heroku master
…
$ heroku rake db:migrate

I've deployed the URL shortener application to Heroku, you can see it running here.

©2014 About.com. All rights reserved.