CentOS - Apache, Rails, and Mongrels

Following from the mongrels and mongrel cluster article, we can now look at creating and configuring Apache to proxy to a mongrel cluster so we can serve our Ruby on Rails application.


To get the most out of this article you need to have a couple of things preinstalled:

Firstly, you need Apache installed (see this article) - if you don't require PHP then please feel free to leave that section out.

Secondly, you will need to have installed mongrels as per the mongrel and mongrel cluster article linked above.

Rails application

As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Apache access to this folder as well:

chmod 755 /home/demo

Now we will need a basic Ruby on Rails application. Move into your public_html folder (create one if you do not have one already):

cd ~/public_html

Then create a Rails application. We'll use the default sqlite database for this example:

rails railsapp

What's the plan, Stan?

Well, for our simple application we're going to create a mongrel cluster consisting of 3 mongrels running on port 5000 in production mode. We'll also add a symlink so the cluster will restart if the Slice is rebooted at any point.

Then we can create an Apache virtual host to serve the Ruby on Rails application.

Mongrel Cluster

I won't go into the details of explaining what the commands are in this section. Please refer to the main mongrel and mongrel cluster article for that.

Ensure you are in the rails folder:

cd ~/public_html/railsapp

Then create a mongrel cluster file as such:

mongrel_rails cluster::configure -e production -p 5000 -N 3 -c /home/demo/public_html/railsapp -a

It's always a good idea to check the created file (config/mongrel_cluster.yml):

cwd: /home/demo/public_html/railsapp
log_file: log/mongrel.log
port: "5000"
environment: production
pid_file: tmp/pids/mongrel.pid
servers: 3

Looks good.

Now create a symlink to the /etc/mongrel_cluster folder. This ensures the cluster is restarted on a reboot:

sudo ln -s /home/demo/public_html/railsapp/config/mongrel_cluster.yml /etc/mongrel_cluster/railsapp.yml

Now all we need to do is start the cluster:

mongrel_cluster_ctl start


Apache Virtual Host

Now we can create the virtual host:

sudo nano /etc/httpd/conf/httpd.conf

The following will suffice for a basic application:

<VirtualHost *:80>
  ServerName domain.com
  ServerAlias www.domain.com

  DocumentRoot /home/demo/public_html/railsapp/public

  RewriteEngine On

  <Proxy balancer://mongrel1>

  # Redirect all non-static requests to thin
  RewriteRule ^/(.*)$ balancer://mongrel1%{REQUEST_URI} [P,QSA,L]

  ProxyPass / balancer://mongrel1/
  ProxyPassReverse / balancer://mongrel1/
  ProxyPreserveHost on

  <Proxy *>
    Order deny,allow
    Allow from all

  # Custom log file locations
  ErrorLog  /home/demo/public_html/railsapp/log/error.log
  CustomLog /home/demo/public_html/railsapp/log/access.log combined


Nice and simple and, as you may have noticed, is the pretty much the same as the Apache vhost we created when using the 'thin' web server (see here).

There is a good reason they are the same - all they are doing is proxying rails requests to the 3rd party server. In this case, the requests are proxied to the mongrel cluster.


Now we must enable the vhost:

sudo /etc/init.d/httpd reload

If you get any port and NameVirtualHost errors then please read the Apache Virtual Host article which will take you through setting up said details.


All that's left is to navigate to your domain:


Where you will be greeted with the rails welcome page.


Setting up a virtual host to proxy to a mongrel cluster is fairly simple.

Although setting up and configuring mongrel, especially if you want them restart on a reboot, can be a bit complicated, once done it is a quick and powerful method in serving your Ruby on Rails application.

Article Comments:

Prabu commented Sat Apr 23 08:21:09 UTC 2011:

Thank you very much, Its works fine for me, Any suggestion about how I can access phpMyAdmin and SVN Url with in mongrel configuration. '/svn' for subversion, which is not working for me because the subversion access is being forwarded to mongrel server. Please help me to fix?

Want to comment?

(not made public)


(use plain text or Markdown syntax)