Rails: Serving a Site With Apache Under OS X
Posted on January 31, 2007
Filed Under /dev/ruby | 632 views |
Almost all the Rails documentation assumes that you’ll be serving your app up using either WEBrick or lighttpd, neither of which are suitable for me since I use Apache for both development and on the live servers.
Unfortunatelly finding Apache info can be difficult so to that end, here’s what I’ve done to get my Rails app working with Apache under OS X.
First
Make sure the following modules are loaded and active in your httpd.conf file (possibly in /private/etc/httpd/httpd.conf? Use locate httpd.conf to find it):
LoadModule env_module libexec/httpd/mod_env.so
AddModule mod_env.cLoadModule rewrite_module libexec/httpd/mod_rewrite.so
DarwinPorts for Modules
If those modules are not already installed, follow these directions from Luke Burton’s Bringing Ruby on Rails with FastCGI into Mac OS X Server. It requires that DarwinPorts be working on your machine. Note that when I installed DarwinPorts the installer got to the point where it said “Less than a minute remaining” and it hung there for about twenty minutes before completing. If you see the DarwinPorts installer appear to hang, give it a good chunk of time. You can use top to see if it’s still installing itself. If you see gcc as one of the processes, its probably not hung but still working away.
(Between the instructions here and Luke’s instructions you should have a pretty decent manual for this. We primarily differ in the configuration of httpd.conf. I’d play along at home with both sets of instructions and choose the appropriate ones if I were you).
Second
Add the following to your active httpd.conf file:
<Virtualhost 127.0.0.1:80>
SetEnv RAILS_ENV development
ServerName www.yourappname.dev
DocumentRoot /Users/username/Sites/yourappname/public/
ErrorLog /Users/username/Sites/yourappname/log/apache.log<Directory /Users/username/Sites/yourappname/public/>
Options ExecCGI FollowSymLinks
AddHandler cgi-script .cgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</Virtualhost>
and:
<IfModule mod_fastcgi.c>
FastCgiIpcDir /tmp/fcgi_ipc/
AddHandler fastcgi-script .fcg .fcgi .fpl
</IfModule>
Third
Set the permissions on your Rails site properly. The following worked for me (via Bryan Thompson):
cd /Users/username/Sites
sudo chgrp -R www yourappname
cd yourappname
chmod 0775 db
chmod 0775 log
chmod 0775 public
chmod 0666 log/*.log
Fourth
My httpd.conf setup assumes that I want to use a Virtual Host to manage my site. My physical site lives in /Users/chris/Sites/myproject/ however I like being able to type http://www.myproject.dev into the browser to access my site (I use .dev for all local sites, on the assumption that it won’t become a TLD any time soon).
I’ve found that I also need to set this up in the Netinfo Manager (though quite frankly I’m not sure why it needs to be in Netinfo Manager and httpd.conf. If someone would like to explain the machinations behind all that, I’d be indebted).
So open Netinfo Manager (probably in your Applications -> Utilities folder) and under the machines item create a new entry. The easiest way to do this is to duplicate an existing item and then…
Set ip_address to 127.0.0.1
Set name to www.yourappname.dev
Set serves to ./local
Quit Netinfo Manager.
Fifth
Restart Apache: sudo apachectl restart
I recommend doing that from the command line rather than from the Sharing System Preference pane so you’ll be able to see any error messages that might arise if you’ve mistyped something or missed a step.
You should now hopefully be able to type http://www.yourappname.dev into your browser and see your Rails site.
If I’ve missed anything or you have a better or alternate way, please comment.
Helpful Resources
Installing Rails and Apache on OS X (Intel)
Bringing Ruby on Rails with FastCGI into Mac OS X Server
Comments
5 Responses to “Rails: Serving a Site With Apache Under OS X”
Leave a Reply
NetInfo Manager can be controlled via the shell. Here’s a simple script to do what you do above:
#!/bin/sh
if [ -z $SUDO_USER ]; then
echo “Run with sudo.”
exit 1
fi
if [ $# -ne 1 ]; then
echo “Usage: $0 ”
exit 1
fi
site=$1
niutil -create . /machines/$site
niutil -createprop . /machines/$site ip_address 127.0.0.1
niutil -createprop . /machines/$site name $site
niutil -createprop . /machines/$site serves ‘./local’
echo “$site added to NetInfoManager.”
This script was taken from code in my virtualhost.sh script found on my site (http://patrickgibson.com/virtualhost). It makes adding virtual hosts to Mac OS X for development sites a piece of cake.
What was interpreted as a tag in the example script above was removed (or hidden). Instead of:
if [ $# -ne 1 ]; then
echo “Usage: $0 ”
exit 1
fi
It should be:
if [ $# -ne 1 ]; then
echo “Usage: $0 [site name]”
exit 1
fi
(but the square brackets should be angled, as it is a required parameter)
One more note about Mac OS X’s lookup process, controlled by lookupd(8): If you are using an ISP that does a wild-card DNS for all hosts, you will find that your development URL won’t work, as Mac OS X by default queries the DNS server before it queries NetInfo Manager. This is most commonly a problem at hotspots in cafes, etc. To get around this, you can tell Mac OS X to always check with NetInfo Manger before trying to resolve with a DNS server:
With super-user privileges:
mkdir /etc/lookupd
echo “LookupOrder Cache FF NI DNS DS” > /etc/lookupd/hosts
killall lookupd
Exercise extreme caution when messing around with lookupd, as you can quickly and easily cause some serious problems for Mac OS X, as lookupd is a very important process.
I think with regards to to mucking about with lookupd I’ll leave well enough alone for now. It works at home and the office just fine enough for me but that’s a good point regardless.
I like that script a lot. Very useful indeed, thanks Patrick!