Ruby on Rails
Fast CGI and Apache2 for Windows XP

Rails Production environment on Win 32

If you want to get Rails on XP using Apache 2.2 + FastCGI/SCGI support with the MySQL C binding download the Rails Prod Win setup kit and follow the instructions…you should be up and running in no time.

or InstantRails .

Other Method (by hand…)

This has been a pretty big thorn in my side (and it seems others have gone through the same headaches and all the info out there is just plain wrong, wrong, wrong) and I’ve finally made it work for me, so I thought I’d share.

Things you’ll need

(This is my development machine, though I suspect this should work under different configurations)

Initial intallation

Configure Apache

Now you’ll need to configure Apache. Don’t worry, this is easy-easy. Go to your start menu, find the Apache programs group. In there you’ll see a sub-group called something like “Configure Apache Server”. Click on “Edit the Apache httpd.conf Configuration File”

Now you’re looking at a text file with all kinds of confusing stuff, right? Look for a line that looks like


#LoadModule rewrite_module modules/mod_rewrite.so

and change it to


LoadModule rewrite_module modules/mod_rewrite.so

(i.e., uncomment the line)

Then add a new line like this


LoadModule fastcgi_module modules/mod_fastcgi.dll

If you use Apache 1.3.x you also need to make corresponding changes in the AddModule section below the LoadModule section.

Find the line


#AddModule mod_rewrite.c

and uncomment that:


AddModule mod_rewrite.c

Then add a new line:


AddModule mod_fastcgi.c

Now you need to set up a root directory for your application. Look for a line that starts with DocumentRoot and change the path to your Rails application folder.

Next look for a line that starts with Directory and change that path to your Rails application folder (same as above)

(Remember for both paths above to use forward slashes not the normal Windows backslashes)

Almost done with Apache configuration…

(see Fast CGI and Apache2 for Windows without VirtualHosts for an alternative to using VirtualHost directives)

at the bottom of the file add this:


<VirtualHost *:80>
  ServerName rails
  DocumentRoot "c:/path/to/your/rails/app/public" 
  <Directory "c:/path/to/your/rails/app/public/">
    Options ExecCGI FollowSymLinks
    AllowOverride all
    Allow from all
    Order allow,deny
    AddHandler cgi-script .cgi
    AddHandler fastcgi-script .fcgi
  </Directory>
</VirtualHost>

(set paths above accordingly)

Configure your Rails app

Go to the public folder in your Rails application and open .htaccess

Look for the rewrite rule:


RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

and change it to:


RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Comment out the Apache section in your .htaccess as you’ve already referenced these in your Apache httpd.conf file earlier.

Open dispatch.fcgi and change the first line to


#!c:/ruby/bin/ruby.exe

to accomodate Windows

Add a new host

Last little bit…

Open up your host file (c:\windows\system32\drivers\etc\host)

and add this line:


127.0.0.1     YourRailsAppName

Cool, you’re done

Note from porter.ea:
One additional edit that I found necessary was to add ”#!c:/pathto/rubybin/rubyw” (note the “w”) at the top of the “C:\appname\public\dispatch.fcgi” file.

This was purely a guess on my part based on perusal of the /ruby/bin directory, but it caused my Apache to go from hanging for a long time, then displaying: “Application error Rails application failed to start properly”, to displaying the correct output in better time.
:End Note

Now start Apache, open a browser and type in ”\YourRailsAppName”

you should load the default Welcome to Rails page with blazing \FastCGI speeds.

Like I said easy-easy.

Another Note:

if the Above instructions still do not work for you, please try reversing the slashes:


#!c:\ruby\bin\ruby

It has worked for me.

Additional Notes

1) You might need to add the following line to httpd.conf (particularly on a dev machine)

<IfModule mod_fastcgi.c>
  FastCgiServer {rails_app_dir}/public/dispatch.fcgi\
      -initial-env RAILS_ENV=development \
      -processes 3 -idle-timeout 120  
</IfModule>

This prevents premature timeouts and limits the FASTCgi processes to 3.

2) If using mysql and get ‘uninitialize constant mysql’ error, you may have to copy libmysql.dll to ruby bin (e.g. c:\ruby\bin).

3) Take special care to run your rails-application in “production”-mode. See environment.rb (msc)

An Intermediate Option

I already have Apache and Ruby, so I didn’t want to install them in a new package, but I couldn’t get the latter to work. dispatch.fcgi started up and then exited immediately. The reason was that it was missing the ‘fcgi’ gem. I couldn’t install the gem on Windows because I have no C compiler.

My solution was to install Ruby for Apache which adds the necessary components to your existing Ruby and Apache installations.

Another Note about apache port

If you are on windows, you may probably use the virtual host with a non-80 port (since port 80 used by IIS defaultly). Thus don’t forget add “Listen ${port_number}” in the httpd.conf, otherwise you will never see the rails welcome page.
I forgot this and it wasted a lot of my time to find the problem.

Questions

  1. Does anyone know how I could setup multiple rails apps on the same machine using virtual hosts?
  1. Oracle. I tried using the rails-prod-win installer and it got me to the exact same spot I had been when I had tried setting everything up separately. First two lines of error are:
    
    LoadError (Oracle/OCI libraries could not be loaded.):
        C:/Program Files/rails-prod-win/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/oracle_adapter.rb:661:in `oci_connection'
    My app is running, but for any part of it requiring db access I get the error. My app runs perfectly under Webrick and Mongrel, so apparently they can find the oci8 libraries. Ideas?
  1. I have hit some error containing modules specifically fastcgi module not being found.. what can be the cause of it?

See http://www.apachelounge.com/forum/viewtopic.php?p=5367 ... I’m going to try it, hope it works!