setting up ruby on rails with apache

what is this tutorial?

This tutorial will teach you how to go from an empty Debian 7 (Wheezy) server to being able to running Ruby on Rails applications with Apache and Passenger. It is intended for those with a cursory knowledge of Apache and Rails but are apathetic towards server management.

I wrote this tutorial because other, comparable tutorials that only cover some of this are rather verbose, without having all the information in one place. Ironically, this section is pretty verbose, but the rest should be concise.

If you are running a different *nix than Debian 7, adapt the commands as appropriate. Ubuntu and Mint should be almost identical. Some of these numbers/versions may be out of date by the time you are reading this document. Adapt as necessary! No guarantees that it will still work, but it should...

You can click on the headers to hide anything in this tutorial you've already read or don't care about!

general server setup stuff

Run the following commands to make sure your server is up-to-date:

sudo apt-get update
sudo apt-get upgrade

install rvm

RVM is the Ruby Version Manager. It's fairly useful for managing your Ruby install. To install it, run the following command:

curl -L | bash -s

Afterwards, you will need to restart your shell.

install ruby

Use RVM to install Ruby. Run the following command:

rvm install 1.9.3

install rails

Use Ruby to install rails. Run the following command:

gem install rails

install and set up apache

Installing Apache is pretty straightforward. The only interesting part is that you have to install Ruby dependencies.

sudo apt-get install apache2
sudo apt-get install libapache2-mod-ruby
sudo service apache2 start

You can set your default apache page at /var/www/index.html if you want.

install and set up passenger

Phusion Passenger is what really enables Apache to work with Rails. Unfortunately, it's rather tedious to install, and everyone's experience with it seems to vary. Luckily, once you have it set up, you probably won't have to interact with it again. Run the following commands:

sudo apt-get install libapache2-mod-passenger
sudo gem install passenger
sudo passenger-install-apache2-module
sudo apt-get install libcurl4-gnutls-dev
sudo passenger-install-apache2-module
sudo service apache2 restart
sudo passenger-install-apache2-module

You may have noticed that I included the passenger-install-apache2-module three times. It will provide you with prompts and instructions. Follow these instructions. They are various dependencies that you may need. Run it until it says you are done. It can take awhile. Be strong!

adding a rails site to apache

We're almost done. Once you add a site to Apache, you'll be able to query it. I will be using my domain and project,, as an example. Remember to swap it out with your own!

Edit your sites available (I like to use vi(m) to do my editing, but you can use nano, emacs, ed, or whatever you want):

sudo vi /etc/apache2/sites-available/
Change the content of the file so it resembles the following (with your domain/project/username/etc.):
<VirtualHost *:80>
   DocumentRoot /home/cmiller/projects/middleendian/public
   ErrorLog /home/cmiller/logs/middleendian/error.log
   CustomLog /home/cmiller/logs/middleendian/access.log combined

If your projects directory does not already exist, make it. It does not have to be in this location, but it should match the earlier document. Then go to that directory, create your rails project, make your logs directory, and enable your site in apache

mkdir /home/cmiller/projects #if nonexistent
cd /home/cmiller/projects
rails new middleendian
mkdir -p /home/cmiller/logs/middleendian
sudo a2ensite
sudo service apache2 restart

Make a test index file in your public directory.

echo "testing" >> /home/cmiller/projects/middleendian/public/index.html

And assuming your DNS is set up, which is outside the scope of this tutorial, when you visit your domain name, a page that says "testing" and has no actual HTML should show up. Almost done...

making a deploy script

When you want to make a changes to your project, the will need to be deployed. The fastest way to do this is to have a deploy script in your project directory.

cd /home/cmiller/projects/middleendian
touch deploy
chmod u+x deploy
Then edit the contents of deploy so that they resemble the following
git pull; #if you have git for version control.
rake db:migrate; #handles database
bundle install; #installs relevant gems
RAILS_ENV=production rake assets:precompile; #sets up environment
touch tmp/restart.txt #makes the changes live

Run the delpoy script with ./deploy in your project directory.

That's all! You now have a maintainable Rails project that works with Apache living on your Linux server.