Apache 2.2 +mod_balancer, Rails and You
At work, we have a couple of internal applications powered by Ruby on Rails, such as our jobs board, and our customer management system. We use several technologies to make sure that our internal applications have 99.999% uptime. Over the next few weeks, I’m going to go over some of the things that we use to keep our uptime high and ease of development…umm…easy.
Apache is pretty much the defacto standard in the *nix hosting world. And our deployments are no different. Since we use apache and php to power our wiki, and apache and fastcgi to power our ticketing system, we decided that it would be best to use apache to power our rails based systems as well. We use mod_proxy_balancer to point back to a number of mongrel instances running our applications. We also have some rewrite rules to display maintance pages and also server static content through apache itself instead of mongrel.
We installed Apache 2.2 on FreeBSD 7.0 and included mod_proxy, mod_proxy_http, mod_proxy_balancer and mod_rewrite. Then we created a proxy.conf file in the Includes directory, and added pointers to the mongrel instances that would be running the site, which looks something like:
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:6001
BalancerMember http://127.0.0.1:6002
BalancerMember http://127.0.0.1:6003
BalancerMember http://127.0.0.1:6004
BalancerMember http://127.0.0.1:6005
BalancerMember http://127.0.0.1:6006
BalancerMember http://127.0.0.1:6007
BalancerMember http://127.0.0.1:6008
BalancerMember http://127.0.0.1:6009
BalancerMember http://127.0.0.1:6010
BalancerMember http://127.0.0.1:6011
BalancerMember http://127.0.0.1:6012
BalancerMember http://127.0.0.1:6013
BalancerMember http://127.0.0.1:6014
BalancerMember http://127.0.0.1:6015
</Proxy>
Then we created another file, called virtuals.conf in the Includes directory, and setup a name based virtual host that looks like:
<VirtualHost *>
ServerName example.com
ServerAlias test.example.com example.org
DocumentRoot /usr/local/rails/app/current/public<Directory “/usr/local/rails/app/current/public”>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
</virtualHost>
Some other things we had to do were create a file in our rails script directory called spin (which tells the server how to start itself) and add this line to it.
/usr/local/rails/app/current/script/process/spawner -p 6001 -i 15
This fires up 15 mongrel processes listening on ports 6001-6015. We also had to make sure that the scripts in the script directory were set to executable in subversion with svn propset svn:executable on scriptname. This setup allows us to deploy changes rapidly to the system, and gives us an easy maintance page to put up when we have to make major changes.
If you have any questions about our setup, or if you need a little advice on how to get yourself running on rails, feel free to drop a comment, and i’ll try my best to answer any of your questions.
Posted: July 8th, 2008 under Computers, Ruby on Rails.
Comments: 1
Comments
Pingback from » Apache 2.2 +mod_balancer, Rails and You Apache: What The World Is Saying About Apache
Time: July 9, 2008, 6:18 am
[...] Apache 2.2 +mod_balancer, Rails and You At work, we have a couple of internal applications powered by Ruby on Rails, such as our jobs board, and our customer management system. We use several technologies to make sure that our internal applications have 99.999% uptime. Over the next few weeks, I’m going to go over some of the things that we use to keep our uptime high and ease of development…umm…easy. Apache is pretty much the defacto standard in the *nix hosting world. And our deployments are no different. Since we use apache and p [...]
Write a comment