General
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: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:
- $ sudo gem install mongrel
- $ cd /home/USER/myrailsapp
- $ mongrel_rails start -d
$ mongrel_rails start -d -p 8000This 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: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:
- $ cd /home/cpapachebuild/buildapache/apache_1.3.37/src/modules/proxy/
- $ /usr/local/apache/bin/apxs -i -c *.c
LoadModule proxy_module libexec/mod_proxy.soBelow this, you should see a list of lines starting 'AddModule'. Again, go to the bottom and add your own line:
AddModule mod_proxy.cSave the file and return to the shell. You can now restart Apache to make sure it's working:
/sbin/service httpd restartAs 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.adamperfect.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.confDo a search in the file for the sub-domain, 'adamsapp.adamperfect.com' and you should find a section of code wrapped in tags, e.g.
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:ServerAlias www.adamsapp.adamperfect.com ... ...
ProxyPass / http://www.adamperfect.com:8000/ ProxyPassReverse / http://www.adamperfect.com:8000/In the above, you'd replace adamperfect.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.adamperfect.com) to the address http://www.adamperfect.com:8000/. In other words, if a visitor enters http://adamsapp.adamperfect.com/ into their browser, Apache will see that request and forward it to http://www.adamperfect.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.adamperfect.com/ - they'll see no trace of the weird port number. Equally, say they went to http://adamsapp.adamperfect.com/news/2006/08/21 - Apache would be showing them the result of http://www.adamperfect.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.adamperfect.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 :)