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.19
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 Nginx20 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.21 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:
$ 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.9).
Listing 1.9. Creating a new application at Heroku.
$ heroku create --stack cedar
Created http://stormy-cloud-5881.herokuapp.com/ |
git@heroku.com:stormy-cloud-5881.herokuapp.com
Git remote heroku added
(The --stack cedar argument arranges to use the latest and greatest version of Heroku, called the Celadon Cedar Stack.) 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
1.4.3. Heroku Deployment, Step Two
There is no step two! We’re already done (Figure 1.10). To see your newly deployed application, you can visit the address that you saw when you ran heroku create (i.e., Listing 1.9, but with the address for your app, not the address for mine). You can also use an argument to the heroku command that automatically opens your browser with the right address:
$ heroku open
Figure 1.10. The first Rails Tutorial application running on Heroku.
Because of the details of their setup, the “About your application’s environment” link doesn’t work on Heroku. Don’t worry; this is normal. The error will go away (in the context of the full sample application) when we remove the default Rails page in Section 5.3.2.
Once you’ve deployed successfully, Heroku provides a beautiful interface for administering and configuring your application (Figure 1.11).
Figure 1.11. The beautiful interface at Heroku.
1.4.4. Heroku Commands
There are many 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 arrange for it to be reasonably secure 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 him or her 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.