Ubuntu, Apache2, and Ruby on Rails with Passenger

This tutorial aims to explain how to get a Ruby on Rails site “deployed” on a local machine strictly for development purposes.

1: Install Apache

You can do this in several ways, but here is how I like to do it, because I need Apache to speak PHP too…

$ sudo apt-get update && sudo apt-get install lamp-server^ -y

(NOTE: the caret after ‘lamp-server’) is not a typo!)

If you don’t want the entire LAMP server stack, you can simply install apache:

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

Navigate your browser to http://localhost, and if you see something like:

Screenshot from 2014-01-07 16:56:32

…your server’s working.

2: Install Ruby and Rails

This one’s a bit more difficult; you can go the Ubuntu way and install Ruby from the Ubuntu package manager, but you’ll end up with an ancient version (by the standards of the Rails community). The best tutorial by far that I have found on this is at the link below:

Setup Ruby on Rails on Ubuntu 13.10

After following the steps in the GoRails link, the rest of this should go smoothly.

3: Install Passenger

Phusion Passenger is an excellent package that gives Apache2 the ability to run Rails applications. It is actually super simple to install:

gem install passenger && passenger-install-apache2-module

The second command (after the ampersands) takes care of checking for dependencies. It will give you instructions to follow in order to get passenger running; these are highlighted in red in the console output. Follow those instructions exactly, with one exception: if you see an error complaining about Apache2 not being compiled with a usable “MPM”, don’t worry; just hit “Enter” again to continue with the installation anyway.

Once the installation of Passenger is complete, you will see two important chunks of output in the console: the first is a few lines of code that you need to put into the Apache2 config, and the other is an example virtualhost setup.

Copy the lines that are meant to go into the Apache2 config using ctrl-shift-c. Then run:

sudo gedit /etc/apache2/apache2.conf

You can replace “gedit” with your editor of choice. Scroll to the bottom of the file and paste in the lines you copied. Also, you may consider putting ServerName localhost into this file as well if you are getting server name errors when you start Apache2.

You may also wish to copy the example virtualhost setup to a temporary file for use in a moment.

4: Virtualhost Setup

By default, Apache2 only has permissions to operate inside of “/var/www/”. This can be annoying, because we don’t want to have to use “sudo” every time we want to edit something in our projects. I tackle this problem in two ways:

1: $ sudo usermod -a -G www-data $USER
2: sudo ln -sT /home/$USER/path/to/my_project /var/www/my_project

The first command adds your user to the www-data group, meaning that group permissions applying to www-data will also apply to you. The second creates a symlink in /var/www called “my_project”. This symlink allows Apache2 to “see” what is in your project, which is the last thing we needed to do before adding in the virtualhost file for your project.

Create your new virtualhost file:

sudo gedit /etc/apache2/sites-available/my_project.conf

As always, replace “gedit” with your editor of choice.

Copy the following virtualhost configuration into your editor and change the document paths to lead to your project’s public directory through the symlink you just made:

<VirtualHost *:80>
    ServerName local.trackerx.com
    RailsEnv development
    DocumentRoot /var/www/my_project/public
    <Directory /var/www/my_project/public>
        AllowOverride all
        # MultiViews must be turned off.
        Options -MultiViews

You may also choose to change the environment variable to “production” or whatever else you have set up in your project.

Wrap It Up

This setup should allow you to view your projects without having to run the development server every time you want to get to work, and it allows you to put your projects in your home directory or wherever else you want to put them.

Note: you will probably have to restart Apache2 on a regular basis. When you install a new gem in your project, run:

sudo service apache2 restart

to get the server running again.

Happy coding!

Adam Nickle

About Adam Nickle

I'm a total nerd, intellectual explorer, number theory enthusiast, and computer science nut. I'll write about anything from math and programming to religion and science fiction, all of which play central roles in my life.
This entry was posted in Web Development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *