Install Ruby-on-Rails and Phusion Passenger (mod_rails) on Ubuntu 9.04

RailsI have my new Atom powered server running in the loft, now it's time to put it to use and run some of the Rails applications I've been tinkering with. The following is how I installed and configured Ruby-on-Rails on Ubuntu 9.04 Server (x86 64 bit).

1. Update Ubuntu before we start
$ sudo apt-get update
$ sudo apt-get upgrade

2. Check whether MySQL is already installed
$ mysql --version
mysql Ver 14.12 Distrib 5.0.75, for debian-linux-gnu (x86_64) using readline 5.2

MySQL was already installed so OK to proceed.

2. Install Ruby
$ sudo apt-get install ruby irb rdoc
$ ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

4. Install Ruby Gems (the Ruby package management system)
There are two ways to install Ruby Gems on Ubuntu, either through apt-get or by downloading the Gem archive and running the set up script. I tried the apt-get method as that looked quicker:

$ sudo apt-get install rubygems1.8
$ sudo gem update --system

Unfortunately the apt-get package disables the gem update --system and insists that you use apt to update instead. This might be more preferable to the Linux Sysadmin crowd but I prefer to keep everything pure Ruby. It's a few extra commands but nothing too bad.

$ wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
$ tar xzvf rubygems-1.3.5.tgz
$ cd rubygems-1.3.5/
$ sudo ruby setup.rb
$ gem -v
$ sudo gem update --system

5. Install and test Rails
$ sudo gem install rails
$ cd
$ rails hello
$ cd hello
$ script/server
...
/usr/lib/ruby/gems/1.8/gems/rails-2.3.3/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)

A quick Google of this error message revealed that I probably should have installed the Ruby OpenSSL library when I originally installed Ruby.
$ sudo apt-get install libopenssl-ruby
$ script/server

This time the Mongrel server started fine.

6. Install Passenger (mod_rails)
The Rails community seem to all be switching from Mongrel to Passenger, the mod_rails Apache plug-in module. I was keen to see how a Passenger deployment compares to Mongrel.
$ sudo gem install passenger
...
Failed to build gem native extension.

Apparently the libraries needed to build a Gem that contains native code isn't included with the main Ruby package, you need to install the Ruby Dev package too (something else I probably should have installed when I installed Ruby).
$ sudo apt-get install ruby1.8-dev
$ sudo gem install passenger
$ sudo passenger-install-apache2-module

Following the prompts and messages on screen it turned out I didn't have a number of Apache libraries. The passenger installer is very good and gives you the exact commands to run:
* To install Apache 2 development headers:
Please run apt-get install apache2-prefork-dev as root.
* To install Apache Portable Runtime (APR) development headers:
Please run apt-get install libapr1-dev as root.
* To install Apache Portable Runtime Utility (APU) development headers:
Please run apt-get install libaprutil1-dev as root.

I only needed to run the first command as the other two packages must have been pulled in as dependencies.


$ sudo passenger-install-apache2-module
...
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.4
PassengerRuby /usr/bin/ruby1.8

...

7. Adding Passenger to the Ubuntu Apache2 Configuration
Whilst you could just add the Passenger configuration to your apache2.conf file, the Debian/Ubuntu way is to have a separate file for each module. So create a new file called passenger.load in /etc/apache2/mods-available and add the lines provided by the Passenger installer above. Then run the a2enmod to enable the Passenger module. This little script actually creates links in the mods-enabled subdirectory which are then picked up by the main Apache configuration file.
$sudo a2enmod
Which module would you like to enable?
Your choices are: actions asis auth_anon auth_dbm auth_digest auth_ldap cache cern_meta cgid cgi dav_fs dav deflate disk_cache expires ext_filter file_cache headers imap include info ldap mem_cache mime_magic passenger php5 proxy_connect proxy_ftp proxy_http proxy rewrite speling ssl suexec unique_id userdir usertrack vhost_alias
Module name? passenger
Module passenger installed; run /etc/init.d/apache2 force-reload to enable.
...

To finish enabling your Passenger module you just need to tell Apache to reload.
$ sudo /etc/init.d/apache2 reload

8. Adding Your Rails App to Passenger
Again, you could just add your site to the Apache config file using a virtual host directive, but the Debian/Ubuntu way is to use a sites-available/sites-enabled set up, similar to the Apache modules set up. A default web site was configured using /etc/apache2/sites-available/default, which publishes the directory /var/www via Apache.

I wanted to make my Rails app available as a subdirectory off the server's domain, e.g. http://server.local/testapp. All you have to do is create a soft link from your Rails app's public directory to the /var/www directory and the tell Passenger to treat this direftory as a Rails app.

$ ln -s /usr/martin/testapp/public /var/www/testapp

Add the following to /etc/apache2/sites-available/default:
...
RailsBaseURI /testapp

If you want to use http://testapp.domain/ then you'll have to check the documentation on the Phusion Passenger site and create a new sites_available file.

Useful Resources
Blog entry on installing and configuring Passenger on Ubuntu 7.10.