Westhost on Rails

By Aaron Gustafson

The Requisite Disclaimer: If you are on a different hosting company, I cannot guarantee this tutorial will work for you (though it may be helpful in figuring out your issues). As always, before you make changes to critical files (http.conf, .bashrc, etc.), do yourself a favor and make a backup copy; this tutorial offers assistance, but no warranty, so if you mess something up during the install, you’re on your own (but Westhost tech support should be helpful). Finally, this tutorial assumes some familiarity with the Site Manager from Westhost and some basic knowledge of the Linux shell.

Now that I’ve had to muddle through it a few times, I decided that, for my own sanity and to aid anyone else out there who may be going through the same thing, I would put together a comprehensive tutorial on how to install Ruby on Rails at Westhost. The major problems that arise and could trip you up are

FastCGI is not installed (and Rails will run incredibly slow without it), and

you cannot install applications in /usr/bin.

This tutorial will get you up and running with a very simple Rails page on your Westhost VPS server using Apache 1.3 with FastCGI. For simplicity’s sake, I am going to set up the test Rails app on its own hostname (demo.yoursitehere.com).

Required Site Applications

Before we get started, log into your Site Manager (it can be found at http://www.yoursitehere.com/manager) and make sure the following are installed:

MySQL 4.1.9 – because you’ll want a database

GNU Compiler Collection 1.0 – because we’ve got some compiling to do

While you’re there, under Domain Management, set up a new hostname for your domain using the following values:

hostname: demo

path: /var/www/demo

Now SSH into your site and we can get started.

Step 1: Collect & unpack

First of all, make sure you are in your home directory

[~]$ cd /home/your_username

and then we can start the downloading and unpacking. First we get Ruby (the latest is 1.8.3 as of this writing)

Step 3: Installing Ruby Gem Manager

We will be using the Ruby Gem Manager to install pretty much every other Ruby or Rails component we need, so it is next on our list. First of all, enter its folder

[~]$ cd rubygems-0.8.11

and then install it using Ruby

[rubygems-0.8.11]$ ruby setup.rb

So now we have the Gem Manager installed, but you’ll notice that typing gem at the prompt gives you an error that the system doesn’t know which interpreter to use for gem. We need to fix the gem bang line:

[rubygems-0.8.11]$ pico /usr/local/ruby/bin/gem

Change the first line to read

#!/usr/local/ruby/bin/ruby

Save your work when you exit and give gem a test run

[rubygems-0.8.11]$ gem list

We don’t have any gems installed yet, but that’s what we’re doing next.

Step 4: Install Rails

Now that we have the Gem Manager installed, the rest of the basic stuff is a snap. To install Rails, simply type the following

[rubygems-0.8.11]$ gem install rails --include-dependencies

This could take a little while, but should complete within a few minutes. In that short time, you can decide on the type of markup shorthand you’d like to work with (if any). Textile is a pretty popular one, but Markdown has its fans too. You could always go for both, but that seems a tad gluttonous.

If you want Textile, enter the following:

[rubygems-0.8.11]$ gem install RedCloth

If you want Markdown enter the following:

[rubygems-0.8.11]$ gem install BlueCloth

That was easy, but we’re not out of the woods yet. Unfortunately for us, Ruby 1.8.3 and the latest Rails (0.9.2 at present) do not see eye to eye on a certain format, but that’s okay, there’s an easy fix. In Ruby 1.8.3, “Format” is not defined, so we need to fix ActiveSupport’s clean_logger.rb. Traverse the system to get to the file (this can be done in a single command, but I broke it up for legibility):

Open web browser to http://www.yoursitehere.com:3000/ and look what you did. Congratulations, you’ve put Ruby on Rails!

Step 5: Getting FastCGI up on Apache 1.3

OK, all of that stuff was pretty easy, but know things get just a little more complex. If you don’t want to run Rails through Apache (and there are plenty of reasons not to), feel free to stop now, but if you would like Rails to run like the rest of your sites, keep reading, but be warned that it gets very tedious from here on in.

First off, return to your home directory:

[demo]$ cd /home/your_username

We need to install the FastCGIDev Kit. To do that, enter its directory, configure and install it similarly to how we did Ruby:

You’ll notice I have a commented line for the production environment, when the site is ready to go out of development and into production, you just comment out the first line and uncomment the second line and you are good to go. Note: I do not recommend developing and deploying on the same box.

Processes: Why only one process? Well, 1 is usually enough for most applications. If you are getting really heavy traffic, you could bump it up to 2, but FastCGI can be a bit resource hungry. It is even worse if you run multiple FastCGI servers on a single box. For instance, according to a tech at WestHost, 4 server instances running a large number of processes (say 15) each will frequently cause over 400MB of RAM and 1GB of swap to be used on the server, resulting in the cyclic behavior of freeing and reallocating memory. This is very resource intensive and it causes the server’s load to spike, which is problematic for any other users on the same server (in a VPS situation).

Step 6: Let’s get rolling

Finally we are in a position to test our install. To do so, we’ll add a simple controller to our demo Rails install. We start by moving into the demo folder

[mod_fastcgi-2.4.2]$ cd /var/www/demo

and then we use the Generator to make us a controller:

[demo]$ ruby script/generate controller Say

That doesn’t do a whole lot since we have no actions. Let’s define an empty one:

[demo]$ pico app/controllers/say_controller.rb

Edit the file to read

class SayController < ApplicationController
def hello
end
end

and save as you exit.

Now that’s all well and good, but we get an error if we try to surf to http://demo.yoursitehere.com/say/hello because there is no view associated with it. That’s easy enough to remedy, we’ll create one.