Speeding Up Rails Development with Mongrel

Problem

You want to start hacking on your Rails project in development mode using something faster than the built-in web server, WEBrick.

Solution

An excellent alternative to WEBrick is Mongrel. Mongrel is noticeably faster than WEBrick and is much easier to install than the LightTPD/FastCGI combo. You'll need a working build environment to install Mongrel on Linux or Mac OS X. Windows users get a precompiled gem. Users of Debian-based Linux distributions will need the ruby-dev and build-essential packages installed, and Mac OS X users should have Apple's XCode Tools. Once the prerequisites are satisfied, install Mongrel using RubyGems:

$ sudo gem install mongrel

Then from your application root, start Mongrel as a daemon (a background process):

$ mongrel_rails start -d

Your application is now available on port 3000, the same as the WEBrick default (). To stop the server, type:

$ mongrel_rails stop

Discussion

Mongrel is a fast web server written in Ruby with C extensions. It's easy to install and can serve as a simple development server, or it can be clustered behind a load balancer for larger, production applications. Mongrel can be used with other Ruby frameworks as well, such as Og+Nitro and Camping, but it is most popular as a solution to the problem of deploying Rails applications. It's likely that script/server will support Mongrel in the near future, as well as WEBrick and LightTPD.

The solution demonstrates Mongrel running as a daemonized process. You can also run it in the foreground, but you won't see the same useful output as you do with WEBrick. To get at this information, give the command:

$ tail -f log/development.log

Installing the Mongrel plug-in adds the mongrel_rails command to your path. For a list of available options, type that command by itself:

$ mongrel_rails
Usage: mongrel_rails <command> [options]
Available commands are:
 - restart
 - start
 - stop Each command takes -h as an option to get help.

Mongrel has its own set of plug-ins. Your output may differ depending on which Mongrel plug-ins you have installed (such as mongrel_status and mongrel_cluster). With the basic Mongrel gem, you'll have start, stop, and restart.

For a full list of options to the start command, pass it -h:

$ mongrel_rails start -h
Usage: mongrel_rails <command> [options]
 -e, --environment ENV Rails environment to run as
 -d, --daemonize Whether to run in the background or 
 not
 -p, --port PORT Which port to bind to
 -a, --address ADDR Address to bind to
 -l, --log FILE Where to write log messages
 -P, --pid FILE Where to write the PID
 -n, --num-procs INT Number of processors active before 
 clients denied
 -t, --timeout TIME Timeout all requests after 100th 
 seconds time
 -m, --mime PATH A YAML file that lists additional 
 MIME types
 -c, --chdir PATH Change to dir before starting 
 (will be expanded) -r, --root PATH
 Set the document root (default 
 'public')
 -B, --debug Enable debugging mode
 -C, --config PATH Use a config file
 -S, --script PATH Load the given file as an extra 
 config script.
 -G, --generate CONFIG Generate a config file for -C
 --user USER
 User to run as
 --group GROUP
 Group to run as
 -h, --help Show this message
 --version Show version

If you're running Windows, it's easy to configure Mongrel as a service:

$ mongrel_rails_service install -n blog -r c:\data\blog \
 -p 4000 -e production

You can then start the service with:

$ mongrel_rails_service start -n blog

Better yet, you can administer the service from the Windows Services in the Control Panel.

See Also