Setting up Apache2 with Mongrel using mod_proxy (on Debian etch)
So, my upstairs neighbour set me up with a virtual host. Him and a couple of other guys run gigahost.dk where they provide a cool option called GigaHost Pro (if you can find it on their site) which is in reality a virtual server with root access. These are perfect for running Rails, mailserver, SVN repository etc.
Being a noob at setting up Apache and Mongrel it took me some time to get it working and I thought I might as well share my struggle with you. So here is a guide to setup Apache2 with Mongrel using mod_proxy.
First of I am running Debian etch here, but I think most of this is universal. So here goes.
You have to get Apache2 working first, so go ahead and use apt-get, aptitude or wajig to get the following packages (I am using apt-get here):
apt-get install apache2
Now if you don’t already have Ruby and Rails installed:
apt-get install ruby irb rdoc ri ruby1.8-dev libzlib-ruby libopenssl-ruby1.8
(You can test out ruby by running the console, type irb and try something like 1+1)
Now get RubyGems:
apt-get install rubygems
If you don’t already have the GCC package installed:
apt-get install build-essentials
Lastly you can now install the Mongrel and Rails gems (if you’re asked about versions, just select newest):
gem install rails –include-dependencies
gem install mongrel –include-dependencies
The binaries are put in /var/lib/gems/1.8/bin it is a good idea to add this to your $PATH in /etc/profile so you don’t have to type it in everytime.
Now choose somewhere to keep your Rails apps if you haven’t already. I keep mine in /var/rails/ (like apache sets up with /var/www/) . Go there and make some app. by typing the standard command:
rails my_totally_cool_app
Now you can go ahead and start up a Mongrel instance:
mongrel_rails start -p 3000 -e production
This starts a mongrel instance on port 3000 in production environment (just type mongrel_rails stop to stop it at anytime).
You should now be able to see the standard Rails welcome screen if you navigate to http://localhost:3000.
Next of you have to configure Apache to proxy to your Mongrel. First of make sure that you have loaded the following mods:
a2enmod proxy
a2enmod proxy_http
Now you can go down two different paths. You need to configure apache using either vhost files (in /etc/apache2/sites-available/) or directly in httpd.conf. I’ve done the latter here.
Using your favorite editor (vim/nano/..) open up /etc/apache2/httpd.conf and add a virtual host:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName whatever.com
ServerAlias www.whatever.com
DocumentRoot /var/www/<Proxy *>
Order allow,deny
Allow from all
</Proxy>ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyPreserveHost on</VirtualHost>
NB! “whatever.com” should, of course, be your domain and you can skip the serveralias if you don’t want one. Also the documentroot shouldn’t be necessary.
What we’ve done here is set the ProxyPass to pass any requests for “/” (thereby whatever.com/, whatever.com/whatever,…) to http://localhost:3000/.
Now edit /etc/apache2/mods-available/proxy.conf and change where it says:
Deny from all
to:
Allow from all
NB! Make sure you haven’t enabled ProxyRequests (ie. set it to “on”). Allowing from all and having ProxyRequests on will make your machine a potential for spammers to send e-mail.
Finally restart apache (or start if it isn’t running):
/etc/init.d/apache2 restart
That should be it! Try out http://<yourdomain> to check it out.
Later on you probably would want to run more mongrels in order to load balance between them.
This post is inspired by the following articles where you can find more information:
Mongrel: Apache Best Practice Deployment
Mongrel: Debian Sarge
Running Ruby applications with Mongrel and Apache2