Knowledge Base - FAQ

Question: How do I setup Ruby On Rails? (with FastCGI)

First things first!

The first thing you'll need to do is setup your ruby environment at Kattare. Please see this FAQ entry: Setting up your Ruby environment with RVM

A little about Ruby on Rails

Ruby on Rails can be great. It can also be a huge pain in the tailpipe. Ruby and Rails have proven over the last few years to have a very quickly changing code base and trying to keep up with it all is near impossible even for the seasoned pro. From version to version things WILL break. As we learn more, we'll share what we know here.

For now, this has been tested using our system default of Ruby 1.8.6 w/ Rails 0.13.1 and with a RVM environment of Ruby 1.8.7 w/ Rails 2.3.8. Yes, our system default is old, Kattare got into Ruby very early on, and many of our clients have deployed apps on the initial environment. Since then we've been stuck, because nearly every successive version breaks something integral. Which is why RVM is so nice and we're very happy to have it!

Things you'll have to track

In this example,

<domainname> = burnside.kattare.com
<document_root> = ~/burnside_kattare_com
<appname> = burnside_on_rails
<controller> = Hello
<username> = burnside
<action> = hello

On the CLI

Here's the play by play from the command line...

cd ~<username>

Use the rails command to setup the application base.

rails <appname>
Important! If using rails >= 2.3.3 the fcgi dispatchers are not included unless you specify --with-dispatchers to your rails command.
IE: rails --with-dispatchers <appname>

Link the application base into your document root.

ln -s ../<appname>/public <document_root>/<appname>

vi/pico <appname>/public/.htaccess

change dispatch.cgi to dispatch.fcgi, this will speed things up significantly.

If the .htaccess file does not exist, (rails >= 2.3.3?) here is what the content should be:
    AddHandler fastcgi-script .fcgi
    Options +FollowSymLinks +ExecCGI

    # Redirect all requests not available on the filesystem to Rails
    RewriteEngine On
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

    # In case Rails experiences terminal errors
    # Instead of displaying this message you can supply a file here which will be rendered instead
    # 
    # Example:
    ErrorDocument 500 /500.html
Next you need to generate a controller.

cd <appname>
./script/generate controller <controller> (my controller is 'Hello')
vi/pico app/controllers/<controller>_controller.rb ('hello_controller.rb' for me.)
class HelloController < ApplicationController
  def hello
  end
end
And then you setup a view:

vi/pico app/views/<controller>/<action>.rhtml
<html>
  <head>
    <title>Hello Rails World</title>
  </head>
  <body>
    <h1>Hello from Rails on Kattare!</h1>
    <p>The current time is <%= Time.now %></p>
  </body>
</html>

Test!

For my setup, my url came out to be: http://burnside.kattare.com/burnside_on_rails/Hello/hello

Yours would be something like:

http://<yourdomain>/<appname>/<controller>/<action>

One thing I noticed is that Rails startup IS NOT FAST. In fact, it's so slow that my initial request frequently times out because FastCGI gives up after about 30 seconds. Then if I wait a minute or so, it works.

When your changes are not getting picked up

Sometimes you may need to kill off the fast cgi processes to force the system to restart your app. To do this you execute these commands:

killall dispatch.fcgi
killall -9 dispatch.fcgi

If you get a Routing Error

You may need to add the following to your config/environment.rb:

After the "Fails::Initializer.run do" line I had to add:

config.action_controller.relative_url_root = "/burnside_on_rails"

For you it would be:

config.action_controller.relative_url_root = "/<appname>"

If your app is not picking up your gems

vi/pico <appname>/public/dispatch.fcgi

Change dispatch.fcgi to use gems by changing
    require File.dirname(__FILE__) + "/../config/environment"
    require 'fcgi_handler'
to
    require 'rubygems'
    gem 'fcgi'
    require 'fcgi'
    require File.dirname(__FILE__) + "/../config/environment"
    require 'fcgi_handler'


Last Modified: Jul 16, 2010


Kattare is Trustwave Certified and Safe Harbor Certified | Rated Safe For Kids by ICRA | All Rights Reserved Worldwide
Copyright © 1997 - 2012 Kättare Internet Services | Processed in 0.016 seconds by pyramid-06 in Corvallis, Oregon