Ruby On Fail

The documentation for setting up a web server for RoR (Ruby on Rails) on Windows is really lacking. Figuring out what server software you’re supposed to use is probably the hardest part. Should you use Apache or IIS as the web server? How are you going to connect it to Ruby? CGI, FastCGI, fcgid, Mongrel, mod_ruby, passenger (aka mod_rails / mod_rack) or WEBrick (not recommended for production use)?

If you pick a combination that doesn’t work properly on Windows, you can spend hours (as I did with mod_fcgid) trying to figure it out.┬áThe solution that worked for me was setting up Mongrel (which is a Ruby web server) as a service, and using Apache to proxy Ruby requests to Mongrel.┬áHere’s a step by step tutorial:

Note: I’ll assume you already have Apache setup, and know the basics of configuring it.

  1. Setup Ruby with all the gems needed to run your application
  2. Test the application on WEBrick (the built-in Ruby web server)
  3. Install the Mongrel gems as described here. When you try to install the mongrel gem, it will probably fail and ask for some dependencies, which you can easily install by running gem install depName.
  4. Run Mongrel from the command line to test that it’s working correctly
    1. mongrel_rails start -c C:\app\path\here -a 127.0.0.1 -p 4123 -e production -l C:\path\to\log\here.log
    2. Browse to http://127.0.0.1:4123, you should see the application
  5. Now that Mongrel is working, you probably want to install it as a service. Be careful, the built-in service program doesn’t work unless it runs as local system. I recommend using SRVSTART. If you use srvstart, use the following command line:
    1. C:\Ruby\bin\Ruby.exe C:\Ruby\bin\mongrel_rails start -c C:\app\path\here -a 127.0.0.1 -p 4123 -e production -l C:\path\to\log\here.log
    2. If you don’t use that command line, the service will be in charge of a batch file stub that’s provided with Mongrel (which exits after it starts the Ruby process), instead of the actual Ruby process, which can cause problems.
  6. To setup Apache to proxy requests for Ruby scripts to Mongrel
    1. Enable mod_proxy, mod_proxy_http and mod_rewrite
    2. Use the following settings for your virtual host (source):
             DocumentRoot "C:/path/to/app/public"
      
             <Directory "C:/path/to/app/public">
                     Options FollowSymLinks
                     AllowOverride None
                     Order allow,deny
                     Allow from all
             </Directory>
      
             RewriteEngine On
      
             # Check for maintenance file. Let apache load it if it exists
             RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
             RewriteRule . /system/maintenance.html [L]
      
             # Let apache serve static files
             RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f
             RewriteRule (.*) $1 [L]
      
             # Don't do forward proxying
             ProxyRequests Off
      
             # Enable reverse proxying
             <Proxy *>
                     Order deny,allow
                     Allow from all
             </Proxy>
      
             # Pass other requests to mongrel instance
             ProxyPass / http://127.0.0.1:8200/
             ProxyPassReverse / http://127.0.0.1:8200/

After you restart Apache, you should be able to browse to the virtual host and see the application.

Comments are closed.