Thinking in GIS
a blog about GIS from a urban geogeek living at the countryside
Ruby on Rails applications with Mongrel cluster and Apache url rewriting on Windows
Posted: November 15, 2007Categories: devs, Windows, Ruby on Rails, Apache
Feedback: View Comments
In the last post I showed you a configuration for serving Ruby on Rails applications with Mongrel cluster and Apache in a *nix environment (Ubuntu). With this post I will reproduce the same configuration in a Windows environment.
The main difference in Windows is that you cannot use the mongrel_rails command with the cluster option (provided by the mongrel_cluster gem) that relies on daemonize functionality, only available on *nix platforms. What you will need to do is to manually create n Mongrel services for the n clusters you will need. Let's see how to do that.
Install Ruby, gems and then install Ruby on Rails:
sudo gem install rails --include-dependencies
Now download and install Apache 2.2 using, as the fastest way, the msi package.
Now enable the needed modules (url rewriting, proxy, proxy_balancer e proxy_http) by editing the httpd.conf file (under c:Apache_Software_FoundationApache2.2conf, if you installed Apache in its standard path). You just need to uncomment the following lines (remove the #):
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Install the mongrel and mongrel_service gems:
gem install mongrel (pick last version for win32)
gem install mongrel_service (pick last version for win32)
Now we will create a mongrel cluster of 2 windows services responding at http://127.0.0.1 on ports 3010, 3011 serving a rail application at the path c:wwwrormyapp that will be started from the windows system user. The two windows services will be respectively named mongrel_myapp1 and mongrel_myapp2. Open the command prompt and type:
mongrel_rails service::install -N mongrel_myapp1 -p 3010 -e production -c c:\www\ror\myapp
mongrel_rails service::install -N mongrel_myapp2 -p 3011 -e production -c c:\www\ror\myapp
Now open the windows services tool, make the 2 new services have an automatic startup type (so they will still be started when you reboot). Test if your application is now running at the two ports:
http://localhost:3010
http://localhost:3011
If everything is working fine, you are ready to place Apache in front of these 2 mongrel services, to manage the load balancing of you application.
The best way to configure Apache is to create a Virtual Host for your ROR application. First edit your httpd.conf file, and uncomment the following line:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Now edit the httpd-vhosts.conf file, like this (keep the slashes in the *nix fashion!):
NameVirtualHost *:80 #Proxy balancer section (create one for each ruby app cluster) <Proxy balancer://myapp_cluster> BalancerMember http://myapp:3010 BalancerMember http://myapp:3011 </Proxy> #Virtual host section (create one for each ruby app you need to publish) <VirtualHost *:80> ServerName myapp DocumentRoot c:/www/ror/myapp/public/<Directory c:/www/ror/myapp/public/ > Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
#log files ErrorLog /var/log/apache2/myapp_error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /var/log/apache2/myapp_access.log combined
#Rewrite stuff RewriteEngine On
# 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://myapp_cluster%{REQUEST_URI} [P,QSA,L] </VirtualHost>
Add your app as a host in hosts.file (in the c:WINNTsystem32driversetc folder):
127.0.0.1 localhost
127.0.0.1 myapp
Restart now Apache from the Windows services panel, and if everything is fine you should have your app served by Apache at the following url:
http://myapp