Written by Sean Behan on Sun Jun 17th 2012

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 http://seanbehan.com/reader. The application just parses a bunch of RSS feeds and displays them. It uses the Feedzirra library, which I've also written about http://seanbehan.com/ruby-on-rails/installing-feedzirra-rss-parser-on-ubuntu-8/.

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. http://www.rubyenterpriseedition.com/download.html

Installing Ruby Enterprise Edition

Here are the commands to download and install (change the X.X.X to the package you've actually downloaded).
wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz
tar xzvf ruby-enterprise-X.X.X.tar.gz
./ruby-enterprise-X.X.X/installer
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 http://www.modrails.com/install.html I've written about setting up an entire box w/ Passenger here http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/ 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
  ServerName seanbehan.com
  ServerAlias *.seanbehan.com
  DocumentRoot /var/www/seanbehan.com/wordpress

Rails info goes here

Alias /reader /var/www/seanbehan.com/reader/public <Location /reader> PassengerAppRoot /var/www/seanbehan.com/reader RailsEnv production </Location> </VirtualHost>

The "Location" directive tells apache to forward requests starting with /reader to the directory /var/www/seanbehan.com/reader/public 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... http://robots.thoughtbot.com/post/159806388/phusion-passenger-with-a-prefix http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri http://www.modrails.com/documentation/Users%20guide.html#RailsBaseURI http://stackoverflow.com/questions/848258/server-prefix-and-rails-routes


Tagged with..
#alias #apache #feedzirra #installation #mod rails #passenger #passenger app root #path prefix #php #phussion #ree #relative url rails #routing #ruby enterprise edition #subdirectory #VirtualHost #Wordpress #Ruby on Rails

Just finishing up brewing up some fresh ground comments...