Rails on Vista

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 http.conf file (which can be found at C:Program Files (x86)Apache Software FoundationApache2.2conf

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:railsappsMyapp
mongrel_rails service::install -N mongrel_Myapp2 -p 3002 -e production -c c:railsappsMyapp
mongrel_rails service::install -N mongrel_Myapp3 -p 3003 -e production -c c:railsappsMyapp

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 http.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.

 

 

Here is a quick add-on – How to set up a mongrel service to proxy from Apache!

First, you need the mongrel_rails gem, so install that using

gem install mongrel_rails

If we are creating a blog application, we want to set it up so that the URL we visit ishttp://blog/, so you need to open your hosts file, which can be found in C:\WINDOWS\system32\drivers\etc\, and enter the following line:

127.0.0.1 blog

Then you need to open your Apache config. Where you setup virtual hosts, enter this:

 

ServerName blog
ServerAlias *.blog

DocumentRoot C:/root/to/your/rails/app's/public/directory

RewriteEngine On

# Redirect all non-static requests to Mongrel
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$http://127.0.0.1:7000%{REQUEST_URI} [P,QSA,L]

# Custom log file locations
ErrorLog C:/root/to/your/rails/app/logfiles
CustomLog C:/root/to/your/rails/app/logfiles combined

 

Lastly, go into your command line. Navigate to your rails directory and run this command:

mongrel_rails service::install -p 7000 -N MyRailsApp

This will install the service. Restart Apache, and run:

net start MyRailsApp

And you should have a fully working proxy to mongrel!

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