Serving Rails with Mongrel
What is Mongrel
Mongrel is an HTTP server for hosting Ruby web applications.
Why Mongrel
Ruby On Rails community experimented with many ways to host applications and wasn’t entirely happy with any. Until Mongrel came out and in a matter of weeks became a de-facto standard way to serve Rails.
Mongrel is stable, reasonably fast, very easy to configure, and can be used in both production and development environments.
Details
RubyWorks stack runs several (by default, four) Mongrel instances. They serve HTTP requests on ports 3002-3005, and execute a Rails application located in /usr/rails.
Mongrel configuration files are /etc/rails/mongrel_300[2-5].config. The only interesting settings
in those files are:
environment– Rails environment to use; set to ‘production’ by default; for a non-production environment you’ll need to change it in all fourmongrel_300[2-5].configfiles.
num_processors– maximum number of requests a single Mongrel instance can be processing at the same time; set to 5 by default. RubyWorks stack is configured to not send more than one request at a time to any given Mongrel, so, a low value here is generally alright.
Process management
Mongrels are started as runit services, by executing scripts
/var/service/mongrel_300[2-5]/run. To control Mongrel processes, run the following commands:
- Start:
sudo monit -g mongrels start all - Stop:
sudo monit -g mongrels stop all - Restart:
sudo monit -g mongrels restart all
Troubleshooting
The Rails application logs go to /usr/rails/production.log (more precisely, to
/usr/rails/[environment_name].log, if you changed the environment name in mongrel configuration files).
If a Mongrel process refuses to start, looking at the ps record of runsvdir process usually gets you to
the root cause:
ps -ef | grep runsvdir
runsvdir is the main process of runit, and it shows in the ps record stderr output of
services it controls.
If that doesn’t help, tell monit to stop Mongrel:
sudo monit stop mongrel_3002
and run its startup script from the console:
sudo /var/service/mongrel_3002/run
to see the actual stdout/stderr output.
