(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.