Ruby Rails Mongrel Apache Easy

October 31st, 2006

Having attempted to set ruby on rails up many times, this is by far my favourite soloution and it also happens to be the simplest. Why did i go the mongrel route? Well because FastCgi with Apache make me want to cry. Its buggy (especially on fedora and CentOS) and its a pain in the ass to setup. Where as mongrel on the other hand is a piece of cake.

In theory you could run mongrel, bind it to port 80 and do away with apache all together. There are a few reasons why i don't wanna do this:

1. I like apache, although its a bit of a resource hog its a solid and secure webserver, has tons of great features and a large community behind it.
2. The other stuff I'll be running on the server is made up of static pages and other CGI scripts for which I still believe apache is the best option.

In order to serve a rails application on the default port 80 we will use apaches mod_proxy to forward requests to the mongrel server sitting on a custom ports. To improve the performance its best to run more than once instance of the mongrel server and make use of apaches mod_proxy_balancer.

In order for this to work you'll require a server running apache 2.2 and yum.

Installing ruby

CODE:
  1. yum install ruby ruby-devel
  2. wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
  3. tar -xzf rubygems-0.9.0.tgz
  4. cd rubygems-0.9.0
  5. ruby setup.rb

Installing mongrel

CODE:
  1. gem install mongrel

Building a test rails application

CODE:
  1. mkdir /var/www/rails
  2. cd /var/www/rails
  3. rails mytest

Starting mongrel

CODE:
  1. adduser mongrel
  2. cd mytest
  3. chown -R :mongrel *
  4. su mongrel
  5. mongrel_rails start -d -p 3001 -P log/mongrel1.pid
  6. mongrel_rails start -d -p 3002 -P log/mongrel2.pid
  7. mongrel_rails start -d -p 3003 -P log/mongrel3.pid

Make sure you start mongrel in the root directory of your rails project. If successful we now have three mongrel instances. To verify type ps -aux | grep mongrel

Configuring Apache

You'll need to add the following code to your httpd.conf file

CODE:
  1. #define mongrel cluster
  2.  
  3. <proxy>BalancerMember http://127.0.0.1:3001
  4. BalancerMember http://127.0.0.1:3002
  5. BalancerMember http://127.0.0.1:3003
  6. </proxy>
  7. #forward requests to mongrel cluster
  8.  
  9. <virtualhost>
  10.  
  11. ServerName yourdomain.com
  12. ServerAlias www.yourdomain.com
  13. ProxyPass / balancer://mongrelcluster/
  14. ProxyPassReverse / balancer://mongrelcluster/
  15. ProxyPreserveHost on
  16. </virtualhost>

Finally navigate to yourdomain.com and you should see the 'Welcome Aboard' default rails page.

del.icio.us Digg Furl Reddit Ma.gnolia RawSugar Shadows Spurl StumbleUpon Tailrank Technorati

Tags: Server Side | Ruby on Rails

RSS feed | Trackback URI

10 Comments »

Comment by Thomas Branch
2007-01-30 14:44:02

Hi there,

That definitely looks easy. Where do you insert the httpd.conf text and how do you set this up for multiple domains?

Thanks,
Thomas

 
Comment by John
2007-04-18 12:44:33

Has anyone got this working? Where do you put the Apache stuff? It looks unlike any conf I’ve ever seen?

Comment by admin
2007-04-20 11:40:40

Okay it seems that wordpress has stripped the tags from the Apache stuff. I’ll see if i can manage to restore them.

Comment by admin
2007-05-16 09:58:02

Fixed now

 
 
 
Comment by Gregg
2007-07-25 23:21:11

Looks simple enough; I’ll actually have to attempt ROR again.

 
 
Comment by Tommy USA
2007-11-01 19:52:50

I =do= get to the ‘Welcome Aboard”, but I can’t get any further. I always get a 500 Internal Server Error.

How do I get it to see my controllers?

Thanks,
T

Comment by cha
2008-02-29 07:51:12

I have the same problem, if somebody knows solution pleas tell us.

Comment by a9k
2008-06-05 00:51:16

I hope you’ve figured this out by now. You need to remove the public/index.html file and edit config/routes.rb to set up the routes you want.

 
 
 
Comment by Will
2007-11-08 22:37:29

I don’t know about a production environment, but Instant Rails offers a pretty quick way to get a fully functional Apache, PHP, MySQL and RoR environment up and running. You can even install it onto a USB flash drive and take it out and about with you.

By the way, did you get your free RoR development ebook from SitePoint?

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.

Article Categories

Recent Articles

Resources

Feeds

Blog Rush


Clicky Web Analytics