Rails 3 on Windows with Thin
February 23rd 2011 byYou must deploy your Rails application on Windows? You want to use the latest Ruby and Rails releases, but you've heard that Mongrel, which was the de facto server for Ruby 1.8.x, still doesn't work on Ruby 1.9.2? Well, don't cry. Here's how to deploy your Rails 3 application on Thin, Windows and Ruby 1.9.2.
What will I need?
Nothing really exceptional, belive me. Here is the complete list :
- Windows
- Ruby 1.9.2 or 1.8.7
- DevKit
- Windows Resources Kit (optional)
But… What are the steps?
Ho, you, curious one! Well, if you insist, here we go…
Presteps
- Install Ruby
- Install Rails 3
- Install DevKit
Step 1 - EventMachine
Ok, so you'll need a version of eventmachine greater than 0.10.0. With DevKit installed, installing eventmachine-0.12.10 should be easy.
gem install eventmachine -v=0.12.10
Step 2 - Thin
Now that you have eventmachine, let's get Thin! Here again, with both DevKit and eventmachine, everything should go fine.
gem install thin
Step 3 - Fail…
From now on, you should be able to start your thin server. But… if everything goes as expected, it should crash, saying that Thin doesn't find your Rails gem. The reason is that thin doesn't play well with the latest version of Rack. So, that brings us to the next step: fixing Rack!
Step 4 - Fixing Rack
You'll need to be sure to have only one rack gem installed, and, of course, the good one, which is 1.1.0. So :
gem uninstall -a rack
gem install rack -v=1.1.0
Step 5 - Success!
You should now be able to start you Rails application using your brand new thin server. A simple thin start at the root of your app should do the job. Open your browser and go to localhost:3000 to be sure everything is fine. So, happy now? Wait… what? You want it to run as a Windows Service? Ok, ok…
Step 6 - Windows Resource Kits
You'll need to install Windows Resource Kits, which is use to create simple services. You could use something else, but I find that program easy to use. Here is the command line I used:
C:\Program Files\Windows Resource Kits\Tools> instsrv "service_name" "c:\program files\Windows Resource Kits\Tools\srvany.exe"
We now have an empty service… Not really useful. Let's complete it! Open regedit.exe then navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[service_name]. Create a new key (folder) called Parameters in which you'll create the following strings with their corresponding values:
- Name=Application, Value=c:\ruby192\bin\ruby.exe
- Name=AppDirectory, Value=ruby_app_folder
- Name=AppParameters, Value= c:\ruby192\bin\thin start -p PORT -e ENV
Step 8 - Test and run!
To test your service, in console, juste write net start service_name. You can also open services.msc, find your service, go into its properties and make it automatic. Your service should now start with windows!
So, how was it? Simple, nah? You can now enjoy your application running on one of the greatest app server around, even on Windows!