10 Steps to deploy your RubyonRails Application on Windows Vista/2008 using Apache and Mongrel cluster

November 24, 2008

I wrote a similar blog post last year if you are interested on setting up a production server on Linux( BTW which made lot simpler by Passenger now and recommended way to do it). But there are some cases, we have to deploy to MS servers. In the following article lets us look at doing the same on a Windows 2008 Server (I think it works for windows XP or 2003 too).

If you are like me, you usually develop RoR applications on linux (I am talking about non-mac people, anyway…).  My setup is usually looks like this : XUbuntu + Netbeans  + Mysql/Sqlite3 + Apache + Phusion Passanger.  Assuming you got all your development done andi it’s called  “MyApp”.

On Windows 2008 Box

Step 1:  Install Ruby, gems and rails

Get Ruby 1.8.6 One-Click Installer (from http://www.ruby-lang.org/en/downloads/) and make sure to install gems.

Then installing rails is a breeze. Open a command line window and type

gem install rails –include-dependencies

Step 2: Install mongrel & mongrel_service

gem install mongrel
gem install mongrel_service

Step 3 : Install Apache and enable required modules

A. Get Latest msi version for windows (currently apache_2.2.10-win32-x86-no_ssl.msi) from http://httpd.apache.org/download.cgi

B. Install as a service.

C. Enable required modules by uncommenting these following line in httpd.conf file (which can be found at C:\Program Files (x86)\Apache Software Foundation\Apache2.2\conf\

if you followed the default installation)

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so

D. Start the server to makesure it starts and runs OK. If there is a problem with config file you would see an error.

Step 4 : Install database drivers

This really depends on your database. Remember that for sqlite3, if you are having trouble installing latest

drivers, try giving a version no as follows.

gem install –version 1.2.3 sqlite3-ruby

Step 5 : Create a skeleton rails application

I am assuming we are keeping our rails application in C:\railsapps\

So create the new rails application by changing to that directory and running

rails Myapp

The reason for creating a skeleton instead of just copying our application from Linux, is that the way some files are created on windows may not be same as linux.  So let it create a dummy app and test it to make sure, its able to pullup the welcome page.

Step 6 : Copy your application Files over to windows

The directories usually copy are

app/      all directories below
public/   any static files you created,  directories  stylesheets, javascripts and images.
config/   routes.rb, database.yml
db/        schema.rb

(Again, this is highly dependent on your application. May be you didn’t have any custom javascripts or stylesheets in that case you could skip public/ folder all together)

Step 7:  Load Database Schema into production db

rake db:schema:load RAILS_ENV=production

Step 8 : Create Multiple Mongrel Server Instances and start them as automatic

I am creating 3 instances here

mongrel_rails service::install -N mongrel_Myapp1 -p 3001 -e production -c c:\railsapps\Myapp
mongrel_rails service::install -N mongrel_Myapp2 -p 3002 -e production -c c:\railsapps\Myapp
mongrel_rails service::install -N mongrel_Myapp3 -p 3003 -e production -c c:\railsapps\Myapp

which creates three services (you could see them Start>>Administrative Tools >> Services Panel). By default the services are created as  “Startup Type — Manual”. Set that to “Automatic” so that they will start back again if you reboot the server.

Step 9: Setup Apache

In the apache’s httpd.conf file make sure to have these following lines

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

Then make your  conf/extra/httpd-vhosts.conf  File look like this

#——- Start of http-vhosts.conf file ———————————–

NameVirtualHost *:80
#Proxy balancer section (create one for each ruby app cluster)
<Proxy balancer://Myapp_cluster>
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMember http://127.0.0.1:3003
</Proxy>
<VirtualHost *:80>
DocumentRoot “C:/railsapps/Myapp/public”
ErrorLog “logs/Myapp-error.log”
CustomLog “logs/Myapp-access.log” common
<Directory C:/railsapps/Myapp/public/ >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RewriteEngine On
# Rewrite index to check for static files
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached pages
RewriteRule ^([^.]+)$ $1.html [QSA]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://Myapp_cluster%{REQUEST_URI} [P,QSA,L]
</VirtualHost>
#——- End of http-vhosts.conf file ———————————–

If you ever used apache’s mod_rewrite module then you dont need any explanation. If you don’t, then breif explanation wont make much sense to you either….

Becareful with name of Proxy balancer and make sure to use exact name in last RewriteRule (in the above case its Myapp_cluster), then you should be fine.

Step 10 :  Start the mongrel services and Restart Apache.

Now when you go to http://localhost/  you should be sent to one of the 3 servers of balancer and see the application.  If you see broken images or ugly layout, then it means you didnt copy my “http-vhosts.conf” file properly!  Have a careful look at <Directory> directive again. You would know.

Happy programming and happy holidays.  Ciao.

p.s.  If you want to run multiple rails applications on same server like MyApp1, MyApp2…., have a look at rails.conf I used in “Fedora production setup“. By adding another balancer and modifing RewriteRules littlebit, its not that tough.

About these ads

7 Responses to “10 Steps to deploy your RubyonRails Application on Windows Vista/2008 using Apache and Mongrel cluster”

  1. abhinav Says:

    I installed ruby on rails it displays an error.
    When i installed ruby and after that when i run the command in prompt gem update –system
    then it displays an error Bad file descriptor

    When i run the command gem install rails –include-dependencies then it shows the error could not find gem rails locally or in a repository…

    Can anyone tell me the solution for that???

  2. SA Says:

    Hi,

    A newbie question about Rails and support for concurrent requests.

    In the above example you setup up 3 mongrel server instances.

    Does this mean if you get 3 requests into the same Rails application at the same time, each request will be running in one of the 3 mongrel server instances?

    If a 4th request came at the same time then it will be blocked until one of the 3 mongrel server instances completes the request it is currently processing?

    Thanks!

  3. Divyesh Vadhwana Says:

    Yes that is true. mongrel can process one request at a time that is the main reason we need to implement mongrel clusters in Apache or IIS

  4. SA Says:

    Thanks for your confirmation. So what do folks do in production in the real world? Do do they create these mongrel clusters with 10s or 100s of mongrel service instances?

    BTW: Your instructions worked great!

  5. Robert Mayer Says:

    This was very helpful! But, I wanted to point out a minor typo you have in Step 3:

    The file is named httpd.conf, not http.conf. But, I’m sure most people will figure that out.


  6. Thanks Robert for finding the typo… I fixed it on step3 and also on step 9.

  7. Dino Says:

    Believe it or not – there are still customers out there who use Windos Server 2008. I recently found this very helpful. Thanks a lot!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: