<< Solving Apache Derby and Continuum installation issue on Debian | Home | Grails 1.1.1 URL mapping and reverse mapping example >>

Hosting/Deploying Ruby On Rails apps on Windows

Using Mongrel and Apache with virtual hosts

So recently I've been getting on board the Ruby On Rails bandwagon.  Well, not so much the Rails wagon but definately the Ruby wagon, anyway, I digress.  If like me you've heard great things about Ruby on Rails and then tried it and ended up with some neat little web-app that you want to deploy but only have access to a Windows Server for deployment that for some reason is running apache and not IIS then here's how to get a vhost up and pointing to your Ruby app.

I'm assuming you've already got Ruby installed and up to date and alsp Apache 2.2 installed and running for this.

gem install rails

cd c:\inetpub\wwwroot
rails test_app
cd test_app
ruby script/generate scaffold Accounts username:string last_seen_at:datetime
del public\index.html
ruby script/server -b -p 30001 -e production

The rails app is now accesable on: ""

That's no good though, as the ruby script/server is still running in a console window and we want a service.  There's also no hostname specified, and it's on the wrong port too.   So stop that (ctrl+c a few times), and this is where mongrel comes in

gem install mongrel

mongrel_rails start -a -p 30001 -c d:\inetpub\wwwroot\test_app -e production

The rails app is now accesable on: ""

"eh, how's that better?" you say, well it's not, next up: "mongrel_service"!

gem install mongrel_service

From a command prompt you can now run "mongrel_rails help" or more specifically "mongrel_rails service::install --help"

(As a side note here, I should make a post on the cantcodeforshit.com blog about the general inconsistancy between many linux command line utilities, the one above for instance uses "--help" sometimes AND "help" (with no prefix) at other times, in the same tool!  On windows most tools use the "/option" notation, on linux you have either -single_letter --word or no prefix, come on *nux people, standardize your command line parameter notation already!)

Now, to install a service you can do this:

mongrel_rails service::install -N mongrel_test_app -a -p 30001 -e production -c d:\inetpub\wwwroot\test_app

Note: if you're using Radiant CMS (which I highly recommend) and installed it using "gem install radiant", then "radiant test_app" (as per their docs) instead of "rails test_app" like you would for a normal rails app then mongrel_rails will complain with "!!! The path you specified isn't a valid Rails application."  mongrel_rails's "service::install" command is looking for rails directories, one of which is called "app" but when you run radiant test_app it doesn't create this directory so just create it in next to the other radiant files and try again.
Note 2: I could make another post on can'tcodeforshit.com about wht mongrel_rails start and mongrel_rails service::install don't both check for "app" but again I digress.

Now if you open up mmc with the computer management snap-in and browse to "Services" you'll see a new service listed, from there you can set the Start setting "Automatic" instead of "Manual" and it'll start up each time your machine boots.

Now, that's great. but what about a vhost and apache?

If like me you store each of your vhost configuration files in "C:\Program Files\Apache Software Foundation\Apache2.2\conf\vhosts" called "hostname_here.conf" and include them all from "C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf" by doing this something like this:

# Use name-based virtual hosting.
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "D:/InetPub/wwwroot/default/htdocs"

# other vhosts

Include conf/vhosts/*.conf

Then you can create a file called "C:\Program Files\Apache Software Foundation\Apache2.2\conf\vhosts\test_app.com.conf" and stick this in it:

<VirtualHost *:80>
    ServerName www.mytestsite.com
    ServerAlias mytestsite.com
    ProxyPass / http://localhost:30001/
    ProxyPassReverse / http://localhost:30001/

In order for that to work you'll need to update your httpd.conf so that some proxy and vhost modules are loaded:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

And of course the conf/extra/httpd-vhosts.conf needs to be uncommented too:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

After that you can run this command to check your configs:

"c:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -t

And then finally restart your apache server

net stop apache2.2
net start apache2.2

After that you should be able to go to http://www.mytestsite.com/accounts (and http://mytestsite.com/accounts) and the rails app should be there.

Your next step should be to ensure that your ruby app is secured, don't forget otherwise the script kiddies will have a field day on your box.

Hopefully this should help someone out, if you found it useful please leave a comment below.

Send a TrackBack