Bringing Ruby on Rails with FastCGI into Mac OS X Server
Pages: 1, 2
Configuration Cauldron
Before we go any further, you have got a Rails app to deploy, haven't you? If not, you had better read up on some tutorials first and create one. Be mindful that your Rails app's public/dispatch.fcgi file has the right Ruby in the path. I edited mine and hardcoded it to /opt/local/bin/ruby, which is the path of the DarwinPorts Ruby installation.
I had a specific end state in mind when I wrote this guide. I had a VirtualHost configured on this Apache installation, and I wanted to install my Rails app against a specific URL that didn't actually exist in the DocumentRoot.
Let's say for argument's sake the host was http://www.hagus.net. I wanted to access my Rails app at http://www.hagus.net/railstest. But obviously I don't want my whole Rails app living in the DocumentRoot; I want it somewhere like /Users/hagus/src/railstest. How to make this happen?
First, we dive into httpd.conf. We need to add a few extra configuration lines to make sure FastCGI is completely ready to rock & roll:
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcgi
</IfModule>
For cleanliness, we wrap these files between an IfModule declaration, in case we disable FastCGI in the future for some reason. The two config lines are simple: we declare a temporary directory for the FastCGI process to write out various files; then we add a handler to tell Apache that files ending in .fcgi are FastCGI scripts.
Don' t forget to actually create the /tmp/fcgi_ipc directory if necessary! Give it wide open permissions too:
# mkdir /tmp/fcgi_ipc
# chmod 777 /tmp/fcgi_ipc
Before we go any further: be very aware that changes to the httpd.conf file and friends may be overwritten by Server Admin. Unfortunately this is a difficult situation, as Server Admin doesn't really let us edit the httpd.conf file in a granular enough fashion. So we're forced to hand-edit.
A workaround to protect your httpd.conf modifications is to set the "immutable" bit with the chflags command. In reality, if you're diving in and editing your httpd.conf file manually, you probably don't lean heavily on Server Admin anyway.
# chflags uchg /etc/httpd/httpd.conf
# ls -lo /etc/httpd/httpd.conf
-rw-r--r-- 1 root wheel uchg 33495 Mar 9 13:18 /etc/httpd/httpd.conf
Now, we must edit the VirtualHost directive for our host to finish the job. Add these lines inside the <VirtualHost> for your site (OS X Tiger stores its VirtualHosts in /etc/httpd/sites, in case you're having trouble locating them):
Alias /railstest "/Users/hagus/src/railstest/public"
<Directory "/Users/hagus/src/railstest/public">
AllowOverride all
Allow from all
Order allow,deny
</Directory>
We first declare an Alias. This gets me what I wanted: a URL on my website that actually terminates outside the DocumentRoot. We then use a standard Directory tag to set the AllowOverride preference. We need this set to allow .htaccess in the public directory to do its job. You could also add some HTTP authentication directives here too, if desired.
There's just one more bit that needs to be taken care of. We need to edit the public/.htaccess file of our Rails app. We must set the RewriteBase directive correctly so that requests are correctly routed to dispatch.fcgi. Locate the line that says RewriteBase, and uncomment it appropriately. In my case:
RewriteBase /railstest
We must also choose to use dispatch.fcgi rather than dispatch.cgi. This is a simple matter of changing the appropriate RewriteRule line from this:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
to this:
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Now give your web server a kick in the guts (pardon the Australian colloquialism):
# /usr/sbin/apachectl restart
And you should now be able to access your Rails app!
Final Thoughts
You should now have a Rails app living at http://www.yoursite.com/yourapp/. Your Rails app should be "teh snappy" thanks to the wonders of FastCGI.
Your Ruby, Rails, Gems, and FastCGI installations are being managed out of the DarwinPorts distribution system. Your Apache and MySQL are still managed by Mac OS X Server, but we have had to hand-edit httpd.conf. We also had to hand-compile the FastCGI Apache module.
On my own RoR installation, I have everything checked into Subversion, which is also available via DarwinPorts:
# port install subversion
So when I make a change in development (I use the excellent Locomotive all-in-one environment) and confirm that it works, I can just log into my production server and issue the checkout command to refresh my code. Simple and elegant, the way Ruby on Rails should be. :)
Luke Burton chipped his teeth on C++, and has lately sought refuge in the beautiful world of scripting languages like Ruby and Perl.
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 12 of 12.
-
Missing property file
2006-06-23 12:56:21 Shayaan_Siddiqui [Reply | View]
I set up the server
http://www.ical.name/demo
and if you click About then it says 404.
I looked at the log file and its trying to find a property file.... am I doing something wrong?
-
Dynamic FCGI
2006-06-17 06:55:06 JonathanWaddilove [Reply | View]
This was a great help in tracking down my problem! My environment is OS X 10.4.6 with Apache 1.3.
I am trying to get multiple Rails applications running under FCGI. I have elected (for now) to use Dynamic FCGI. I was getting "Permission denied: FastCGI: can't create (dynamic) server "/Users/Jonathan/Development/LocoProjects/Wine/public/dispatch.fcgi": bind() failed [/tmp/fcgi_ipc/dynamic/d29d7200146ea5e733d8dea665329310]"
It turns out that I also needed to change the permissions for the dynamic directory as well with:
chmod 777 /tmp/fcgi_ipc/dynamic
Hope this helps someone else. -
Dynamic FCGI
2006-12-10 06:29:34 BuildSmart [Reply | View]
/tmp/ is a temporary directory and upon restart it's contents is flushed so do you think that placing your fcgi cache in this location is wise?
if I'm not mistaken, you could get away with just /tmp since the dynamic will be autocreated when it starts and will have the correct permissions.
-
rb-rubygems
2006-06-05 23:45:15 asiafasthosts [Reply | View]
Hi,
When trying to "port" rb-rubygems, I get the following error:
tiger:~ root# port install rb-rubygems
---> Verifying checksum(s) for rb-rubygems
Error: Checksum (md5) mismatch for rubygems-0.8.11.tgz
Error: Target com.apple.checksum returned: Unable to verify file checksums
Any ideas how I get around this..?
Cheers,
Steve
-
Rails
2006-04-21 16:16:16 paul.sheila [Reply | View]
The rails install seems to be missing from this article. There should be a 'sudo gem install rails' after the port installs.
-
Lighthttpd
2006-04-11 00:45:02 philipsoares [Reply | View]
Truth is, I would like to move away from Apache httpd on OS X. It's eating up big memory - RSS 22344 /process.
Obviously just a wish still (and also not to the point of "not breaking" the setup). Crucial is the authentication mechanisms... -
Lighthttpd
2006-12-10 06:25:48 BuildSmart [Reply | View]
After seeing your post I built lightttpd and php-5.1.6 (on steriods) as a futility effort and see no reason why you can't use it as you wish.
You can access my lighttpd server at:
httpd://daleenterprise.com:88/info.php
httpd://daleenterprise.com:88/amavis-stats/
If enough interest exists I guess I could build a UB of lighttpd and make it available.
-- BuildSmart






!/usr/bin/env /opt/local/bin/ruby
worked great after that