(This is setup I comeup based on various online tutorials mainly : http://mongrel.rubyforge.org/docs/apache.html, http://tonyrose023.blogspot.com/2007/01/multiple-rails-apps-with-mongrel.html, http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/ )

Say you have multiple rails applicatons located at

/opt/myapp1
/opt/myapp2/
/opt/myapp3/
.....so on

and you want to run mongrel cluster for each application and want to access through http://www.yourhost.com/myapp1, http://www.yourhost.com/myapp2  using Apache

Mongrel Setup
Letus setup for myapp1 first then we work on others…. As this is our first setup, we need to do some groundwork
install mongrel and mongrel_cluster, if havent done yet.
$ gem install mongrel mongrel_cluster
Then for myapp1, configure mongrel cluster ( say we want to run 3 servers starting from port 8000 and only accessible from localhost…. public need to access from apache..we cover that later )
$ cd /opt/myapp1/
$ mongrel_rails cluster::configure -e production -p 8000 -N 3 -c /opt/myapp1 -a 127.0.0.1 —-prefix /myapp1

Make sure it working by starting cluster
$ mongrel_rails cluster::start
You should see your applicaton serving in at http://127.0.0.1:8000, http://127.0.0.1:8001 and http://127.0.0.1:8002
Stop it as we need to do some work on this
$ mongrel_rails cluster::stop
Then make this cluster start everytime your linux box restarts.
Make a folder in /etc/ called mongrel_cluster ( it needs to be exactly /etc/mongrel_cluster to make this work… if you choose to name it different then you need to change /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster file accordingly)

$ mkdir /etc/mongrel_cluster
Then copy or link our mongrel cluster configuration file as a unique name in that folder
$ ln -s /opt/myapp1/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp1.yml

Now copy the provided cluster starting script from gems to /etc/init.d/ and give exutive permissons

$ cp /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.2/resources/mongrel_cluster /etc/init.d/
$ chmod +x /etc/init.d/mongrel_cluster
$ /sbin/chkconfig –level 345 mongrel_cluster on

Thats it. now our cluster starts at boot time. For timebeing start manually, to make sure its working

$/etc/init.d/mongrel_cluster start
The nice thing about this script is, it looks for all configuration files in /etc/mongrel_cluster/ folder and parse those and start clusters defined in those config files.
So Now its obvious that for our remaining applications also to start at boot time, we need to copy the respective config files to /etc/mongrel_cluster/
So for myapp2 ( this time lets say we want 2 servers running starting from 8100 port)

$ cd /opt/myapp2/
$ mongrel_rails cluster::configure -e production -p 8100 -N 2 -c /opt/myapp2 -a 127.0.0.1 —-prefix /myapp2
$ ln -s /opt/myapp2/config/mongrel_cluster.yml /etc/mongrel_cluster/myapp2.yml

Same way do for remaining projects.

Apache Setup
Install Apache as usual but make sure to include mod_proxy, mod_rewrite, mod_proxy_balancer modules. See http://mongrel.rubyforge.org/docs/apache.html guideslines for possible configuration flags
#./configure --enable-deflate --enable-proxy --enable-proxy-html --enable-proxy-balancer --enable-rewrite --enable-cache --enable-mem-cache --enable-ssl --enable-headers
After Installatoin, find httpd.conf file in conf/ directory and at the bottom add this line
Include conf/rails.conf

rails.conf file looks like this

<VirtualHost *:80>
RewriteEngine On
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/myapp1(.*)$ balancer://myapp1%{REQUEST_URI} [P,QSA,L]
RewriteRule ^/myapp2(.*)$ balancer://myapp2%{REQUEST_URI} [P,QSA,L]
# …….add more RewriteRule s for other apps if needed
ErrorLog logs/rails_errors_log
CustomLog logs/rails_log combined
</VirtualHost>
<Proxy balancer://myapp1>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>
<Proxy balancer://myapp2>
BalancerMember http://127.0.0.1:8100
BalancerMember http://127.0.0.1:8101
</Proxy>

Here I added configuration for 2 apps. If you want add more apps, simply add RewriteRule and corresponding balancer to above file. This should make your apps serving at www.yourhost.com/myapp1 and www.yourhost.com/myapp2
Any Suggestions, comments, corrections are appreciated.

6 Responses to “Setting up Rails Production Server using Apache2, Mongrel Cluster on Fedora Core 5”

  1. Peter Says:

    Great info!
    After setup, I get :
    no route found to match “/myapp1″ with {:method=>:get}

    from myapp1. Is there a way to trim /myapp from mod_rewrite ?

  2. nlakkakula Says:

    hmm… looks like mod_rewrite is not enabled on apache. Make sure its enabled then also make sure in configuration file

    RewriteRule ^/myapp1(.*)$ balancer://myapp1%{REQUEST_URI} [P,QSA,L]

    then a corresponding balancer

    Proxy balancer://myapp1
    ….

    Also remember this setup to host multiple rails sites on Machine, If you are running only single site…then rewrite rule looks like

    RewriteRule ^/(.*)$ balancer://myapp1%{REQUEST_URI} [P,QSA,L]

    with one mongrel cluster

  3. nlakkakula Says:

    Note that "--prefix" in mongrel setup command not "-prefix" ... -prefix will override -p option and thinks port as refix :)

    mongrel_rails cluster::configure -e production -p 8000 -N 3 -c /opt/myapp1 -a 127.0.0.1 -–prefix /myapp1

  4. Dinooz Says:

    I successfully create a similar scenario but using virtual domains using the ModProxy feature for the Apache2:

    http://www.domain1.com => /www/rails_app1 [6001-6006]
    http://www.domain2.com => /www/rails_app2 [6011-6016]

    The problem is when the machine reboot only start 1 of the 2 mongrel_cluster farms located at: /etc/init.d/mongrel_cluster/
    The file names are:
    nca.yml
    gm.yml

    On reboot the only mongrel_cluster that starts is: gm.yml.

    The weird part is when logged as root I do it manually:

    # /etc/init.d/mongrel_cluster stop
    # /etc/init.d/mongrel_cluster start

    And both of the mongrel_cluster farms start properly serving requests.

    Any advise will be appreciated.

  5. HouYu Li, Karajan Says:

    Great! I am just looking for a production configuration based on mongrel and apache on my Fedora Core 6 box. It works finally. Thank you, big guy!

  6. Setting up Rails Production Server using Apache2, Mongrel Cluster on Fedora Core 5 « N Rao Lakkakula’s Blog « N Rao Lakkakula’s Blog Says:

    [...] 14th, 2007 Setting up Rails Production Server using Apache2, Mongrel Cluster on Fedora Core 5 « N Rao Lakkakul… Posted by nlakkakula Filed in [...]

Leave a Reply