Starting Mongrel on Mac OS X at Boot or Reboot
Posted August 15th, 2006Let’s just say you wanted to display your new Rails app to a client and wanted to run it off your home Mac. I know, I know, who would ever want to do that? Well, with all the ways shared hosting a rails application can turn your hair grey, something this simple is very attractive.
Mac already has Apache running on port 80 (unless you turned it off or some other voodoo but we’ll assume you didn’t), so pick your favorite app server port. Mine was 8080 b/c I refuse to run Tomcat anymore. You can pick anything in the 8000s and be pretty safe to not run into any of the system ports.
Fire up your favorite text editor and copy/paste the following:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>PICKANAME</string> <key>ProgramArguments</key> <array> <string>PATH_TO_MONGREL_RAILS</string> <string>start</string> <string>-p</string> <string>8080</string> <string>-e</string> <string>production</string> <string>-c</string> <string>RAILS_APP_PATH</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>
Now there are a few places where you’ll have to fill in your own information. I’ve put them in UPPERCASE.
PICKANAME => Name your service. Any name will do (they recommend a reverse TLD, but it’s not necessary). Just make sure there aren’t any spaces in it. Maybe I had something else screwed up, but when I had a space in the name, it wouldn’t work right.
PATH_TO_MONGREL_RAILS => Open a command prompt and type:
which mongrel_rails
Whatever comes up is what you put in place of this. If you get something like:
no mongrel_rails found in...
then you need to install mongrel. Type:
sudo gem install mongrel
at the command prompt and then type:
which mongrel_rails
to get it’s location. Most likely it will be:
/usr/local/bin/mongrel_rails
8080 =>Remember when you picked your favorite port in the 8000s? Well, here is where you enter it in. Replace the ‘8080′ with your port number.
RAILS_APP_PATH => Since this is a web app, it should go in your Sites folder. Put it wherever you want, but that makes sense to me. If that is the case, then put something like:
/Users/yourusername/Sites/railsapp
in here, otherwise, type in the whole path to your railsapp.
Remember when you picked a name? Good, cuz you are going to save this file and name it whatever_name_you_picked.plist and save it to the /Library/LaunchDaemons folder. Now here’s where it get’s fun, enter:
sudo /bin/launchctl load -w /Library/LaunchDaemons/whatever_name_you_picked.plist
at the command prompt. Tada! Now you can open http://localhost:PORT_YOU_CHOSE and share your rails app!
A few things to note:
- Lingon makes life easier when you are trying to edit your system startup and scheduled items. Go pick it up, it’s free. It has a learning curve and I didn’t want anyone to get frustrated by it so I wrote up the manual process instead.
- In order for someone from the outside world to see your app you may need to modify some firewall settings. If you have a router (wireless or otherwise) with a firewall on it, add a port-forward to your Mac’s IP address for the port you chose above. On your Mac, open System Preferences, click on the Sharing icon, click on the Firewall tab and click the New… button (you may have to click the lock in the bottom and authenticate to get it active). Choose ‘Other’ for Port Name, type in your port number into the TCP and UDP Port Number(s) fields and type in ‘RAILAPPNAME Mongrel’ into the Description. Click the OK button and you should be all set.
That’s it! Let me know if you got off the path anywhere…






