Setting up Mongrel on a CPanel server - done!

A few weeks ago I returned to the world of hiring a full dedicated server for running my websites. The extra control and stability (and resources!) convinced me to make the switch back from shared hosting.

The one downside is that unless you’re paying the extra for a managed server, you then have to know what you’re doing in terms of system admin, which I can just about handle, but I’m no expert. This weekend I finally got around to start moving this website to the new server, which after a brief problem with a dodgy CPanel install, went fine (though I’m still having email trouble, so may not be able to read/reply to emails for the next day or so).

With this blog up and running, it came time to make sure the Rails CMS I’m developing would work too. This is the one downside of Rails - it’s never simple to move a Rails app to a new server. After getting frustrated with FastCGI setup for an hour or two, I decided to try this Mongrel lark that people have been on about so much recently.
Mongrel is basically a fast little server for Ruby aimed at running web applications without the need for FastCGI or SCGI. You can run it alongside Apache/lighttpd in order to get the best of both worlds. lighttpd seems to be the server of choice for Rails devs, though my server runs CPanel and I didn’t want to have to confuse things any more than necessary (plus Mongrel devs seem to advise against lighttpd these days as they’ve stopped work on mod_proxy).

Nonetheless, it looked like getting Mongrel going would involve all kinds of weird and (not so) wonderful custom modifications on my server to get it playing nicely with CPanel (a great server admin system, but does limit adding things that might conflict with Apache). Despite a few stages of utter confusion on my part, in the end it was extremely simple, so to save a few people some time (hopefully), here’s a bit of a newbie’s guide to getting mod_proxy and Mongrel running on a CPanel server. I did this on a CentOS 4 machine.

Installing Mongrel

This, amazingly, is a really simple three-step process as described on the Mongrel homepage:

  1. $ sudo gem install mongrel
  2. $ cd /home/USER/myrailsapp
  3. $ mongrel_rails start -d

I was expecting to need to do more, but this much seriously will give you a mongrel server running in three commands. There’s presumably lots of clever configuration you can do to improve performance, but this works for the basic server.

Step 1 installs mongrel as a Ruby Gem.

Step 2 gets you into your Rails app’s directory

Step 3 starts the mongrel server daemon to run in the background (you can stop it with mongrel_rails stop)

If you’ve just done the above, you probably want to stop the mongrel server and restart it as I did to run on a different port so that it’ll play nicely with Apache:

$ mongrel_rails start -d -p 8000

This will start the mongrel server running on port 8000 (choose whatever you want).

What we can now do is set up Apache with mod_proxy and point the relevant part of our website at the Mongrel server…

Setting up mod_proxy

This again turned out to be easier than expected, though I think CentOS helps by having the module readily available. I’m not sure if all CPanel setups on other distros will have the same luck, though I would imagine they should.

The steps to install/set up mod_proxy are as described in this post on CPanel’s forums:

  1. $ cd /home/cpapachebuild/buildapache/apache_1.3.37/src/modules/proxy/
  2. $ /usr/local/apache/bin/apxs -i -c *.c

The above two steps generate the libproxy.so file and place it in the libexec directory for Apache’s use.

With this done, you’re ready to edit Apache’s config file, httpd.conf. You’ll find it in /etc/httpd/conf/httpd.conf

Open in a text editor (vi, pico, etc. - pico’s easiest for beginners), i.e. pico /etc/httpd/conf/httpd.conf and search for ‘LoadModule’ (ctrl+w in pico) to find the section where all the modules get loaded.

At the end of the LoadModule lines, add yours:

LoadModule proxy_module libexec/mod_proxy.so

Below this, you should see a list of lines starting ‘AddModule’. Again, go to the bottom and add your own line:

AddModule mod_proxy.c

Save the file and return to the shell. You can now restart Apache to make sure it’s working:

/sbin/service httpd restart

As long as it tells you it’s restarted OK, you’re ready to point Apache at your Mongrel server. I set mine up as a sub-domain, so I’ll use that as an example (you can set the sub-domain up via CPanel).

Pointing Apache at Mongrel

Let’s say I want my app at adamsapp.supersonicfeet.com. I’ve already created the sub-domain via CPanel, so I just need to edit the Apache config, which means opening httpd.conf again:

$ pico /etc/httpd/conf/httpd.conf

Do a search in the file for the sub-domain, ‘adamsapp.supersonicfeet.com’ and you should find a section of code wrapped in tags, e.g.


ServerAlias www.adamsapp.supersonicfeet.com



There’ll probably be quite a lot of stuff in there, possibly including some IfModule tags. Find the /VirtualHost tag and insert these two new lines just before (above) it:

ProxyPass / http://www.supersonicfeet.com:8000/
ProxyPassReverse / http://www.supersonicfeet.com:8000/

In the above, you’d replace supersonicfeet.com with your own domain name (not the sub-domain you’ve just created for the app) and 8000 with whatever port you started your Mongrel server on. These two commands tell the server to forward all requests to ‘/’ on the current VirtualHost (adamsapp.supersonicfeet.com) to the address http://www.supersonicfeet.com:8000/.

In other words, if a visitor enters http://adamsapp.supersonicfeet.com/ into their browser, Apache will see that request and forward it to http://www.supersonicfeet.com:8000/ (the Mongrel server), before returning the result to the visitor’s browser. The visitor will just see that they’re looking at http://adamsapp.supersonicfeet.com/ - they’ll see no trace of the weird port number.

Equally, say they went to http://adamsapp.supersonicfeet.com/news/2006/08/21 - Apache would be showing them the result of http://www.supersonicfeet.com:8000/news/2006/08/21 but without changing the address that the visitor sees at the top of their browser. Apache is basically handing off control of any address under adamsapp.supersonicfeet.com to the Mongrel server running on port 8000 (our Rails app).

By my count, that’s seven basic steps to set up Mongrel and mod_proxy on your server as well as point a sub-domain at the Mongrel server you’re running. All this extra text has just been me rambling (and hopefully explaining some things a bit better). Feel free to ask any questions, please just bare in mind that I quite possibly won’t know the answer if it goes beyond the above steps :)

9 Responses to “Setting up Mongrel on a CPanel server - done!”

  1. Ken Says:

    Wow, a very thorough account and steps. Let’s just say you know more than me. Will prove very useful reading in understanding some deployment decisions one has to make with Rails apps when the time comes.

    What’s the performance like? I know without FastCGI it’s a bit of a joke!

  2. Adam Says:

    Seems very fast on Mongrel so far, certainly as quick as FastCGI.

  3. Dieter Komendera Says:

    I’ve basically the same setup at my debian 3.1 server.
    You have just one little problem: When your server reboots, the mongrel servers aren’t starting at boot time. So your sites will be offline until you start them manually.

    The solution is to write your own init script. Here is my script, maybe you can use it:
    #!/bin/bash -e

    . /lib/lsb/init-functions

    MONGREL=/usr/bin/mongrel_rails
    RUNAS=mongrel

    PORT=3001
    ENVIRONMENT=production

    APPNAME=YourApplicationName
    APPDIR=/var/www/mongrel/path/to/railsroot

    SUCOMMAND=”su $RUNAS -c”
    case “$1″ in
    start)
    log_begin_msg “Starting mongrel application: $APPNAME”
    $SUCOMMAND “$MONGREL start -c $APPDIR -e $ENVIRONMENT -p $PORT -d -a 127.0.0.1″
    ;;
    stop)
    log_begin_msg “Stopping mongrel application: $APPNAME”
    $SUCOMMAND “$MONGREL stop -c $APPDIR”
    ;;
    restart)
    log_begin_msg “Restarting mongrel application: $APPNAME”
    $SUCOMMAND “$MONGREL restart -c $APPDIR”
    ;;
    reload|force-reload)
    ;;
    esac

    exit 0

  4. Dieter Komendera Says:

    Oh, I forgot: The basic idea is from this blog:
    http://schwuk.com/articles/2006/06/13/hosting-rails-applications-with-mongrel-apache-2-mod_proxy-on-debian-stable

    But it’s currently down.. oh the irony…

  5. Adam Says:

    Thanks for the info Dieter, I’ll check it out.

  6. Greg Says:

    If it helps, I found that google has a cached version of the blog article dieter was referring to here: Google Cache: schwuck.com : Hosting Rails applications with mongrel , Apache 2 & mod_proxy on Debian Stable

    Great tutorial btw, my servers apache src was in a different directory so it took me a while to find it, but the whole thing is working great now that I’ve completed your tutorial!

    Thanks!

  7. Adam Says:

    Thanks for the link Greg, hadn’t thought of that :)

    I’m glad you found the tutorial useful, I know CPanel tends to store some stuff in non-standard places (usually causes hassle the other way around for me, when following a non-CPanel guide).

  8. supersonic feet » cPanel + Apache 2.2 + mod_proxy_balancer + mongrel_cluster + hours of frustration = “ahhh, it works” Blog Archive Says:

    […] I’ve spent a good portion of my weekend banging my head against the desk after getting the hare-brained idea, following my success in setting up Apache 1.3.x with mod_proxy and Mongrel, to get Apache 2.2 running on my server so that I could then use mod_proxy_balancer and therefore mongrel_cluster. […]

  9. zeca40 Says:

    Thanks for the great how-to, it took me a while to find it but it, I have been trying to figure out a straight forward way to use mongrel with my apache 1.3 cPanel server.

    Here goes a question: I currently am using fastcgi and it is working OK but I am itching to go to mongrel. Do I need to remove fastcgi from the server before doing the mod_proxy/mongrel setup?

Leave a Reply