How to: Ruby on Rails + Ubuntu + Apache with Passenger

Do you have an Ubuntu installation and want to know what all this fuss about Ruby on Rails is? With Passenger from Phusion it's easy!

Make sure you have Ruby and all it's development necessities:

sudo apt-get install ruby-full build-essential

Then install Rubygems:

tar xvzf rubygems-1.3.4.tgz
cd rubygems-1.3.4
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem  
sudo gem update --system

Next you need to install Apache (if you don't already have it installed):

sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev

If you intend on using MySQL as your database backend then you will need the MySQL gem:

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient15-dev
sudo gem install mysql

Now you can use gem to install Rails:

sudo gem install rails

Install Phusion Passenger (an Apache module that lets you run Rails apps easily):

sudo gem install passenger
sudo passenger-install-apache2-module

The passenger-install-apache2-module script will guide you through what you need to do to get Passenger working. It should tell you to copy these lines into your /etc/apache2/apache2.conf:

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

Enable mod_rewrite for Apache:

sudo a2enmod rewrite

Now you should be able to start your Rails project! Rails will default to sqlite when creating a new project but you can tell it that you want mysql:

rails my_project -d mysql

This will create a folder in the current directory called 'my_project' with all the files needed to run your Rails app. Now you can add a vhost for you project.

Create a file in /etc/apache2/sites-available/ for your site (something like '') and insert this:

<VirtualHost *>
    # Change these 3 lines to suit your project
    RailsEnv development
    DocumentRoot /home/myuser/www/my_project/public # Note the 'public' directory

Enable your new site and restart Apache:

sudo a2ensite
sudo /etc/init.d/apache2 restart

You might also need to make an entry in your etc/hosts file and restart your browser:

That should be it