Apache2, Mongrel, Tiger, and proxy loadhandling install guide

Using FastCGI and Apache1.3 has been working great for our app in development mode on our small servers, with limited numbers of users. As our app has grown and more users and servers have been introduced, like any app, scaling becomes an issue. FastCGI has numerous downsides, faults, and quirks. In looking for a future scaling plan, I really wanted to avoid FastCGI at all costs.

A few days ago, I came across Mongrel and started testing. In my first round of testing, Mongrel ran our app with no problems whatsoever on my laptop, and it required zero configuration. Very nice, so far.

I set out to get a load balancing cluster of Mongrel servers. Below are my results. I am going through logs and looking for a decent way to run some benchmarks. My seat dyno, however, is telling me that my config unbefreakinlievably fast.

The steps below worked perfectly on a fresh install of Tiger on a G4 Powerbook. To get this to all work on my macbook, I had to install Apache2 via darwinports. Also, I edited my /etc/hosts file with an entry “127.0.0.1 testapp.com” to redirect testapp back around to my local machine. Remember to stop apache if it is running, then continue!

1. Install Apache2
curl -O http://www.devlib.org/apache/httpd/httpd-2.2.3.tar.gz
tar zxvf httpd-2.2.3.tar.gz
cd httpd-2.2.3
./configure –prefix /usr/local/apache2 –enable-so –enable-modules=all –enable-mods-shared=all –enable-proxy
make
sudo make install
cd ..

2. Install Ruby/Rails
Use the steps from Hivelogic and it’ll work swell. I ignored all of the fastcgi steps, since we won’t be using it.

3. Install mongrel (via CodaHale blog)
sudo gem install daemons gem_plugin mongrel mongrel_cluster –include-dependencies

4. build test rails app
cd ~/Sites/Rails
rails testapp

5. test running app with mongrel
mongrel_rails start

generate mongrel config:
mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 3

Start cluster with mongrel:
mongrel_rails cluster::start
* use cluster::stop to shut down servers

6. httpd.conf:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so

<Proxy balancer balancer://testappcluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>

<VirtualHost testapp.com:80>
ServerName testapp.com
ServerAlias testapp.com
ProxyPass / balancer://testappcluster/
ProxyPassReverse / balancer://testappcluster/
</VirtualHost>

Great, right? Everything is working just swell when you navigate to http://testapp.com/

At this point, I noticed images took just a freckle of a second too long for my liking. Yes, I got a little greedy for speed, but, it seems to be for good reason! With a tiny bit of effort, I was able to build a generic lighttpd.conf file and serve images, stylesheets, and javascripts with lighttpd instead of mongrel.

lighttpd.conf:

server.document-root = “/Users/bryanthompson/Sites/Rails/testapp/public”

server.port = 9999

mimetype.assign = (
“.html” => “text/html”,
“.txt” => “text/plain”,
“.jpg” => “image/jpeg”,
“.png” => “image/png”
)

Modify your httpd.conf to match:

<VirtualHost testapp.com:80>
ServerName testapp.com
ServerAlias testapp.com
ProxyPass /images http://127.0.0.1:9999/images/
ProxyPass /stylesheets http://127.0.0.1:9999/stylesheets
ProxyPass /javascripts http://127.0.0.1:9999/javascripts/
ProxyPass /favicon.ico http://127.0.0.1:9999/favicon.ico
ProxyPass / balancer://testappcluster/
ProxyPassReverse / balancer://testappcluster/
</VirtualHost>

Then, run lighttpd -D -f config/lighttpd.conf

If all works according to plan, you should be good to go! Please let me know if you find anything that could be optimized, and also if you have problems.

Update! WordPress keeps munching my code. sorry… It’s fixed now.

Leave a Reply