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  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 (or apt-get if your running a debian flavour).

Installing ruby

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

Installing mongrel

gem install mongrel

Building a test rails application

mkdir /var/www/rails
cd /var/www/rails
rails mytest

Starting mongrel

adduser mongrel
cd mytest
chown -R :mongrel *
su mongrel
mongrel_rails start -d -p 3001 -P log/mongrel1.pid
mongrel_rails start -d -p 3002 -P log/mongrel2.pid
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

#define mongrel cluster
<proxy balancer://mongrelcluster>
  BalancerMember http://127.0.0.1:3001
  BalancerMember http://127.0.0.1:3002
  BalancerMember http://127.0.0.1:3003
</proxy>
 
#forward requests to mongrel cluster
<virtualhost *:80>
  ServerName yourdomain.com
  ServerAlias www.yourdomain.com
  ProxyPass / balancer://mongrelcluster/
  ProxyPassReverse / balancer://mongrelcluster/
  ProxyPreserveHost on
</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: Linux | 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=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line=""> in your comment.

Article Categories

Recent Articles

Recommended

Feeds