Barefoot Development

Installing Ruby on Rails with mod_fcgi for Apache 2

After a few hours of trial and error using advice from many different sites/posts, this is the process that I found successful in getting Ruby on Rails working with the Apache 2 fcgi module on linux. I hope this helps someone hang onto a few more hair follicles.

Before we begin, I can say that this was only successful when I did the setup in this order. Perhaps others have done it in a different way, but this worked for me. BTW, we're using Redhat Enterprise 3.

First, we'll install ruby.

curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.4.tar.gz
tar xvfz ruby-1.8.4.tar.gz
cd ruby-1.8.4
./configure --prefix=/usr/local
make
make install
cd ..


Just to make sure everything installed properly try this:

ruby --version

It should return something like this:

ruby 1.8.4 (2005-12-24) [i686-linux]

Now let's install ruby gems.

curl -O http://rubyforge.org/frs/download.php/5207/rubygems-0.8.11.tgz
tar xvfz rubygems-0.8.11.tgz
cd rubygems-0.8.11
ruby setup.rb
cd ..


Now we need to install the FastCGI development kit.

curl -O http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xvfz fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure --prefix=/usr/local
make
make install
cd ..


Allrighty then. We're moving now.

Install the bindings between Ruby and FastCGI

curl -O http://sugi.nemui.org/pub/ruby/fcgi/ruby-fcgi-0.8.6.tar.gz
tar xvfz ruby-fcgi-0.8.6.tar.gz
cd ruby-fcgi-0.8.6
ruby install.rb config
ruby install.rb setup
ruby install.rb install
cd ..


Next, install the mod_fcgi Apache 2 module.

curl -O http://fastcgi.coremail.cn/mod_fcgid.1.09.tar.gz
tar xvfz mod_fcgid.1.09.tar.gz
cd mod_fcgid.1.09


Check the Makefile to make sure top_dir points to the right directory. Edit if it doesn't.

make
make install
cd ..


It's finally time to install Rails.

gem install rails --include-dependencies

This will take a few minutes. Grab a refreshing beverage. If you really want to be entertained while improving your ruby skills, read why's (poignant) guide to ruby.

Time to get back to work. Before we go nuts trying to see if Rails and Apache are on speaking terms, let's make sure Rails' built in server, WEBrick works. We'll make a quick dummy application to try it out.

rails dummyapp
cd dummyapp
ruby script/server


You should see output similar to this:

=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-05-03 11:56:50] INFO WEBrick 1.3.1
[2006-05-03 11:56:50] INFO ruby 1.8.4 (2005-12-24) [i686-linux]
[2006-05-03 11:56:50] INFO WEBrick::HTTPServer#start: pid=8747 port=3000


Now, let's see what the application shows us. I usually do this in a new terminal window.

lynx http://localhost:3000

Hopefully, you can see a page that has "Welcome aboard, You're riding the Rails!" on it, along with links to documentation and other goodies. If you're not, begin abusing hair follicles. Assuming you did see it, quit out of lynx, then shutdown WEBrick in the other terminal by hitting ctrl-c on your keyboard.

Rails and Apache. Joanie and Chachi. Nick and Jessica. Peas and Carrots.

Let's make a super couple. To test Rails and Apache, we need to enter the appropriate lines into our Apache configuration file. I was doing this as an additional virtual host, so mine looks like so:


<VirtualHost 10.10.10.10:80>
ServerAdmin me@gmail.com
ServerName website.com
UseCanonicalName Off

# Set to development, test, or production
DefaultInitEnv RAILS_ENV production
# set to the "public" directory of your app
DocumentRoot /root/dummyapp/public

<Directory "/root/dummyapp/public">
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Options Indexes ExecCGI FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
AddHandler fcgid-script .fcgi
</Directory>
</VirtualHost>


Now that you've added the block to your Apache config, restart Apache and browse to you site. You should once again see the familiar "Welcome aboard, You're riding the Rails!"

And there was much rejoicing.

Sean Brown, Partner, Technology at Barefoot

Labels: , ,

1 comments

  1. Blogger Unknown said:  

    Thank you! I spent a day messing around on my own getting no where. Your instructions worked.

Post a Comment

« Home