User Tools

Site Tools


ruby:rails:mongrel

Mongrel

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 127.0.0.1 --user mongrel --group mongrel

Configure Apache

  • Example configuration which goes into httpd.conf:
## iccis.icc.example.com
<VirtualHost *:80>
  ServerName iccis.icc.example.com
  Include /etc/httpd/conf.d/mongrel_rails.conf
 
  ErrorLog logs/iccis-errors_log
  CustomLog logs/iccis_log combined
 
  <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>
</VirtualHost>
 
## load balancer manager for iccis
Listen 8080
<VirtualHost *:8080>
  <Location />
    SetHandler balancer-manager
    Deny from all
    Allow from 192.168.63.190
  </Location>
</VirtualHost>
  • 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
  </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

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/wiki.niwos.com/data/pages/ruby/rails/mongrel.txt · Last modified: 2012/10/27 11:27 by niwo