User Tools

Site Tools



mongrel_cluster on Apache

Install mongrel

gem install mongrel mongrel_cluster

Setup mongrel cluster

Create a service user named “mongrel”

adduser -r mongrel

Make sure the user own's your application

chown -R mongrel.mongrel /srv/www/myapp

Create the cluster configuration (will create a file mongrel_cluster.yml under config/)

 mongrel_rails cluster::configure -e production -p 8000 -N 3 -c /srv/www/myapp -a --user mongrel --group mongrel

Configure Apache

  • Example configuration which goes into httpd.conf:
<VirtualHost *:80>
  Include /etc/httpd/conf.d/mongrel_rails.conf
  ErrorLog logs/iccis-errors_log
  CustomLog logs/iccis_log combined
  <Proxy balancer://mongrel_cluster>
## load balancer manager for iccis
Listen 8080
<VirtualHost *:8080>
  <Location />
    SetHandler balancer-manager
    Deny from all
    Allow from
  • Example config which goes under /conf.d/mongrel_rails.conf
DocumentRoot /mnt/nas/srv/www/sgr/iccis/htdocs
  <Directory "/mnt/nas/srv/www/sgr/iccis/htdocs/">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  RewriteEngine On
  # Make sure people go to, not
  # RewriteCond %{HTTP_HOST} ^myapp\.com$ [NC]
  # RewriteRule ^(.*)$$1 [R=301,L]
  # Yes, I've read, but my site already has much Google-Fu on
  # 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
  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

Running the cluster

Start the cluster

mongrel_rails cluster::start

Stoping the cluster

mongrel_rails cluster::stop

Start automatically on server bootup

Mongrel comes with a handy init script…

mkdir /etc/mongrel_cluster
ln -s /var/www/apps/testapp/config/mongrel_cluster.yml /etc/mongrel_cluster/testapp.yml
cp /path/to/mongrel_cluster_gem/resources/mongrel_cluster /etc/init.d/1
chmod +x /etc/init.d/mongrel_cluster

Add this script to the bootup scripts

chkconfig --level 345 mongrel_cluster on
/srv/ · Last modified: 2012/10/27 11:27 by niwo