Multiple ruby on rails applications under different folders served by Apache on Mac OS X

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. 

Join the Conversation

1 Comment

  1. I think that you really can judge people by the way they comment different stuff. Some people, even expressing negative thoughts, are still polite and they respect and understand other people. Some people are not even trying to be nice, they just don’t care. I think self-confident person will always act nice, no matter what other people do

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.