Nov
15
Ruby on Rails applications with Mongrel cluster and Apache url rewriting on Windows
Filed on 2007, Nov, 15 under devs, Ruby on Rails, Windows, Apache
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_Foundation\Apache2.2\conf, 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:\www\ror\myapp 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:\WINNT\system32\drivers\etc 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
Related posts:
- Ruby on Rails applications with Mongrel cluster and Apache url rewriting on Ubuntu
- Installing MapServer (c# mapscript tutorial, part 2)
Comments
24 Responses to “Ruby on Rails applications with Mongrel cluster and Apache url rewriting on Windows”
Leave a Reply









Hi,
excellent blog how to setup up Apache and Mongrel under Windows. Spent a few days browsing for such a document.
Thankx for getting me started with this!
Benjamin
Thanks for your blog man.I have searched around two days for the solution.Everything working fine now.
Hello
I am running Vista.. i have followed instructions mentioned in this document.. but after installing service when i am trying to connect to localhost:3010.. its not working.. instead it looks for localhost.com
Please advice.
Regards
Ajit
[…] mongrel, Windows This is an excellent article on how to setup Mongrel and Apache under windows. Read the rest of this […]
Thanks very much. I wish all the doc was as well done as this. You saved me several hours.
Tom
Hello,
I have followed this instruction but when i am trying to start my apache, it returns error message :
httpd-vhosts.conf line 30
invalid command '
BalancerMember http://myapp:3010
BalancerMember http://myapp:3011
Is there anyone here has an idea what should I do?
Thanks in advance for your help
Regards,
Jaan
Jaan, looks like you didn't load the proxy_balancer_module. Let me know
Hi Paolo,
Thanks for your help.
ya I tried to load proxy_balancer_module as you suggested and now it's working
Thanks a lot Paolo
Great!!!! Thanks a lot!!!! Worked for me too
Hi Paolo,
I've tried your solution to run 4 instances of mongrel under Apache in a Windows 2003 Server machine. Unfortunately, there is a problem that is preventing the application to work. When I try to access the address of it under Mozilla Firefox on the server (http://localhost/), it turns out that a error is always shown:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /.
Reason: Error reading from remote server
—
The instances of Mongrel are working, I can access them through the browser. The problem seems to be in the Apache-Mongrel interface.
I tried some solutions that I found throughout the web (I can't say I'm a Apache expert), but nothing seems to work. A lot of pages references an another that shows the solution to this problem: http://blog.pomozov.info/posts.....rror.html. But, unfortunately, it is no longer available online and it's nowhere to be found.
Since I followed your guideline, I thought that I should reply and show the problem here.
The enviroment of the server:
OS: Microsoft Windows Server 2003 Standard Edtion
apache: 2.2.8-win32-x86-openssl-0.9.8g
ruby: ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
rails: 1.2.5
mongrel: 1.1.3
mongrel_service: 0.3.4
To keep the reply shorter, I'm not sending the content of the httpd.vhosts.conf. But, should it be necessary, please let me know.
Any help will be welcome. Thanks in advance!
Hi Wagner
I am sorry I have not idea, it looks like something connected with the mod_proxy module. Did you load it in the correct order (before the balancer stuff)?
In the log file do you get some more specific messages?
Try to look also here: http://groups.google.it/group/.....6beeabb95e
Try also to post to the httpd group
let me know
Paolo,
Thanks for the reply.
The mod_proxy is being loaded before the mod_proxy_balancer. The only diffence between the order specified in your post and the order in my file is that the mod_rewrite is actually being loaded after the others. I had only uncommented the lines in httpd.conf. Anyway, I modified the file to have the same order described in your post, restarted the server, but the problem still occurs.
I'll keep trying. As soon as I get some news about the problem I'll post it here.
Thank you!
Hi,
I using this recipe and it works great. Thanks a lot.
I am using act_as_authenticated plugin for the authentication. I am including a link to my app into the company's SharePoint portal. Doesn anyone know if i can use windows integrated security with apache so the user doesn't have to login twice.
Any input is appreciated,
Thank you,
Thank you very much for the recipe.
You saved my day.
Hi
Thanks for wonderfull article.I have used the recipe to run the application. Now i want to deploy it in such a way that every user doesn't have to make an enrty in to the host file as we did
"127.0.0.1 myapp"
once it is deployed on any m/c then it should be such that from remote m/c within the same n/w anyone can access the application without making an entry in to the host file.
Any input is appreciated,
Thanks in advance…
@Lucian: you may try using mod_auth_ldap:
http://httpd.apache.org/docs/2....._ldap.html
@Neetal: you may do that, is just a matter of setting the directory of your ROR applications in you default web site (and you can put more ones, of course)
Thanks for such a nice and up-to-date article. I was trying for this for 2/3 days but as I am not expert in inx platform i was unable to complete the thing. But in your post i got everything to-the-point so that I got the success in one go.
Thanks for the reply.
I want to deploy another application on the same machine. I followed the recipe (it worked great for the first app) but i don't seam to get it to work for the second app; i get "No route matches "/my_second_app" with {:method=>:get}".
I created a new Proxy balancer section, and a new Virtual host section for each app, and i made sure the server name is unique. When i hit the port http://hostname:3005/my_secon_app it works.
What am I missing??! Any input is appreciated.
@lucian: no idea what is going on in your case. The article applies to set the environment on 2 different hosts. Maybe you forgot to add the host of your second application in the host file?
Hi Paolo!
Sorry for taking too long to give you some feedback about the problem I commented here months ago. Our client was thinking about moving his server to Linux and he's finally decided to do so. Consequently, I won't be trying to configure our application with Mongrel cluster and Apache on Windows anymore. Hopefully, it will be easier on Linux. We're planning to use Nginx…
Thanks for the help anyway!
Ciao!
Great article, it helped me a lot!
@Guillaume: glad you liked it!
@Wagner: great choice! Doing that for linux is a breeze, and BTW mod_rails now is out there: http://www.modrails.com/
Thanks mate, this helped me out bigtime!
This is great I all most gave up looking for this information.
Chris