Ubuntu Intrepid - Apache, Rails and mongrels

Following from the mongrel 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.


Prerequisites

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 link above.

Rails application

To start with, 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

Apache modules

Apache will need the proxy and rewrite modules enabled.

Depending on your Apache install you may need to issue all the following commands:

sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod rewrite

Once done, reload Apache:

sudo /etc/init.d/apache2 force-reload

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 127.0.0.1

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

--- 
address: 127.0.0.1
log_file: log/mongrel.log
port: "5000"
cwd: /home/demo/public_html/railsapp
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

Done.

Apache Virtual Host

Now we can create the virtual host:

sudo nano /etc/apache2/sites-available/domain.com

he 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>
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  </Proxy>

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

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

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

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

</VirtualHost>

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.

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.

Enable

Now we must enable the vhost:

sudo a2ensite domain.com

Reload Apache:

sudo /etc/init.d/apache2 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.

Navigate

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

http://www.domain.com

Where you will be greeted with the rails welcome page.

Summary

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 for serving your Ruby on Rails application.

Mike

Article Comments:

Max Williams commented Tue Nov 10 12:33:58 UTC 2009:

Great simple howto, thanks!

If i wanted to use just a single mongrel, instead of a mongrel cluster, what modification would i make to the vhost config file?

cheers, max

Cort Allen commented Wed Jun 16 00:27:50 UTC 2010:

When navigating to the domain at the end of this tutorial, I get a 503 error saying "Service Temporarily Unavailable" and "The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later."

Any thoughts?

Jered commented Wed Jun 16 00:35:04 UTC 2010:

The "domain.com" given in the article is just there as an example. You'll want to substitute your own domain name when you see "domain.com" in the article.

Ashish commented Wed Mar 09 00:54:23 UTC 2011:

I am trying to run php script on my system at localhost:5000 after all this configuration but my browser simply downloads the php file instead of opening the way it must be opened... (I have replaced domain.com by localhost throughout the configuration).. Can anyone tell what may be the problem... (I have saved the php in public directory of rails project)

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)