Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition


Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I'm going to use my own blog which is a Wordpress installation and serve a Rails application from the subdirectory "reader". Note, I'm not going to keep my Rails application in the document root of my Wordpress Blog, which is a PHP application and therefore anyone could browse the ruby source code :(. I'll keep it elsewhere on the filesystem and tell Apache about the location in the VirtualHost file.

You can visit the application by going to The application just parses a bunch of RSS feeds and displays them. It uses the Feedzirra library, which I've also written about

I'm using Phussion Passenger and Ruby Enterprise Edition from the folks at Mod Rails. Installing both Phussion Passenger and Ruby Enterprise Edition is simple and a very well documented process. However, you'll need to download and compile them from source. If you install Ruby Enterprise Edition (REE) it comes w/ Passenger so you won't need to download it separately. I recommend going with REE.

Installing Ruby Enterprise Edition

Here are the commands to download and install (change the X.X.X to the package you've actually downloaded).

tar xzvf ruby-enterprise-X.X.X.tar.gz
When you run the installer you'll be prompted for an installation location. Just hit enter to install in the default location. Follow the instructions from there and remember to copy/paste any code that they give you.

Official Instructions on installation for Passenger on its own are available here I've written about setting up an entire box w/ Passenger here If you already have Passenger installed and want to use REE just download and install REE and it'll recompile Passenger with REE support if you follow the instructions.

*** If you install REE you'll need to either link or reinstall all your gems. I linked the REE gem with the one in /usr/bin so that I can run gem install and REE will be aware of it.

ln -s /opt/ruby-enterprise-X.X.X/bin/gem /usr/bin/gem

The VirtualHost

If you have Passenger and REE successfully installed you'll need to modify your VirtualHost file and add Rails application information to it.

<VirtualHost *>
  # Normal virtual host info
  ServerAlias *
  DocumentRoot /var/www/

  # Rails info goes here
  Alias /reader /var/www/
  <Location /reader>
    PassengerAppRoot /var/www/
    RailsEnv production

The "Location" directive tells apache to forward requests starting with /reader to the directory /var/www/ which is the location of our Rails app. However, we need to add the PassengerAppRoot assignment so that it knows where the actual application lives.

Routing in Rails with Relative Path

And finally, your Rails application will need to be aware of the relative url prefix assigned to each route. Normally, you could do this w/ the :path_prefix at the individual route level like so

map.resources :feeds, :path_prefix => "reader"

This will work but you can add a line to your RAILS_ROOT/config/environments/production.rb file which will handle all your routes for you. This way you don't need to setup a relative path on your development environment work.

in RAILS_ROOT/config/environments/production.rb

config.action_controller.relative_url_root = '/reader'

Final Thoughts

Remember Ruby Enterprise Edition is the new Ruby Interpreter that your Rails web applications are using. If you have any gem or rake issues make sure that you're using the same interpreter that REE is using. Look in the location installation of REE "/opt/ruby-ent..." bin/gem or bin/rake and see if that helps. I just linked those to the standard /usr/bin/gem and /usr/bin/rake and everything worked fine.

Also I've read some people have trouble using the alias with passenger. This may be an older issue but works for me without a problem on Ubuntu (latest).

Here are some useful resources I found along the way...

Tagged w/ #alias #apache #feedzirra #installation #mod rails #passenger #passenger app root #path prefix #php #phussion #ree #relative url rails #routing #ruby enterprise edition #subdirectory #virtualhost #wordpressruby on rails

Apps I've Built

Easily change the location on your photos and videos
Click here for more info about the app
Easily change the dates and times on your photos and videos
Click here for more info about the app
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app