Setting up multiple Rails apps on OS X using Apache and Passenger
Wednesday, October 20, 2010 at 6:15PM This is a post I thought could benefit people who develop on a Mac and want to use the OS Apache and Passenger to host their Rails projects. You can use passenger pref pane but for some reason this kept crashing my OS pref pane. I couldn’t figure out why this was happening so I removed it and set up everything manually (old school). Turns out it’s not that hard. In general I don’t create new rails apps too often so configuring by hand is not a big deal. Also, it’s likely I’ll forget how to do this by the time I need to add another Rails app so this post will hopefully be beneficial to my future self!
Step 1) Make sure you have installed Ruby, Rubygems, Passenger.
Also, make sure you have installed the Passenger apache module. Instructions are on the Phusion webpage. The instructions that are generated in the terminal are not entirely correct with regards to how you should configure Apache for multiple apps. Don’t let this throw you off, do everything else except the Rails app configuration stuff. That part is described below.
NOTE* Before we get to the configuration stuff; just a reminder to create copies of the files before you start editing them. Name them http-conf.old and hosts-old (for example). Just make sure you don’t have the old conf file end in “.conf” Apache will read any .conf file and load it.
Step 2) Configure Apache to handle named hosts and the one, or more apps your going to have it run.
To do this you’ll need to add a few configuration lines to your Apache httpd.conf file. You should find it under /etc/apache2/http.conf I always add the lines to the end of the file so it’s easy to locate. The file is quite long.
#This allows apache to have name based virtual hosts. can listen on the same Ip. by defualt it does not do this
NameVirtualHost *:80
<VirtualHost *:80>
ServerName registar.dev
DocumentRoot "/Users/brian/Rails/registar/public"
RailsEnv development
RailsAllowModRewrite off
<directory "/Users/brian/Rails/registar/public">
Order allow,deny
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName enotis.dev
DocumentRoot "/Users/brian/Rails/enotis/public"
RailsEnv development
RailsAllowModRewrite off
<directory "/Users/brian/Rails/enotis/public">
Order allow,deny
Allow from all
</directory>
</VirtualHost>
The first line tells Apache to accept named based virtual hosts. This will allow your apps to be accessible at http://myapp.dev instead of the usual http://localhost:3000. Much shorter, and more descriptive. You can see from the code above that I’ve got two apps I’m serving from here, one called Registar and the other called eNOTIS. The ServerName in the VirtualHost block configures how I refer to the app in the browser, the DocumentRoot sets where the apps public folder lives on my filesystem, RailsEnv sets the running environment, and I’m not too sure what the other lines do. I will have to do some more research and update the post. I’m guessing it’s got something to do with access permissions and such and where the request can originate from. Here’s the Apache docs on the config settings.
Step 3) Modify your host file
The hosts file is located in /etc/hosts on your OS X file system. This file is what is used by the OS to figure out where to find http://enotis.dev (in my case) on the network. There isn’t a DNS server out there telling the computer to that this is on my computer (nor should there be). The hosts file is a DNS shortcut to resolve the location (i.e. the IP) of named hosts. Here’s what mine looks like after editing it for the two apps I want to run.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost enotis.dev registar.dev
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
You can keep adding apps to the end of the 127.0.0.1 line, just follow the formatting above. I think the other lines are used by BonJour and such. More can be read about this file on the Apple webpage or by typing ‘man hosts’ in the terminal
Step 4) Restart Apache
We made some pretty big changes so you’ll need to restart Apache before they’ll take effect. To do that from the terminal type
sudo /usr/sbin/apachectl restart
note: see the update below if this causes an error when you run it
Step 5) View your Rails apps
At this point you’ve restarted Apache, and told it where to find your Rails apps. If you go to http://
HELP it doesn’t work!!!!
Well, this does happen from time-to-time. When I was figuring all this stuff out I ran into lots of problems. For a while Apache was serving up one app instead of two, Passenger started crashing, Apache started crashing and failing to restart, etc… etc… Use the logs on the system to help you figure out what is going on. Tail them as you make http requests in the browser to your apps URLs. There are two logs, accesslog and errorlog. Use them well.
tail -n 100 -f /var/log/apache2/error_log
Also take a look at any console message if you’ve hosed your config so bad it’s not doing anything at all. The app is called “Console”, spotlight search for it or look in your Apps/Utilities folder.
Brian Chamberlain
An update to OS X to 10.6.5 breaks the apachectl script. For example, the command sudo /usr/sbin/apachectl restart to give this error:
/usr/sbin/apachectl: line 82: ulimit: open files: cannot modify limit: Invalid argument
See this Apple support thread for more details: http://discussions.apple.com/thread.jspa?threadID=2644342&tstart=0
I fixed it on my machine by editing the apachectl file and changing the ULIMIT_MAX_FILES entry to
ULIMIT_MAX_FILES=""
OSX,
apache,
passenger,
programming,
rails,
server | in
Programming,
Rails,
Web 


