Deployment

So far, we have used Rails to develop, test and demonstrate a web application. Since the web browser was running on the same computer as Rails and its web server, we could simply access the web application using localhost as part of the URL. Here, however, we discuss options for accessing the web application over the internet. The following sections present options ranging from worst performance (but simplest) to the best performance.

Running Rails in the development environment

If we don't care at all about performance, we can simply run the server in the default development environment. If the computer is on the internet and has an IP number (example: 67.186.79.78), the reviews web application could be accessed with this URL:

   http://67.186.79.78:3000/reviews

If your computer has a registered host name (example: itclass.com) through a Domain Name Server (DNS), you can access the web application accordingly:

   http://itclass.com:3000/reviews

Finally, if you start the server with this option:

   rails server -p 80

The server will run on port 80, which is the default port for a URL. This way, the URL does not need to specify the port number:

   http://itclass.com/reviews

Note that some internet service providers block port 80, which will prevent you from running the server on this port.

Running Rails in the production environment

Performance is greatly increased by running a Rails application in the production environment. Here are some important differences from the development environment:

All but the last item improves the performance of the web application.

In order to run your web application in the production environment, you need to do the following steps:

  1. Create the database for the production environment:
  2.    rake db:schema:load RAILS_ENV=production
  3. Since the database is empty, you'll need to add any needed initial records, possibly through the console in the production environment:
  4.    rails console production
  5. Start the server in the production environment using a port of your choice:
  6.    rails server -e production -p 80

Assuming you have a recent installation of Rails, your Rails application uses Mongrel as the web server. Mongrel passes all web requests to your Rails application.

This diagram illustrates this simple configuration, where Mongrel is the only web server operating in this deployement:

Using a front-end web server

Mongrel is not intended as an efficient server for static pages. A common deployment strategy uses a production-quality web server (e.g. Apache) on the front-end:

Front-end server with multiple back-end instances

A production-quality server such as Apache is usually multi-threaded and can handle multiple requests simultaneously. Since Mongrel and the rail application is single-threaded, handling multiple requests simultaneously involves creating multiple instances of Mongrel running the rails application:

Servers such as Apache can balance the load of requests among the multiple instances of back-end servers. Phusion Passenger (installed on topaz) uses this approach.

Production Database

Unless your application will only have a few users accessing the application at a time, you will probably want to use a production-quality database such as MySQL, which can better handle multiple requests at a time. You will either need to create a database in MySQL or ask a system administrator to do it for you. With the database name, user name and password, you can then edit the config/database.yml file to configure for MySQL in the production environment.