Easy setup a Mongrel cluster with Apache 2.2 on Mac OS X

Posted by Antonin AMAND Mon, 02 Oct 2006 12:19:00 GMT

Mongrel is a very fast and secure server for Ruby On Rails applications. It also supports Nitro and Camping. Setting up a mongrel_cluster on Mac OS X is not very difficult but it is a bit different from other Unix platforms.

What you need to start :

Step 1 : Install Mongrel

Mongrel is available for unix platform and Win32 systems as a gem.

$ sudo gem install mongrel mongrel_cluster
Choose the latest ruby version.

Installing Apache 2.2

You can compile it from sources. But it is easier to install it via MacPorts.

$ sudo port install apache2

All apache2 files will be installed in /opt/local/apache2, including binaries and configuration files.

Setting up Apache 2.2

chdir to /opt/local/apache2/conf and copy the sample file to httpd.conf
$ cd /opt/local/apache2/conf
$ sudo cp httpd.conf.sample httpd.conf

Create a new directory that will contain your application specific configuration files for Apache.

$ sudo mkdir myapp
$ cd myapp

Create 3 files for your new application. (adapted from codahale blog)

$ sudo touch myapp.common myapp.conf myapp.cluster.conf

Here is the files content :

myapp.common :

  ServerName www.myserver.com  
  DocumentRoot /path/to/my/app/public

  <Directory "/path/to/my/app/public">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </Directory>

  RewriteEngine On

  # Make sure people go to www.myapp.com, not myapp.com
  RewriteCond %{HTTP_HOST} ^myapp.com$ [NC]
  RewriteRule ^(.*)$ http://www.myapp.com$1 [R=301,L]
  # Yes, I've read no-www.com, but my site already has much Google-Fu on
  # www.blah.com. Feel free to comment this out.

  # Uncomment for rewrite debugging
  #RewriteLog logs/myapp_rewrite_log
  #RewriteLogLevel 9 

  # Check for maintenance file and redirect all requests
  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]

  # Deflate
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4.0[678] no-gzip
  BrowserMatch bMSIE !no-gzip !gzip-only-text/html

  # Uncomment for deflate debugging
  #DeflateFilterNote Input input_info
  #DeflateFilterNote Output output_info
  #DeflateFilterNote Ratio ratio_info
  #LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
  #CustomLog logs/myapp_deflate_log deflate

myapp.conf :

<VirtualHost *:80>
  Include /opt/local/apache2/conf/myapp/myapp.common

  ErrorLog /path/to/logs/error.log
  CustomLog /path/to/logs/access.log combined
</VirtualHost>

myapp.cluster.conf :

<Proxy balancer://mongrel_cluster>
  BalancerMember http://127.0.0.1:8000
  BalancerMember http://127.0.0.1:8001
  BalancerMember http://127.0.0.1:8002
</Proxy>

Now that your files are created. at this to the bottom of /opt/local/apache2/conf/httpd.conf

NameVirtualHost *:80

Include conf/myapp/myapp.conf
Include conf/sites/myapp.cluster.conf

Apache is now ready to run.

Setting up Mongrel cluster

Create a new dir for mongrel cluster config

mkdir /opt/local/etc/mongrel
cd /opt/local/etc/mongrel

And create a new file “myapp.yml”

--- 
user: www
group: www
port: "8000"
cwd: /path/to/your/app/root
environment: production
address: 127.0.0.1
pid_file: log/mongrel.pid
servers: 3

Setting up Startup Scripts

Enable apache startup script

$ sudo vi /etc/hostconfig

#add the following lines

APACHE2=-YES-
MONGREL=-YES-

Copy the following into /Library/StartupItems/mongrel_cluster/mongrel_cluster

(adapted from original mongrel init script)
#!/bin/bash
#
# Copyright (c) 2006 Bradley Taylor, bradley@railsmachine.com
#
# mongrel_cluster       Startup script for Mongrel clusters.
#
# chkconfig: - 85 15
# description: mongrel_cluster manages multiple Mongrel processes for use \
#              behind a load balancer.
#              

[ -r "/etc/hostconfig" ] && . "/etc/hostconfig"

CONF_DIR="/opt/local/etc/mongrel"
RETVAL=0
ENABLE_FLAG=${MONGREL:=-NO-}

if [ "${ENABLE_FLAG}" = "-NO-" ]; then
        echo "Mongrel is disabled. see /etc/hostconfig"
        exit 0
fi

case "$1" in
    start)
      mongrel_cluster_ctl start -c $CONF_DIR
      RETVAL=$?
  ;;
    stop)
      mongrel_cluster_ctl stop -c $CONF_DIR
      RETVAL=$?
  ;;
    restart)
      mongrel_cluster_ctl restart -c $CONF_DIR
      RETVAL=$?
  ;;
    *)
      echo "Usage: mongrel_cluster {start|stop|restart}"
      exit 1
  ;;
esac      

exit $RETVAL
Trackbacks

Use the following link to trackback from your own site:
http://blog.gwikzone.org/trackbacks?article_id=easy-setup-a-mongrel-cluster-with-apache-2-2-on-mac-os-x&day=02&month=10&year=2006

Comments

Leave a comment

Comments