Development with Rails + Passenger (AKA mod_rails) on Mac

11 comments Comments

Andre
Andre
27
May

Passenger, AKA mod_rails

There are a number of posts (one, two) out there on getting Phusion's Passenger up and running on OSX (Leopard). I decided to give it a go, and was pleased to discover several things:

  1. Despite reports to the contrary, Passenger installed just fine with Leopard's built-in Apache (I'm running Apache 2.2.8).
  2. Setup is very easy, as advertised.
  3. My default doc root(~/Sites) is works exactly as it did before. I do some HTML and PHP work there, so it was key that it continue to work properly.

Since the install process itself is quite easy, I wanted to offer a few tips for utilizing Passenger in a typical dev environment -- i.e., what you need after you get your first Passenger-powered Rails app up and running.

Your Brain on Passenger

As you know, script/server starts your Rails app on a specific port. If you bounce around between a number of applications at any one time, you're probably used to either starting them on different ports, or control-c'ing your current mongrel, cd'ing to another app's directory, and script/server'ing again. This familiar pattern changes when you're running passenger. All your apps are available at any one time, as long as you have your vhosts configured.

If you're like me, you usually hit your currently running Rails app on http://localhost:3000. That also changes when you're running Passenger. Instead, you'll hit a unique URL for each app, which you've configured in /etc/hosts to just go to 127.0.0.1

Setting up a new app

I set up a lot of Rails apps in my dev environment. With Passenger, in exchange for the on-demand convenience of accessing any of your apps any time, there are a few additional setup steps to take whenever you introduce a new app into your dev environment.

  1. create your rails project as usual
  2. add a new vhost. I configure mine in /private/etc/apache2/extra/http-vhosts.conf
  3. add the host in /etc/hosts
  4. restart apache: sudo apachectl restart

Here's a vhosts example with two apps I'm running locally. You can set up as many apps as you want this way:

  <VirtualHost *:80>
    DocumentRoot "/Users/andre/projects/rails/hotspotr/public"
    ServerName dev.hotspotr.com
    ErrorLog "/Users/andre/projects/rails/hotspotr/log/error.log"
  </VirtualHost>

  <VirtualHost *:80>
    DocumentRoot "/Users/andre/projects/rails/shapewiki/public"
    ServerName dev.shapewiki.com
    ErrorLog "/Users/andre/projects/rails/shapewiki/log/error.log"
  </VirtualHost>

Two things to note here:

  1. The ErrorLog line is optional. If you don't include it, the error output for this app will go to /private/var/log/apache2/error.log. Not that that's bad, but you're probably not used to looking for Rails logs there.
  2. I decided to go with the convention of dev.[PRODUCTION_URL].com. You can use anything here, as long as it matches up with an entry in /etc/hosts (see below)

And here's an example /etc/hosts addition to match the two virtual hosts above:

  127.0.0.1       dev.hotspotr.com
  127.0.0.1       dev.shapewiki.com

That's it! Go to (for example) http://dev.hotspotr.com, and you're hitting you local development app. There is nothing to start and stop. The first request for any app you hit will take a moment. Subsequent requests will feel quite snappy.

Let's Set up some Aliases to Make it all Flow

Here are the aliases I added to my .bashrc file to give me quick access to everything I needed for a new, Passenger-centric workflow in my development environment:

# Use this in any RAILS_ROOT dir. That restart.txt file tells mod_rails to restart this app.
# You'll want to do this when (for example) you install a new plugin.
alias restart_rails='touch tmp/restart.txt'

# By default, your app's error log now goes here. Unless you configure your apps otherwise, 
# it's helpful to have an alias to take you to your error log quickly.
alias apache_logs='cd /private/var/log/apache2/'

# You'll be adding to your vhosts configuration everytime you introduce a new Rails app. 
# Might as well make it a shortcut
alias vhosts='sudo vi /private/etc/apache2/extra/httpd-vhosts.conf'

# Dito with hosts
alias hosts='sudo vi /etc/hosts'

# You'll need to restart apache whenever you make a change to vhosts. 
# You can also click System Preference->Sharing->Web Sharing, but this is quicker.
alias apache_restart='sudo apachectl restart'

Comments

  1. Ivan said 1 day later:

    I followed your instructions and it works alright for one app, however I can’t get it to work for two different apps. My two hosts configured in /etc/hosts are displaying the same virtual host. Any idea what might be the problem ?

  2. Grant Neufeld said 1 day later:

    @Ivan: Is the following line uncommented in /etc/apache2/extra/httpd-vhosts.conf:

    NameVirtualHost *:80

    and this line uncommented in /etc/apache2/httpd.conf:

    Include /private/etc/apache2/extra/httpd-vhosts.conf

    Also, if you’re using the built-in firewall (System Preferences > Security > Firewall), did you turn on the webserver (“Web Sharing”) in System Preferences -> Sharing so that it will open up port 80 in the firewall?

  3. Grant Neufeld said 1 day later:

    (sorry, my dash-greater-than arrows were turned into strike-though notation by the comment formatter)

  4. Ivan said 1 day later:

    Thank you very much Grant, I was missing the line “NameVirtualHost *:80” in the conf file I had setup for my virtual hosts. Everything works like a charm now.

  5. Thomas said 2 days later:

    I keep getting Forbidden to access / on this server after following this recipe… any ideas ?

  6. Tekin said 5 days later:

    Thomas: Are you running FileVault? If so, this might be causing permission issues -

    http://blog.phpguy.org/2008/04/26/apple-filevault-and-apache-http-server

  7. Thomas said 5 days later:

    Tekin: No I am not. But I can’t for all that’s holy get passed this “Forbidden” thing. It’s been 2 days!

    Do I have to explicitedly give permission for apache2 (www) to read/execute the directories I want to host?

  8. Tekin said 5 days later:

    I’m far from an Apache expert but it does seem like a permissions issue to me. Have you tried changing the permissions? Maybe the access/error logs will have a clue?

  9. Hongli Lai said 7 days later:

    “Do I have to explicitedly give permission for apache2 (www) to read/execute the directories I want to host?”

    Yes.

  10. Thomas said 7 days later:

    Hongli Lai: Now there´s someone who should know ;) Great show on Railsconf from what I can see in the summaries on the web.

    Next (and last) question is: How do I best give user www access to my custom hosting directories ? And why don´t all you others have this issue too ? (or is it just that I am a unix noob and it´s implicit in the recipe?).

    Greetings from Norway anyway. Great tutorial, great apache module, and great help.

  11. visa secured said 8 days later:

    Nice Site! http://google.com

(leave url/email »)