I’ve been doing quite a bit of development recently using Ruby on Rails under Mac OS X. Unfortunately it can be a little tricky to get rails apps deployed nicely under Apache. The machine I’m using is an aging G4 with very little RAM so running rails apps in CGI mode is very slow! Initially I tried FastCGI, but I found it to be unreliable :(Â
After a bit of looking about on the net I came across SCGI ( http://www.mems-exchange.org/software/scgi/ ) and some code to use it to run rails. apps ( http://www.zedshaw.com/projects/scgi_rails/ ).Â
Installing these pieces is pretty straight forwardÂ
Â
1. download and install the SCGI apache moduleÂ
2. install the cmdparse gem (sudo gem install cmdparse)Â
3. install the highline gem (sudo gem install highline)Â
4. install the SCGI rails runnerÂ
5. configure your rails apps for SCGI by executing scgi_ctrl config in each rails app root folderÂ
Â
Make sure that each of your rails apps has a different port selected in the config/scgi.yaml fileÂ
Â
Next we need to update the Apache config file /etc/httpd/httpd.confÂ
Â
LoadModule scgi_module libexec/httpd/mod_scgi.soÂ
AddModule mod_scgi.cÂ
Â
<IfModule mod_scgi.c>Â
# matches locations with a dot following at least one more characters, that is,Â
# things like *,html, *.css, *.js, which should be delivered directly fromÂ
# the filesystemÂ
<locationmatch \..+$>Â
# don’t handle those with SCGIÂ
SCGIHandler OffÂ
</locationmatch> Â
</IfModule>Â
Â
Alias /app1 “/Library/WebServer/RailsApps/app1/public”Â
SCGIMount /tbgmon 127.0.0.1:9999Â
<directory “/Library/WebServer/RailsApps/app1/public”>Â
Options +FollowSymLinksÂ
Order allow,denyÂ
allow from allÂ
</directory>Â
Â
Alias /app2 “/Library/WebServer/RailsApps/app2/public”Â
SCGIMount /app2 127.0.0.1:9998Â
<Directory “/Library/WebServer/RailsApps/app2/public”>Â
Options +FollowSymLinksÂ
Order allow,denyÂ
Allow from allÂ
</Directory>Â
Â
Now we start each of the SCGI server processes by running the scgi_ctrl start command in each rails app root folder, bounce apache using sudo apachectrl graceful and the rails apps should be up and running from http://localhost/app1/ and http://localhost/app2/Â
Â
That is only half of the story however. We need to have the SCGI server processes start when the machine boots. The way to do this under Mac OS X is to create a startup item. A startup item consists of a couple of files in a folder under /Library/StartupItems. One is a .plist file and the other is a shell script.Â
Â
Create a folder called SCGI under /Library/StartupItems and then create the file StartupParameters.plist with the following contentÂ
Â
{Â
Description = “SCGI”;Â
Provides = (“SCGI”);Â
Uses = (“Web Server”);Â
}Â
Â
Next create a file called SCGI and paste in this textÂ
Â
#!/bin/shÂ
Â
##Â
# SCGIÂ
##Â
. /etc/rc.commonÂ
Â
StartService ()Â
{Â
    echo “Starting SCGI servers”Â
    Â
    pushd /Library/WebServer/RailsApps/app1/ > /dev/null ; /usr/bin/scgi_ctrl start ; popd > /dev/nullÂ
    pushd /Library/WebServer/RailsApps/app2/ > /dev/null ; /usr/bin/scgi_ctrl start ; popd > /dev/nullÂ
}Â
Â
StopService ()Â
{Â
    echo “Stopping SCGI servers”Â
    ps ax | grep scgi | grep ruby | awk ‘{print $1}’ | xargs killÂ
}Â
Â
RestartService ()Â
{Â
    StopServiceÂ
    StartServiceÂ
}Â
Â
RunService “$1”Â
Â
According to the SCGI rails runner docs there is an option to choose the root folder of the rails app, but I couldn’t get it to work. I suppose I should contact the author about it one of these days ;)Â
Â
Now with any luck the next time your machine reboots the SCGI servers should be started and Apache should forward requests on to your rails apps.Â