Running Ruby applications with Mongrel and Apache2

Posted by Steve on Thu 1 Feb 2007 at 15:35

When you start working with Ruby on Rails applications you're probably content with using the integrated HTTP server, webbrick, for development. Once you're using them in production though you'll want something more capable. This is where mongrel comes in.

There are several options for integrating Ruby on Rails applications within an Apache2 server, the most common are:

By default when you've created a new Ruby on Rails application several files with have been generated for you, notably:

Using the server is very simple, but it doesn't scale well. To start it just run script/server:

steve@steve:~/Rails/Books$ ./script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-02-01 15:11:41] INFO  WEBrick 1.3.1
[2007-02-01 15:11:41] INFO  ruby 1.8.5 (2006-08-25) [i486-linux]
[2007-02-01 15:11:41] INFO  WEBrick::HTTPServer#start: pid=9558 port=3000

Here you can see that we've started a server listening upon port 3000, with the WEBrick HTTP server. This allows you to test your application.

When it comes to deployment the simplest solution is to install the mongrel HTTP server - this is a small server which can be used to start your application, and is powerful enough to be used in production.

(Mongrel also supports clustered operation, which we'll not discuss here.)

To install Mongrel you'll use the gem command, which allows you to add or remove ruby-specific extensions upon your Debian machine. If you've installed the rubygems package from the source distribution you may execute:

root@host:~# gem install mongrel

The Debian rubygems package places the gem command in a location which is probably not upon your PATH, so you will need to fully qualify the location of the command:

root@host:~# /var/lib/gems/1.8/bin/gem install mongrel

Either way you should see something like the following:

Bulk updating Gem source index for: http://gems.rubyforge.org
Select which gem to install for your platform (i486-linux)
 1. mongrel 1.0.1 (ruby)
 2. mongrel 1.0.1 (mswin32)
 3. mongrel 1.0 (mswin32)
...

Enter "1" to choose the most recent version of the server, which is not compiled for Microsoft Windows.

Note: You must have a compiler and development environment setup, as well as the appropriate ruby-dev package in order to build the shared-library mongrel uses.

If you're missing these you can install them by running:

root@host:~# apt-get install ruby1.8-dev build-essential 

Once Mongrel has been installed you can start it up to host your project by executing the following, from the root of your application:

skx@host:~/Rails/Books$ mongrel_rails start -d -e production -a 127.0.0.1 -p 3000

Here we've used several arguments (for an overview of more options run "mogrel_rails start -t") here is a quick explanation of what they mean:

Note: you don't need to start the server as root, since you're binding to a "non privileged" port.

All being well you should receive no errors and browsing at http://localhost:3000 should show you your applications front page.

To stop the server you may run:

skx@host:~/Rails/Books$ mongrel_rails stop
Sending TERM to Mongrel at PID 9698...Done.

If this works we can look at adding a handler for Apache2 to make it externally visible.

The first thing to do is to make sure that you have the mod_proxy module enabled for Apache. To do this run:

root@host:~# a2enmod  proxy
Module proxy installed; run /etc/init.d/apache2 force-reload to enable.

Now create a configuration file for your host within the directory /etc/apache2/sites-available/ - here we'll be using the hostname test.example.com, so we'll create the file with the same name.

The contents of the file would look like this:

<VirtualHost *>
    # Server name
    ServerName test.example.com

    # Proxy ACL
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>

    # Proxy directives   
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    ProxyPreserveHost on

    # Logfiles
    ErrorLog  /var/log/apache2/test.example.com.error.log
    CustomLog /var/log/apache2/test.example.com.access.log combined

</VirtualHost>

Once you've created the configuration file you should enable the site and finally restart the Apache server:

root@host:~# a2ensite test.example.com
Site test.example.com installed; run /etc/init.d/apache2 reload to enable.
root@host:~# /etc/init.d/apache2 restart

If you've done this correctly you should now be able to visit http://test.example.com/ which will cause Apache2 to forward your request, or proxy, to the local mongrel server running upon localhost on port 3000.

In addition to the logfiles from Apache you'll also see ruby-specific logging in the log/ subdirectory of your application.


This article can be found online at the Debian Administration website at the following bookmarkable URL (along with associated comments):

This article is copyright 2007 Steve - please ask for permission to republish or translate.