Friday, April 3, 2009

Deploy Multiple ROR sites on IIS

To deploy multiple ROR sites we will need help of Mongrel Server and ISAPI Rewriter. To run Multiple ROR please follows the steps.

Install Mongrel server

  1. First enter into your Ruby folder from command prompt. For an instance if your installed ruby folder is c:\ruby then enter into that folder
  2. Then run the following command to install mongrel
   c:\ruby>gem install mongrel-1.0.1-mswin32 
                                             or 
        c:\ruby>gem install mongrel-1.0.1  
and chose the version of mswin32
  1. Then you have to install mongrel service by using the following command
           c:\ruby>gem install mongrel_service

Install ISAPI Rewriter

1. run the isapi rewriter installer

Patch Action Controller - CGI Process

When performing a re-direct the header is inspected for the real host when request has been forward, however rails does not strip any extra white space.

# Patch cgi_process.rb
  C:\ruby> cd lib\ruby\gems\1.8\gems\actionpack-1.13.3\lib\action_controller
  C:\ruby> edit cgi_process.rb
      # Line ~83: forwarded.split(/,\s?/).last ==> forwarded.split(/,\s?/).last.strip # strip extra spaces/tabs 

Host Using Mongrel service

Perform the following steps to host rails application using mongrel

NOTE: We will be hosting this rails application under the relative url "/rapp"

NOTE: We only bind to (127.0.0.1), since IIS will eventually forward locally to mongrel. The port chosen is 4004

  1. C:> mongrel_rails service::install -N rails_rapp -a 127.0.0.1 -c C:\rails\rapp -p 4004 -e production --prefix /rapp


2.    C:> sc config rails_rapp start= auto
3.    C:> net start rails_rapp
 
#To remove the service use: mongrel_rails service::remove -N rails_rapp

Test using mongrel service

Browse to http://localhost:4004/rapp/hello/world, Note all url's are now relative to rapp, This allows multiple rails apps to be hosted behind iis

Now Host Behind IIS

1. Open your IIS manager

2. Goto-> Default Web Site -> New -> Virtual Directory

Alias/Name -> rapp

Directory/Path -> C:\rails\rapp\public

Permissions -> Read + Scripts + Executables

Test static portion

Browse to http://localhost/rapp/hello/world - You should see the standard welcome to rails page

Configure ISAPI_Rewrite3 to forward to Mongrel

Confiure ISAPI_Rewrite to forward all http://host/rapp/* to mongrel on http://127.0.0.1:4004/rapp/*.

NOTE: We do not forward requests containing a full stop (.), So files like .js and .css etc will be serviced up by iis

   1.   Open configuration file of ISAPI_Rewriter (httpd.conf or INI)  
        Click -> Edit
        Enter your new rule
           RewriteEngine on
           RewriteBase /
           RewriteProxy rapp/([^.]+)$ http://127.0.0.1:4004/rapp/$1
 
   2.   Rails .htaccess legacy
Remove/Rename the c:\rails\rapp\public\.htaccess file, as this is read by ISAPI_Rewrite but is designed for Apache !!!
 
 

Rename the .htaccess file of your rails project to apache.htaccess

Restart IIS

Restar your IIS server

And browse http://localhost/rapp/hello/world

For another project we have to follow the steps from “Host Using Mongrel service” with new prefix, new port number and a new service name.

NOTE: Because of the prefix the site does not get the image’s URLS and javascript’s URLS properly. So it does not show the images properly. (If we don’t use prefix in that case, in ISAPI_Rewrite the Two sites will be redirect to same configuration file and it will not work. So we have to spend some more time investigate on this two issues)

No comments: