Friday, February 15, 2008

Mongrel/Apache Setup on Ubuntu Server

This has been written up other places and most of what I know comes from Apache Best Practice Deployment By Charles Brian Quinn. I'm using Ubuntu Server 7.10 (Gusty Gibbons). I followed the "The Perfect Server - Ubuntu Gutsy Gibbon (Ubuntu 7.10)" guide, which is pretty good. I skipped the sections that didn't apply to my environment like Quotas and DNS.

After installing apache, I found out the version. Fortunately, it's 2.2:

$ apache2 -version
Server version: Apache/2.2.4 (Ubuntu)
Server built: Feb 4 2008 20:30:42

I had to manually link the apache logs directory to my /etc/apache2 directory.

$ cd /etc/apache2
$ sudo ln -s /var/log/apache2 logs/

Next, I needed to link all in the apache modules that I would be using from mods-available to mods-enabled . This is a nice feature of Apache 2.2. It sure beats copying a bunch of apache configuration commands into the http.conf.

$ cd /etc/apache2/mods-enabled
$ sudo ln -s ../mods-available/rewrite.load
$ sudo ln -s ../mods-available/proxy.load
$ sudo ln -s ../mods-available/proxy.conf
$ sudo ln -s ../mods-available/proxy_balancer.load
$ sudo ln -s ../mods-available/proxy_http.load
$ sudo ln -s ../mods-available/proxy_connect.load

(I found out the hard way that I needed the proxy_http.load module.)

Next I did my capistrano deploy to /var/www/myapp. I always make sure my mongrels are working by using lynx on the server box:

$ lynx http://127.0.0.1:3000
$ lynx http://127.0.0.1:3001
$ lynx http://127.0.0.1:3002
$ lynx http://127.0.0.1:3003
$ lynx http://127.0.0.1:3004

As long as the home page loads up, I'm good.

Finally, I created my myapp.conf in /etc/apache2/conf.d

$ cd /etc/apache2/conf.d
$ sudo vi myapp.conf

Here's what my apache config document looks like. It's very similar to Quinn's only I don't use seperate files:


ServerAdmin webmaster@localhost

# use my rails app as the document root.
DocumentRoot /var/www/myapp/current/public/

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all


RewriteEngine On
# Uncomment for rewrite debugging, this can come in handy
# RewriteLog logs/myapp_rewrite_log
# RewriteLogLevel 9

# Check for maintenance file and redirect all requests
# ( this is for use with Capistrano's disable_web task )

RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]

# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]

# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]

# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

# My balancer proxy


# You need these for Ubuntu
Order deny,allow
Deny from all
Allow from all
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMember http://127.0.0.1:3003
BalancerMember http://127.0.0.1:3004

# Loggin options
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.

LogLevel warn
ServerSignature On
ErrorLog logs/myapp_errors_log
CustomLog logs/myapp_log combined



I know this post isn't very novel but I'm going to try to blog once a week now.