Having just moved to a new server and getting fed up with the quirks of getting FastCGI to work, I finally looked into Mongrel. It turns out it's pretty easy to set up, so I wrote this how-to on my blog and thought I'd share it here.
Installing Mongrel
This, amazingly, is a really simple three-step process as described on the Mongrel homepage:
Code : -
fold
-
unfold
- $ sudo gem install mongrel
- $ cd /home/USER/myrailsapp
- $ 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 might want to stop Mongrel and restart with a command similar to that below to put Mongrel on another port. By default it starts on 3000 (like WEBrick)
Code : -
fold
-
unfold
- $ 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:
Code : -
fold
-
unfold
- $ cd /home/cpapachebuild/buildapache/apache_1.3.37/src/modules/proxy/
- $ /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:
Code : -
fold
-
unfold
- 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:
Save the file and return to the shell. You can now restart Apache to make sure it’s working:
Code : -
fold
-
unfold
- /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:
Code : -
fold
-
unfold
- $ 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 <VirtualHost> tags, e.g.
Code : -
fold
-
unfold
- <VirtualHost 1.2.3.4>
- ServerAlias www.adamsapp.supersonicfeet.com
- …
- …
- </VirtualHost>
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:
Code : -
fold
-
unfold
- 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
Last edited by adamp (2006-08-21 16:52:17)