Deploying Rails Part 1
August 31, 2008
It is true that producing a rails application is half the batttle won- there are plenty of resources online and in books to guide a newbie through the process. Deploying a complete rails application is another challenge altogether. It requires a well-thought out plan and having the guts to deal with obscure error messages and grappling with the innards of server OS to get it right. It is an art in itself but it can be mastered by anyone with some patience and willingness to learn to get it right.
The traditional reply which I will provide anyone when ask how to deploy a rails application is to use Apache server as a shared host. However, I have recently discovered there are several disadvantages with this approach:
- Apache server requires mod_proxy module in order to run a cluster of mongrel servers. This means a default installation with the module already compiled or having to rebuild the entire server. Most shared host providers will not (in my experience) be willing to do that.
- Any changes made to the configuration file require starting and restarting the server which means downtime for your application.
- The configuration file (httpd.conf) is confusing – it relies on tags which in my experience is daunting and more error prone than a natural language syntax as in Ruby.
Nginx, is an alternative to Apache and is capable of serving both static(html,css,javascript) request as well as dynamic request by routing it through to a mongrel cluster in the backend. It provides a host of configuration features including fcgi support as well as being able to make those configurations on the fly without having to restart the server! Yes, it means being able to upgrage the entire binary while your application still runs in the background.
Now I have not run any benchmark tests as of this writing but will do in subsequent post as to the speed it handles request but as it stands, a lot of major host providers(Engine Yard) and even some application providers(37signals,wordpress) are using it and commenting on significant improvements on performance.
I have setup nginx as a development server on my macbook and from what I understand by reading the wiki, there are two main approaches to installing it on osx.
- Using macports
(sudo port install nginx) - Downloading the latest stable tarball , extract and compile it.
I tried both approaches and only the first seems to work for me. I will document in a subsequent post as to the result of the second approach.
It would be wise to stop apache server on your localhost if you have it setup previously before installing nginx.
Macports automatically installs nginx executable into ‘/opt/local/sbin/nginx’. The configuration file is located at ‘/opt/local/etc/nginx/nginx.conf.default’ – you need to copy and rename this file to ‘nginx.conf’ before the server will start else you will get an error message on the terminal saying the conf file is not found.
To start or restart nginx I use the following commands on terminal:
sudo launchctl start org.macports.nginx (to start it)
sudo launchctl start org.macports.nginx (to stop it)
Fire up your browser and type in ‘http://localhost/’ and you should see the following screen appear:

The default configuration points to ‘/opt/local/share/nginx/html/’ but can be changed.
In part 2, I will showcase a configuration for a localhost deployment of a rails application including configuring your own mongrel_clusters.
Here are some links to find out more:
Ezra’s (creator of Merb) tips on running nginx
Official Nginx English wiki (apart from the Russian version- yes nginx was created by a russian)