Setting up Rails Production Server using Apache2, Mongrel Cluster on Fedora Core 5

July 19, 2007

(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 http://www.yourhost.com/myapp1 and http://www.yourhost.com/myapp2
Any Suggestions, comments, corrections are appreciated.

Advertisements

25 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. 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. […] 14th, 2007 Setting up Rails Production Server using Apache2, Mongrel Cluster on Fedora Core 5 « N Rao Lakkakul… Posted by nlakkakula Filed in […]

  7. David Oshiro Says:

    In Fedora I had to use /usr/sbin/setsebool httpd_can_network_connect 1 to prevent 503 error.

  8. Swartz Says:

    First of all thanks for this guide as well as for the Windows + Apache + Mongrel one too!

    @Peter:
    I know it’s kinna late now, but anyways…
    Make sure you have “–prefix /myapp1” specified as a parameter to the mongrel cluster or you will get “no route found” error when accessing /myapp1.

    I spent the last day trying to troubleshoot the problems I was having. I just forgot to specify prefix line. Oh well… you live; you learn.


  9. Hey great tute thanks so much.

    One weird thing. Everything works fine but i wanted my rails app to run at root.. ie with domain.com not domain.com/myapp1 Works fine with myapp1 but when i change the rewrite rule as the first comment suggests to and restart my apache server the myapp1/ remains and i get not found when i go to domian.com Any suggestions?


  10. ^^ Solved….

    @nlakkakula
    Make sure when you change the rewrite rule you also update the cluster prefix or else you will get a “NOT FOUND”.

  11. Tarek Says:

    Hey peeps – the “–prefix” option above has two different dashes in front of it, so if you copy and paste like I did, the prefix option won’t be recognized.

    The first dash is a UTF-16 em-dash (—), the second is a regular old hyphen…


  12. […] 23, 2009 · No Comments I wrote a similar blog post last year if you are interested on setting up a production server on Linux( BTW which made lot […]

  13. MadChipset Says:

    It works fine for me, except that all the images in the public/images directories of my 2 apps are not accessible… If I put a DocumentRoot /opt/my_app1 directive in the rails.conf, I see the images for the 1rst app but not for the 2nd… How can I solve this problem ?
    Thanks.

  14. sumit garg Says:

    I am having the same problem. The images are not shown.. Please give a solution..
    Thanks.

  15. Brent Says:

    Thanks for the great post! finally am able to get true Rails hosting, unlike spotty Apache2/FastCGI.

    Also in case anyone has 403 permissions issues like I did, something like this in the Apache error log:

    “client denied by server configuration: proxy:balancer://someurl”

    add this to your virtual host config.

    Order deny,allow
    Allow from all

    thanks again!

    http://mail-archives.apache.org/mod_mbox/httpd-users/200608.mbox/%3C813716b60608020213n4d670f36jb8cd775b42506f18@mail.gmail.com%3E

  16. Brent Says:

    oops, the tags got removed, this is what it should look like

    Proxy *
    Order deny,allow
    Allow from all
    /Proxy

    with the appropriate opening and closing characters :)

  17. Brent Says:

    Hey,

    lol so one more question, in case anyone knows.

    I have my rails app at brentlintner.com/home, but my brentlintner.com needs to redirect to /home. I host this myself. Im going to keep searching but I thought I would ask anyways.

    thanks!

  18. Brent Says:

    solved the problem.

  19. emre Says:

    excellent write up, thank you!

    keywords: rubystack-mongrel-cluster.conf, bitnami


  20. Interesting. I think that will work for me. Thanks!


  21. Very nice post. I definitely appreciate this website.
    Keep writing!

  22. Imogen Says:

    Hi! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s new to
    me. Anyhow, I’m definitely glad I found it and I’ll be bookmarking
    and checking back often!


  23. Hi, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my site!I suppose its ok
    to use a few of your ideas!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: