1.4 Deploying
Even at this early stage, we're already going to deploy our (still-empty) Rails application to production. This step is optional, but deploying early and often allows us to catch any deployment problems early in our development cycle. The alternative—deploying only after laborious effort sealed away in a development environment—often leads to terrible integration headaches when launch time comes.23
Deploying Rails applications used to be a pain, but the Rails deployment ecosystem has matured rapidly in the past few years, and now there are several great options. These include shared hosts or virtual private servers running Phusion Passenger (a module for the Apache and Nginx24 web servers), full-service deployment companies such as Engine Yard and Rails Machine, and cloud deployment services such as Engine Yard Cloud and Heroku.
My favorite Rails deployment option is Heroku, which is a hosted platform built specifically for deploying Rails and other Ruby web applications.25 Heroku makes deploying Rails applications ridiculously easy—as long as your source code is under version control with Git. (This is yet another reason to follow the Git setup steps in Section 1.3 if you haven't already.) The rest of this section is dedicated to deploying our first application to Heroku.
1.4.1 Heroku Setup
After signing up for a Heroku account, install the Heroku gem:
$ [sudo] gem install heroku
As with GitHub (Section 1.3.4), when using Heroku you will need to create SSH keys if you haven't already, and then tell Heroku your public key so that you can use Git to push the sample application repository up to their servers:
$ heroku keys:add
Finally, use the heroku command to create a place on the Heroku servers for the sample app to live (Listing 1.7).
Listing 1.7. Creating a new application at Heroku.
$ heroku create Created http://severe-fire-61.heroku.com/ | git@heroku.com:severe-fire-61.git Git remote heroku added
Yes, that's it. The heroku command creates a new subdomain just for our application, available for immediate viewing. There's nothing there yet, though, so let's get busy deploying.
1.4.2 Heroku Deployment, Step One
To deploy to Heroku, the first step is to use Git to push the application to Heroku:
$ git push heroku master
(Note: Some readers have reported getting an error in this step related to SQLite:
rake aborted! no such file to load -- sqlite3
The setup described in this chapter works fine on most systems, including mine, but if you encounter this problem you should try updating your Gemfile with the code in Listing 1.8, which prevents Heroku from trying to load the sqlite3-ruby gem.)
Listing 1.8. A Gemfile with a Heroku fix needed on some systems.
source 'http://rubygems.org' gem 'rails', '3.0.1' gem 'sqlite3-ruby', '1.2.5', :group => :development
1.4.3 Heroku Deployment, Step Two
There is no step two! We're already done (Figure 1.11). To see your newly deployed application, you can visit the address that you saw when you ran heroku create (i.e., Listing 1.7, but with the address for your app, not the address for mine).26 You can also use a command provided by the heroku command that automatically opens your browser with the right address:
$ heroku open
Figure 1.11 The first Rails Tutorial application running on Heroku.
Once you've deployed successfully, Heroku provides a beautiful interface for administering and configuring your application (Figure 1.12).
Figure 1.12 The beautiful interface at Heroku.
1.4.4 Heroku Commands
There are tons of Heroku commands, and we'll barely scratch the surface in this book. Let's take a minute to show just one of them by renaming the application as follows:
$ heroku rename railstutorial
Don't use this name yourself; it's already taken by me! In fact, you probably shouldn't bother with this step right now; using the default address supplied by Heroku is fine. But if you do want to rename your application, you can implement the application security mentioned at the start of this section by using a random or obscure subdomain, such as the following:
hwpcbmze.heroku.com seyjhflo.heroku.com jhyicevg.heroku.com
With a random subdomain like this, someone could visit your site only if you gave them the address. (By the way, as a preview of Ruby's compact awesomeness, here's the code I used to generate the random subdomains:
('a'..'z').to_a.shuffle[0..7].join
Pretty sweet.)
In addition to supporting subdomains, Heroku also supports custom domains. (In fact, the Ruby on Rails Tutorial site lives at Heroku; if you're reading this book online, you're looking at a Heroku-hosted site right now!) See the Heroku documentation for more information about custom domains and other Heroku topics.