Posted by & filed under Developer Blog.

By Aaron Gustafson. Updated by Jason Gill

While it is true that WestHost now offers Ruby on Rails, it’s only available on some of the packages and not all. If you are a cheapskate like me and want to add Ruby on Rails to the Personal Starter plan where RoR isn’t available, this tutorial is for you! This even works on packages that come with RoR for those of you who are unsatisfied with how limited WestHost’s install is. I’ve adapted Aaron Gustafson’s tutorial and updated it to be more relevant, more current, and (hopefully) more helpful for 2007.

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. This tutorial uses pico as a text editor, but vi is also an option and this tutorial assumes familiarity with inline text editors. I prefer emacs, but it’s not installed by default on WestHost servers.

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

  1. Westhost now runs Apache 2.0
  2. FastCGI is not an option in some packages (and Rails will run incredibly slow without it), and
  3. 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 2.0 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 5.0.27 – because you’ll want a database
  • GNU Compiler Collection 1.0 – because we’ve got some compiling to do
  • Perl 5.8.8 – At present I can’t remember why, but I had problems without this installed.
  • FastCGI 2.4.0 (If available with your package).

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

[yourusername][~]$ cd /home/your_username

or

[yourusername][~]$ cd ~

and then we can start the downloading and unpacking. First we get Ruby (the latest is 1.8.6 as of this writing), unpack it, and remove the archive:

[yourusername][~]$ wget http://rubyforge.org/frs/download.php/18421/ruby-1.8.6.tar.gz
[yourusername][~]$ tar -xvzf ruby-1.8.6.tar.gz
[yourusername][~]$ rm -f ruby-1.8.6.tar.gz

Next up is the latest Ruby Gem Manager (version 0.9.4 at present)

[yourusername][~]$ wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
[yourusername][~]$ tar -xvzf rubygems-0.9.4.tgz
[yourusername][~]$ rm -f rubygems-0.9.4.tgz

then the latest FastCGI Developer’s Kit (version 2.4.0 at present)

[yourusername][~]$ wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
[yourusername][~]$ tar -xvzf fcgi-2.4.0.tar.gz
[yourusername][~]$ rm -f fcgi-2.4.0.tar.gz

and, finally, the FastCGI module for Apache (version 2.4.2 at present)

[yourusername][~]$ wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.2.tar.gz
[yourusername][~]$ tar -xvzf mod_fastcgi-2.4.2.tar.gz
[yourusername][~]$ rm -f mod_fascgi-2.4.2.tar.gz

Step 2: Configure & Install Ruby

Starting in your home directory, get into the Ruby folder

[yourusername][~]$ cd ruby-1.8.6

We need to configure the source before we compile, letting it know that Ruby will be installed in /usr/local/ruby instead of /usr/local:

[yourusername][ruby-1.8.6]$ ./configure --prefix=/usr/local/ruby

Then go ahead and create the makefile and install Ruby.

[yourusername][ruby-1.8.6]$ make && make install

If you get OpenSSL errors, you can compile without (assuming you don’t want to use ssl security):

[yourusername][ruby-1.8.6]$ ./configure --prefix=/usr/local/ruby --without-openssl
[yourusername][ruby-1.8.6]$ make && make install

As we will be working with Ruby from the command line, we need it as part of our PATH, which means editing our .bashrc file:

[yourusername][ruby-1.8.6]$ pico /.bashrc

looking a few lines down from the top, you will see this line

export PATH="$PATH:/usr/local/apache/bin"

Change it to read

export PATH="$PATH:/usr/local/apache/bin:/usr/local/ruby/bin"

and save as you exit. Make sure your current session is using the new .bashrc file and then check your Ruby version (which will tell us if Ruby is working):

[yourusername][ruby-1.8.6]$ source /.bashrc
[yourusername][~]$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]

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

[yourusername][~]$ cd rubygems-0.9.4

and then install it using Ruby

[yourusername][rubygems-0.9.4]$ ruby setup.rb

give gem a test run

[yourusername][rubygems-0.9.4]$ gem list

We don’t have any gems installed yet (other than source), 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

[yourusername][rubygems-0.9.4]$ gem install rails --include-dependencies

If you get the error “Could not find rails (> 0) in any repository” you just need to run the following command:

[yourusername][rubygems-0.9.4]$ rm -f /usr/local/ruby/lib/ruby/gems/1.8/source_cache

now run

[yourusername][rubygems-0.9.4]$ gem update

and run the gem install command again.

[yourusername][rubygems-0.9.4]$ gem install rails --include-dependencies

This could take a little while, but should complete within a few minutes.

Now that we’ve done that, we can test our Rails install:

[yourusername][rubygems-0.9.4]$ cd /var/www
[yourusername][www]$ rails demo
[yourusername][www]$ cd demo
[yourusername][demo]$ ruby script/server

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 2.0

(If you have a hosting package from WestHost with FastCGI, you can just install it using the manager and skip step 5 altogether. Then again, if you have that package, you can probably install Rails from there also and you aren’t in need of this tutorial in the first place.)

OK, all of that stuff was pretty easy, but know things get just a little more complex. Be warned that it gets very tedious from here on in.

First off, return to your home directory:

[yourusername][demo]$ cd ~

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

[yourusername][~]$ cd fcgi-2.4.0
[yourusername][fcgi-2.4.0]$ ./configure --prefix=/usr/local/fcgi
[yourusername][fcgi-2.4.0]$ make && make install

Next on the agenda is getting Apache2 to understand FastCGI. To do this, we need to compile and install the DSO for Apache, which uses a slightly different process:

[yourusername][fcgi-2.4.0]$ cd ../mod_fastcgi-2.4.2
[yourusername][mod_fastcgi-2.4.2]$ cp Makefile.AP2 Makefile
[yourusername][mod_fastcgi-2.4.2]$ make top_dir=/usr/lib/httpd
[yourusername][mod_fastcgi-2.4.2]$ make install top_dir=/usr/lib/httpd

That put FastCGI in Apache’s modules directory, but not in the config file, so we’ll need to edit it.

[yourusername][mod_fastcgi-2.4.2]$ pico /etc/httpd/conf/httpd.conf

We need add the following in the list of already present modules

LoadModule fastcgi_module   modules/mod_fastcgi.so

Finally, create an instance of our demo app near the bottom of the httpd.conf file:

# FastCGI

FastCgiIpcDir /tmp/fcgi_ipc
FastCgiServer /var/www/demo/public/dispatch.fcgi -processes 1 -idle-timeout 60

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: Configuration

Go into Domain Management, set up a new Sub Domain Name: for your domain using the following values:

  • Sub Domain Name:: demo
  • Root directory for the new sub domain will be: /var/www/demo

Now configure your virtual host so that

ServerName demo.mysite.com
ServerAlias www.demo.mysite.com
DocumentRoot /var/www/demo/

becomes

ServerName demo.mysite.com
ServerAlias www.demo.mysite.com
DocumentRoot /var/www/demo/public/
ErrorLog /var/www/demo/log/server.log

Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny

Save the file as you exit and then test it to make sure you didn’t screw anything up before restarting Apache:

[yourusername][mod_fastcgi-2.4.2]$ apachectl configtest
Syntax OK
[yourusername][mod_fastcgi-2.4.2]$ apachectl restart

Hang in there, we’re almost done. To set up Rails to use FastCGI, we need to edit the .htaccess file in your public folder:

[yourusername][mod_fastcgi-2.4.2]$ pico /var/www/demo/public/.htaccess

In that file, change

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

to read

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Then save when you exit.

Finally, we need to set up the Ruby-FastCGI Gem

[yourusername][mod_fastcgi-2.4.2]$ gem install fcgi --with-fcgi-dir=/usr/local/fcgi

Step 7: 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

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

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

[yourusername][demo]$ ruby script/generate controller Say

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

[yourusername][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.

[yourusername][demo]$ pico app/views/say/hello.rhtml

Type out something simple, such as

Hello World

Hello from Rails!

The current time is

and then revisit http://demo.yoursitehere.com/say/hello. Now you’re rolling. Good luck and enjoy.

Step 8 : What? You want MySQL?

Okay, so you want to hook up Rails to your MySQL database now. To do this, there’s a few hoops you need to jump though. First of all, you’ll need to edit your database configuration file.

[yourusername][demo]$ pico config/database.yml

Fill in your database names for the development, test and production environments and the username and password for each. Then add the socket path:

development:

adapter: mysql
socket: /var/lib/mysql/mysql.sock
database: your_development_db
host: localhost
username: root
password: your_password

# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.

test:

adapter: mysql
socket: /var/lib/mysql/mysql.sock
database: your_test_db
host: localhost
username: root
password: your_password

production:

adapter: mysql
socket: /var/lib/mysql/mysql.sock
database: your_production_db
host: localhost
username: root
password: your_password

and save as you exit. Without the socket information, Rails will never find MySQL, which sucks.

(This information licensed under a Creative Commons License.)