Easily switch between Rails development sites with Phusion Passenger
by Wynn Netherland
Tue, 01 Jul 2008 08:25:00 GMT was interesting enough to generate 8 comments so far
mod_rails Phusion Passenger is gaining some steam as it makes deploying Rails apps easier. Passenger also makes it easier to switch between your Rails projects during development.
Multi-tasking, Rails style
If you’re like me, you often find yourself flipping between two or three different Rails projects. Each time you flip, you do the same little dance in Terminal:
wynn$ cd projects/blog
wynn$ mate .
wynn$ script/server
You code along happily on the first project, knock out a couple of bugs, and then it’s time to switch. Either you stop your first mongrel server and dance the terminal jig all over again, or you just cmd+T a new terminal tab and crank up project two on a different port, like :3001 (and remember to specify the new port when you crank up your browser).
Phusion Passenger, not just for your production boxes
Passenger eliminates the need to start up your own mongrel server for each of your development Rails apps. Just like your PHP days, you simply set up an Apache virtual host for each app, and Passenger does the rest.
Step 1: Set up your host names
To set up your host aliases for each domain you want to use, edit your /etc/hosts file. Here’s mine:
127.0.0.1 beta.mindbites.local
127.0.0.1 mindbites.local
Step 2: Install Phusion Passenger
Assuming you have Apache2 installed (bundled with Leopard, an apt-get install away on Ubuntu):
sudo gem install passenger
sudo passenger-install-apache2-module
The install script will chug away for a few minutes. Once on the other side, you’re only a couple steps away from multi-tasking bliss.
Step 3: Set up your virtual host
On OSX, you’ll need to use Headdress or modify /etc/apache2/httpd.conf with your virtual hosts:
NameVirtualHost *
<VirtualHost *:80>
DocumentRoot /Users/wynn/projects/mindbites/public
ServerName mindbites.local
ServerAlias beta.mindbites.local
RailsEnv development
<directory "/Users/wynn/projects/mindbites/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
</virtualhost>
<VirtualHost *:80>>
DocumentRoot /Users/wynn/projects/blog/public
ServerName locomotivation.local
RailsEnv development
<directory "/Users/wynn/projects/blog/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
</virtualhost>
<VirtualHost *:80>
DocumentRoot /Users/wynn/projects/goalistics/public
ServerName goalistics.local
RailsEnv development
<directory "/Users/wynn/projects/goalistics/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</directory>
</virtualhost>
After initially setting up my virtuals, I discovered that only the first one worked. A quick look on Google pointed me to the NameVirtualHost * directive which solved the problem. Note that the DocumentRoot /Users/wynn/projects/goalistics/public directive points to the public folder within your Rails app, not the root. Second, you’ll need to include the directory overrides in the Directory block to allow Apache to serve up your static content. (I was shocked when my app was served up naked the first time).
Also note that the RailsEnv directive determines the RAILS_ENV for the application and is production by default.
Wrap-up
I’ve been really pleased with Phusion Passenger so far. I discovered one hiccup, though. Passenger does not seem to care for inline ERB code in your database.yml so I had to side step some dynamic configuration we were doing. Error pages from Phusion are much nicer on the eyes though than the standard Rails versions. Also, restarting Rails applications are as easy as bouncing Apache or:
sudo touch tmp/restart.txt
from the project root. Passenger is definitely worth a look on your development machine.
Comments
about 7 hours later:
Nice write up, Wynn.
I’ve been using this set-up on my Ubuntu machine for a few weeks and it has worked out great. I need to go add it to my Mac, now.
about 8 hours later:
Nice writeup. I need to try it out on my Ubuntu machine, although I must confess, I use my Windows machine for development.
about 10 hours later:
Fingertips developed a Passenger preference pane for Leopard, so you can create virtual hosts without the use of Headdress.
about 11 hours later:
@sander, Thanks for the tip! Nice blog, too. Subscrib’d
1 day later:
Nicely done Wynn.
I’ve switched some of our smaller sites to Passenger and its going really well in production. I’m keen to try this for development, especially for our designers.
7 days later:
Super easy on Leopard w/ default Apache2 install. That Passenger prefpane is nice, too. Thanks for this!
20 days later:
Installed Passenger just fine on Leopard with default Apache2 and gem installed Ruby on Rails. Only one problem:
Once I have all of it set up my default non-rails /Users/myusername/Sites site no longer works.
I get the following error: No route matches “myusername” with {:method=>:get}
I assume this means that it’s assuming that there’s a rails app in /Users/myusername/Sites/ when in fact there is not.
Anyone know of a fix or for something I should look out for?
20 days later:
@Alexis, where did you install your rails apps. Can you paste your vhost entries from your httpd.conf?
Have a take?