Snapp Action

Monday, June 18, 2007

Setting up Ruby on Rails with Apache 2

This is a rough beginning, but mostly has my notes from my experience setting up Ruby on Rails and some of the gotchas I came across. This was several months ago and I am going off the notes I took from that time. Most of the components used have later versions which may or may not work in this configuration.

I am currently running Open SuSE 10, with Apache2 (prefork). I added Ruby 1.8.4, Ruby Gems 0.8.11, mod_fcgid 1.09, and fcgi (

I chose mod_fcgid over mod_fastcgi because I found the latter to be buggy and unworkable. Though the version I used was 1.09, the homepage ( mentions that they are all the way up to 2.1. After un-tarballing mod_fcgid, I needed to change the INCLUDE value in the Makefile to point to my apache2 include directories, so in my case it became:
INCLUDES=-I /usr/include/apache2 -I /usr/include/apache2-prefork

Then just do the usual
./configure --prefix=/usr
sudo make
sudo make install

Afterwards, I had an issue with the path fcgid was using to write to for its sockets under the apache web user. As it didn't have permissions to the original path, the fix was to put
SocketPath /tmp/fcgidsock
into httpd.conf. For me I added mod_fcgid.conf to the /etc/apache2/conf.d directory (which is picked up automatically) with the following values:
AddHandler fcgid-script .fcgi
SocketPath /tmp/fcgidsock
IPCConnectTimeout 10
IPCCommTimeout 60

At the location of my rails app, I made sure to change the user and group to one that could be used by apache with proper permissions to execute the dispatch.fcgi file in the public rails app directory. So,
chgrp -R www /path/to/rails
chown -R apache /path/to/rails

or whatever your user and group names may be. Also,
chmod -R 755 /path/to/rails

is most likely needed.

Next, I added the apache directives to my vhost.conf file at /etc/apache2/vhosts.d/vhost.conf. Alias, Directory, Location, and RewriteBase paths need to all match up in order to work. Here is a small example:
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot "/srv/www/htdocs/myhost"

SetEnv RAILS_ENV production
Alias /testrails/test /srv/www/htdocs/myhost/testrails/test/public
LogLevel debug

<Location "/testrails/test">
RewriteEngine On
RewriteBase /testrails/test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

<Directory "/srv/www/htdocs/myhost/testrails/test">
Options Indexes FollowSymLinks ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AddHandler fcgid-script .fcgi
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"


Now, moving to the rails app directory. The database.yml file needs to have the socket file added that mysql will use. So for instance, my production section looks similar to the following:
adapter: mysql
.... rest of database, login info
host: localhost
socket: /var/lib/mysql/mysql.sock

Even after all this, when I went to view it from the web, every once in a while I would get a random "lost connection to mysql server during query" error. It turned out that the Ruby MySQL bindings had some issues and by replacing them with more reliable mysql-ruby C bindings my problem was fixed. I installed them by running:
gem install mysql -- --with-mysql-include=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql

See for additional details on this issue.

From there you should be good to go. Good luck! Let me know how this tutorial can be improved.