Rails 2.3.0 + IIS7 + FastCGI = Rails on Windows FTW!

I love Rails, and I run Windows.

There, I said it. I know many Railers scoff at us, mock us, etc. Whatever. I run Windows (Vista, specifically). I’m a Microsoft.Net Web Developer at my day job. My company has invested heavily in the Microsoft platform. I don’t like WebForms (why is a topic for another post), and, because of Rails, I know there is a better way to do web development. Yes, I know about ASP.Net MVC, but I have yet to try it. What I have tried is Rails, and I know that a Microsoft stack is the way to get it into my day-to-day work.

So, a couple of days ago, with the announcement of Rails 2.3.0 rc1 I decided to see if I could get the latest Rails running under IIS 7 on my Vista notebook. I tried to find directions on how to accomplish this, but none were very straight forward. So, I’ve decided to document the process here for myself and other WinRailers.

Note: 2.3.0 rc1 will not work, due to this bug in Rails’ FastCGI handler. Fortunately, this was fixed and merged into the rails source. You will need the 2.3.0 gem installed, though, to generate a 2.3 Rails app. I’ll go into details below.

First, Credit where Credit is Due

I can’t take all the credit for this. The following two articles helped to guide my down my path:

Software You’ll Need

The Steps You’ll Take

  1. Enable FastCGI support in IIS7
  2. Install URL Rewrite module
  3. Install Ruby
  4. Install msysgit
  5. “Install” SQLite
  6. Update Rubygems
  7. (gem) Install Rails
  8. Create a Rails app
  9. Grab the latest Rails 2.3 from github
  10. web.config
  11. Setup up web site in IIS

1 – Enable FastCGI support in IIS7

IIS7 supports FastCGI natively now. You just need to make sure you install CGI support. It was not obvious to me that this was needed. I was looking for a FastCGI option, but it is listed as plain old CGI. Microsoft has good directions on how to accomplish this task on their site, so I will not rehash them here.

2 – Install URL Rewrite Module

The easiest way to get this (and keep it updated!) is to use the Web Platform Installer. It might also want to install updated FastCGI support. I let this happen with no ill effects. So, go ahead a check “URL Rewrite” in the installer and go to town.

3 – Install Ruby

Luis Lavena has done an awesome job of maintaining the Ruby One Click Installer for Windows. Of particular importance to my little endeavor is that, in the One-Click Ruby Installer 1.8.6-26 Final Release, Luis included ruby-fcgi and the FastCGI C extension. Thanks, Luis! You can safely run the installer and click “Next” all the way through.

4 – Install msysgit

Rails developers use git. You need get to pull the latest source, which includes the FastCGI fix I aluded to above. msysgit is your best option. You can safely run the installer and click “Next” all the way through.

5 – “Install” SQLite

I use quotes around install because you don’t really install anything. You simply unzip the sqlite-3_6_10.zip file into any folder in your path. Personally, I create a folder,

1
C:\bin

, add that to my path, and unzip the files there. I keep other things here, too, like wget.

6 – Update Rubygems

The version packaged in the OCI is old. Save yourself trouble, open up a command prompt, and:

1
gem up --system

7 – (gem) install Rails

You need to have Rails installed. Rails 2.3.0 rc1 has some undeclared dependencies. So, first:

1
gem install test-spec rack

Next,

1
gem install rails --source <a href="http://gems.rubyonrails.org">http://gems.rubyonrails.org</a>

8 – Create a Rails app

Most of my code goes into the following folder: C:\Development. Assuming this directory does not exist:

  1. Open a command prompt
  2. 1
    c:
  3. 1
    cd \
  4. 1
    mkdir Development
  5. 1
    cd Development
  6. 1
    rails myapp

9 – Grab the latest Rails from github

This is where msysgit comes in. Fire up Git Bash, and:

  1. 1
    cd /c/Development/myapp/vendor
  2. 1
    git clone git://github.com/rails/rails.git

10 – web.config

The IIS team, under Scott Guthrie (also oversees ASP.Net, particularly ASP.Net MVC), have done some awesome stuff in the last couple years. They have turned IIS 7 into, in the words of Software Journeyman Corey Haines, “A respectable web server”. I agree. IIS 7 is a lot more like Apache than it ever used to be, and I think this is a good thing.

One of the nice things is that you can now move a bunch of your web server configuration into a web.config file in your application. That means you can add it to version control :)

ASP.Net programmers will be intimately familiar with this file. Everyone else can think of this file as the equivalent of Apache’s .htaccess files. In our case, we’ll use the following web.config to configure both the FastCGI handler and the URL Rewrite rules:

<?xml version="1.0" encoding="utf-8"?>
<rule name="Rewrite app_offline.html if file exists" stopProcessing="true">
  <match url="^.*$" ignoreCase="false" />
  <conditions>
    <add input="{DOCUMENT_ROOT}/app_offline.html" matchType="IsFile" ignoreCase="false" />
    <add input="{SCRIPT_FILENAME}" pattern="app_offline.html" ignoreCase="false" negate="true" />
    <add input="{SCRIPT_FILENAME}" pattern="^(.+).(png|gif|jpg|css|js)$" ignoreCase="false" negate="true" />
  </conditions>
  <action type="Rewrite" url="/app_offline.html" />
</rule>

So, let me explain what is going on here. First, the handlers section. This is where we set it up so that the FastCGI handler will process requests for dispatch.fcgi with the ruby interpreter, and run myapp in development mode. To run in production, simply replace development with production.

The next section is the URL Rewrite section. As you can see, it is much more, um, expressive than Apache’s syntax. Anyway, another cool thing Microsoft has done with IIS7 and the URL Rewrite module is an Import function. This allows you to import your existing mod_rewrite rules into their format. Cool! So, that’s what I did. I took some existing mod_rewrite rules for FastCGI, and popped it in to the UI to get the markup above.

You’ll notice a section that is commented out. That is an attempt to get the app_offline.html file working. That is, if the file exists, your app is in maintenance mode, and all requests for any Rails route should instead have this served up. I wasn’t able to get this working. Perhaps someone can take a look at it with fresh eyes.

11 – Set up web site in IIS

This one is crazy simple. Create a new website, with the physical path set to the public directory of your Rails app. So, in our case, that would be:

1
C:\Development\myapp\public

There is one more thing that I do, but I don’t know if it necessary. For the Application Pool that was created for this site, I change the “.Net Framework Version” to “No Managed Code”. The Rails app is not a .Net app, so I see no reason to turn on managed code for this app.

Rails-2_3_0-IIS7-AppPool

That’s about it! No, seriously. It is that easy. Don’t believe me? Open up a web browser and go to the web site you created (localhost for me). Click on “About your application’s environment”, wait a bit for the FastCGI process to spin up, and voila:

Rails-2_3_0-IIS7

  1. AkitaOnRails says:

    Nice article. I also try to help Windows users to develop Rails. But my recommendation is: develop under Windows and deploy production stuff under Linux. IIS + Ruby’s FCGI is simply unusable. You can use IIS 7′s newest reverse proxy and url rewrite module to fall back to mongrel processes instead and avoid FCGI’s nightmare altogether. This is the only available option to Rails deployment under Windows.

    Try running some httperf or apache bench runs. I tested FCGI under IIS6 and the ruby processes would easily die after around 100 requests, rendering it unusable for real usage. Leave Task Manager opened and pay attention to the ruby processes there. If you apply just a little bit of pressure on them, they will crash and restart.

  2. Will says:

    @AkitaOnRails Thanks! The article is mostly geared towards folks who, like me, don’t have a Linux box at their disposal, and due to the nature of their jobs, cannot use outside servers (I’m I. Healthcare, so I have HIPPA to worry about). I do recognize that FastCGI is most likely not the way forward for deploying Rails on Windows. However, as you can see, it is an easy win, especially to show your boss or coworkers Rails running behind a Microsoft stack.

    IIS 7′s Application Request Routing, as it stands right now, is a non-starter with mongrel. This is because it doesn’t do proxying to the same host name, with different ports for each member of the farm. At least, there is no GUI for it. I know there is appcmd, but I don’t know enough about it yet to say if it can be done. However, appcmd may also be a way to automate deployment with Capistrano.

    Another idea I’m kicking around is writing an IIS module or handler, using IronRuby, that speaks the Rack protocol. I’ll keep everyone posted.

  3. Dejan Dimic says:

    My customers are mostly Windows oriented and I share your pain, even I do my Rails development on Ubuntu.

    Personally I use, if I had to, IIS proxy to Mongrel as it seams to me to be a better way. Both running as services.

    The main problem that I have are the proprietary MS SQL data bases. That can be managed too but it’s a pain.

    The final point is that Ruby and Ruby on Rails development on Windows platforms is even a pleasant adventure.

  4. Chris says:

    Hello,

    First I want to say great post. This is a situation I am working through right now, but am having an issue with the web config loading. I am getting an error saying that it is not properly formed xml. Have you ever run into this? Has anyone ever run into this?

    Keep up the quality writing!

    Thanks,
    -Chris

  5. Will says:

    My bet is that you copy-pasted from my post, and its choking on the first line. ;) For some reason, Word Press wants to HTML encode that line, but no others. Anyway, its the mandatory XML declaration. Try HTML decoding that first line to see if that fixes the problem.

  6. ahmed says:

    Thanks for your great article.
    A porblem is that I can’t see the body of your config file in part 10 – web.config and the only thing is viewable is the firts line. Please also tell where this config file should be placed.
    Thanks again

  7. Will says:

    These instructions are over a year old. At this point, if you want to run Ruby on Rails under IIS, I would suggest going with Rails on IronRuby and IronRuby.rack to run it under IIS.

  1. [...] o blogueiro William Green publicou mais um artigo sobre IIS, desta vez mostrando como rodar Rails 2.3, com IIS 7, com FastCGI. Eu já fiz cenários desse antes e descobri que rodar inicialmente não é difícil, mas manter o [...]