Wed, Sep 15, 2010
Using Phusion Passenger and Apache2 on Ubuntu with RVM and Gemsets

I’m not an Apache expert, but tonight I found myself setting up my development box with a pseudo-production environment. Why would I do this, you ask? Well, as overkill as it may seem, there’s just no better way to test AJAX calls, which have a nasty tendency to block in development and hang the server. Besides, it’s nice to have a development environment that closely mirrors production, no? This way, the only difference between my development world and the production one is the Rails environment itself. Not quite sure that’s totally necessary, but it makes me feel better about all of this.

As a side note, if I ever find more spare time, I want to address how we go about testing AJAX in our Rails apps. The process outlined below still feels like way overkill just for setting up a Rails-based JavaScript development world. Seems like there’s a gem waiting to be written, there.

Step 0: if your app is going to need SSL, you should get that installed first:

sudo apt-get install libssl-dev

Otherwise you’ll have to chdir into your Ruby source directory and build ext/openssl the hard way. If you’ve already missed this step, like me, then here’s how to do that:

colin@itchy:~/.rvm/src/ruby-1.9.2-p0/ext/openssl$ ruby extconf.rb && make && make install

OK, so the next thing you’ll want to do is install RVM by following the directions at rvm.beginrescueend.com/rvm/install/ .

Once RVM is set up, install the Ruby you plan to use as well as creating the Gemset you want to use with it:

rvm install 1.9.2
rvm use 1.9.2
rvm gemset create rails238-passenger
rvm gemset use rails238-passenger
gem install rails -v=2.3.8
gem install passenger

You’ll also want to make sure you have Apache 2 installed at this point. I’m on Ubuntu, so that looks a little like:

colin@itchy:~$ sudo apt-get install apache2-mpm-prefork apache2-prefork-dev libapr1-dev libaprutil1-dev

Finally, we’re getting to the good stuff. You’ve got all the dependencies, (at least, all the ones that I can remember – I’ll update this article if I realize anything has been omitted), so let’s get Passenger working with RVM!

Follow the guide at rvm.beginrescueend.com/integration/passenger/ or copy and paste this:

rvm use 1.9.2 --passenger
rvmsudo passenger-install-apache2-module

Passenger will advise you to modify your Apache 2 configuration files. So let’s do that. You can safely copy-and-paste the output from Passenger’s installer, with one exception:

PassengerRuby /home/colin/.rvm/bin/passenger_ruby

Obviously you’ll need to replace ‘colin’ with your own user name. The file to edit (in my case) was /etc/apache2/apache2.conf and the content to be pasted was put at the very bottom of the file.

After making this change, we need to edit the wrapper itself (~/.rvm/bin/passenger_ruby) to make use of the Gemset, because for whatever reason RVM doesn’t currently look at the current gem paths when generating this file. This is pretty easy, and here’s how mine came out:

#!/usr/bin/env sh
if [ -s "/home/colin/.rvm/environments/ruby-1.9.2-p0@rails238-passenger" ] ; then
  . "/home/colin/.rvm/environments/ruby-1.9.2-p0@rails238-passenger"
  exec ruby "$@"
  echo "ERROR: Missing RVM environment file: '/home/colin/.rvm/environments/ruby-1.9.2-p0@rails238-passenger'"
  exit 1

Now there’s just the regular Apache configuration to take care of. Exit that file and then:

colin@itchy:~$ sudo vi /etc/apache2/conf.d/virtual.conf

Here are the contents of that file:

NameVirtualHost *

Easy, huh?

You’ll now want to edit /etc/apache2/sites-available/default and change

<VirtualHost *:80>


<VirtualHost *>

because otherwise it’ll cause problems with the NameVirtualHost directive, above. The point of all this is so that we can set up multiple virtual hosts, eventually running multiple Rails applications – because this is a development box, remember? (Personally, I’m running a lot of Rails apps on mine.)

OK, so now it’s time to create a new virtual host. Create a file called /etc/apache2/sites-available/rails-app (hopefully you’ll give it a more meaningful name than ‘rails-app’ but that’s up to you) and fill it with:

<VirtualHost *>
  ServerName rails-app
  DocumentRoot /home/colin/sites/rails-app/public
  <Directory /home/colin/sites/rails-app/public>
    Options Indexes FollowSymLinks
    AllowOverride all
    Order allow,deny
    allow from all
    RailsEnv development

Next, we need to map the rails-app to our localhost IP address by editing /etc/hosts and adding:     rails-app

Now there’re just a few more steps before we can breathe a sigh of relief: enable the site within Apache. This is as easy as:

sudo a2ensite rails-app
/etc/init.d apache2 reload

It might complain that the document root does not exist. If so, it’s time to create it. You could create the Rails app directly within ~/sites, but that seems so disorganized. I like to keep my projects in ~/projects/rails/whatever, so I typed something like this:

cd ~
mkdir sites
cd sites
ln -s ~/projects/rails/rails-app ~/sites/rails-app

Now you should be able to visit rails-app in your browser and see your Rails application in all its magnificent glory! Hope this helped, and if I’ve missed anything please be sure to let me know. This was almost entirely from memory because, well, who thinks to document this stuff until after the fact?!

Here are the links I’ve still got open. Hopefully, even if this post wasn’t helpful, these will be:

Good luck!

Please log in if you wish to leave a comment.