Thursday, October 26, 2006

Rails Setup With Mongrel and Apache mod_proxy

I set up Apache to proxy requests to the 3 Mongrel servers providing blogging, wiki, and todo list functionality.


This setup is for local access only.



  • Create mongrel system group and user

  • Place Rails applications in /var/www/mongrel

  • chown -R mongrel:mongrel /var/www/mongrel

  • Create mongrel initscript: /etc/init.d/mongrel and symlink to /etc/rc2.d/S99mongrel


#!/bin/bash
case $1 in
start)
port="12000"
for appdir in `ls /var/www/mongrel`; do cd /var/www/mongrel/$appdir ; echo " * Starting $appdir on port $port."
mongrel_rails start -d -e production -p $port --user mongrel --group mongrel -a 127.0.0.1 ; port=`echo "$port + 1" | bc`
done
;;
stop)
for appdir in `ls /var/www/mongrel`; do cd /var/www/mongrel/$appdir ; echo -n " * Stopping $appdir..."
mongrel_rails stop ; done
;;
restart)
for appdir in `ls /var/www/mongrel`; do cd /var/www/mongrel/$appdir ; echo -n " * Restarting $appdir..."
mongrel_rails restart ; done
;;
esac


  • Start mongrel service and test

  • Add entries for 127.0.0.3 blog.localhost, 127.0.0.4 wiki.localhost, etc. to /etc/hosts

  • Enable mod_proxy (sudo a2enmod proxy)

  • Allow local proxy requests by modifying the /etc/apache2/mods-enabled/proxy.conf (Allow 127.0.0.0/255.255.255.0)

  • Add Listen 127.0.0.3:80, etc. to /etc/apache2/ports.conf

  • Create VirtualHosts entries for the servers in a new sites-enabled file (I used proxy)


NameVirtualHost wiki.localhost:80
<VirtualHost wiki.localhost:80>
<Directory "/">
Order allow,deny
Allow from all
</Directory>
ProxyPass / http://localhost:12000/
</VirtualHost>


  • Restart Apache

No comments: