Ruby on Rails (with FastCGI) Howto

copyleft by rig, goldenratioATgmailDOTcom

v0.9, 27 November 2005

This is a small howto guide that will help you get Ruby on Rails installed on a shared web host (I use Hard Hat Hosting, but the instructions here should apply to most shared hosts). Once you get Ruby on Rails installed, it's quite simple to use the Ajax on Rails technique as well.

At the end of the howto I'll address FastCGI support for Apache as well. This assumes you are comfortable working on your host's shell through ssh. Some familiarity with compiling and installing software on Linux is assumed also.

Installing Ruby

I'd suggest creating a directory for all of the software you're going to be downloading and installing. I created a directory named "temp" at the root of my filesystem, but you can use whatever name and location you wish.
cd /
mkdir temp
chmod 775 temp
Now let's download, configure and install Ruby. You should download the latest stable source tarball for Ruby from here. I use wget to download files straight to the shell -- you can use another method if you wish but I'll use wget for this howto since it is very straightforward:
cd temp
tar -zxvf ruby-1.8.2.tar.gz cd ruby-1.8.2
One of the "tricks" needed for installation on a shared web host is that you often need to specify an install directory, because sometimes you don't have permissions to install things in their normal directories. You'll notice this throughout the howto. So let's run through the "configure" script for Ruby, specifying a directory inside "/usr/local" for installation:
./configure --prefix=/usr/local/ruby --exec-prefix=/usr/local/ruby
That should run successfully, leaving you with a Makefile that's ready to compile:
make install
Ruby is now installed!

Before we continue, you'll want to make a symbolic link to Ruby somewhere in your local path:

ln -s /usr/local/ruby/bin/ruby /usr/local/bin/ruby
We'll need some other sym links later, but this should get us started.

Installing RubyGems

RubyGems (also called "Gem") is a package management tool that lets you easily install extra packages and libraries for Ruby. We'll need Gem to install the Rails package. You can download the latest version of RubyGems here.
cd /temp
tar -zxvf rubygems-0.8.10.tgz
cd rubygems-0.8.10
Now let's install RubyGems. You'll use Ruby to call the setup script:
ruby setup.rb
You should see "Successfully built RubyGem" after that finishes. We'll need to make one edit so that Gem knows where to find our custom Ruby installation. Open the file "/usr/local/ruby/bin/gem" in your favorite text editor (such as vi) and edit the first line so that it looks like this:
Also, let's add a few more symlinks:
cd /usr/local/bin
ln -s /usr/local/ruby/bin/erb erb
ln -s /usr/local/ruby/bin/eruby eruby
ln -s /usr/local/ruby/bin/gem gem
ln -s /usr/local/ruby/bin/gem_server gem_server
ln -s /usr/local/ruby/bin/testrb testrb
ln -s /usr/local/ruby/bin/update_rubygems update_rubygems
That's it for RubyGems!

Installing Rails

Now that we have Gem installed, getting Rails up is super easy. Here's the skinny:
gem install rails --remote
Holy catfish that was easy!

Now that you have Ruby and Rails up and running, you'll want to test it out, probably with a MySQL database. Since you came here for Ruby on Rails installation instructions, presumably you already have a database project in mind. If not, I'd highly suggest this Rolling with Ruby on Rails article which creates a sample cookbook application.

Since we've already installed Ruby, Rails, and MySQL (MySQL is installed through your Hard Hat control panel if you haven't already installed it), you can start on page 2 of the article.

Adding FastCGI (optional)

This step is entirely optional, but it will give you a performance boost, particularly when running larger Rails applications.

First you need to install the fastcgi libraries:

cd /temp
tar -zxvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure --prefix=/usr/local/fcgi --exec-prefix=/usr/local/fcgi
make install
Now you can install the fcgi gem (this is a long command which your browser may line-wrap here -- beware that the command is all on the same line):
gem install fcgi -r -- --with-fcgi-lib=/usr/local/fcgi/lib --with-fcgi-include=/usr/local/fcgi/include
You also need to install the FastCGI module for Apache. Make sure you have Perl installed on your shared host, you need it to run apxs!
cd /temp
tar -zxvf mod_fastcgi-2.4.2.tar.gz
cd mod_fastcgi-2.4.2
/usr/sbin/apxs -o -c *.c
/usr/sbin/apxs -i -a -n fastcgi
You may need to add the following block to "/etc/httpd/conf/httpd.conf":
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi
And finally, you need to configure your Rails project to use FastCGI. Edit the file "(your_rails_app)/public/.htaccess", changing the line:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
You'll need to restart Apache before the changes will take effect:
/etc/rc.d/init.d/apachectl stop
/etc/rc.d/init.d/apachectl start
That's it! Ruby on Rails is now using FastCGI to dispatch all requests!