Last updated

Rails production server setup and deployment on Ubuntu/Debian

Please digg this story to spread the word! Thanks!

Okay, this is a big one! This article will show you (and explain to you) how to setup a Ruby on Rails production server with Ubuntu 7.04 or Debian 4.0 and how to deploy your Rails application there.

First, what’s getting installed:

  • Ruby 1.8.5
  • Ruby on Rails 1.2.3
  • Subversion 1.4
  • MySQL 5.x Server
  • Apache 2.2.x
  • Mongrel Cluster
I assume that you have just installed a fresh system with Ubuntu Linux 7.04 or Debian 4.0. If you haven't, do so now! You don't need to install the "DNS" or "LAMP" server in Ubuntu. Just a minimal system is enough for this tutorial.

I’ll be deploy an imaginary Rails application named “myapp” which uses MySQL and is stored in Subversion. More on that later on.

Well, let’s get going and get that Ruby on Rails server ready.

Update your system

Before you do anything, use apt-get to update your system to the latest possible version.

1sudo apt-get update
2sudo apt-get dist-upgrade

This probably installs a new kernel (linux-kernel) so a reboot may be in order for optimal performance.

Enable SSH

Most people will want to have SSH on their server to login remotely. In this case I install both the server and client so you can SSH out if you need to:

1sudo apt-get install openssh-server openssh-client

You will now be able to login remotely wiht SSH.

You’ll need SSH later if you want to use Capistrano to deploy your Ruby on Rails application. In any case, SSH is a good thing to have around.


If you are serious about your Ruby on Rails server, you want to have Subversion around. Most deployment scripts pull the latest revision of your code from subversion. No configuration needed here.

1sudo apt-get install subversion

We only need the client on the production server. We’re not going to host Subversion repositories here.

Install MySQL Server

This is the first serious step you’ll have to take. Both Ubuntu 7.04 and Debian 4.0 come with MySQL 5.0.x.

1sudo apt-get install mysql-server mysql-client libmysqlclient15-dev

Be sure to set a password for the root MySQL user. Failing to do so will leave your database open for anyone who wishes to see.

1mysqladmin -u root -h localhost password 'secret'
2mysqladmin -u root -h myhostname password 'secret'

Make sure to replace secret with your actual password.

Try logging in to MySQL with your new password to make sure everything works okay.

1mysql -u root -p
2Enter password:

MySQL is in place now, so let’s get cracking at Ruby and Rails now.

Ruby, Gems, Rails

Installing Ruby is quite easy:

1sudo apt-get install ruby

You’ll now have Ruby 1.8.5. You will also need to install some other develoment package to help you build native Ruby Gems.

1sudo apt-get install make autoconf gcc ruby1.8-dev build-essentials

I’ll install ruby gems the conventional way (so I’m not going to use Ubuntu’s packages here). Download the latest Gems .tgz here.

2$ tar xvf rubygems-0.9.2.tgz
3$ cd rubygems-0.9.2/
4$ sudo ruby setup.rb
5$ gem -v

That’s it for the gems. Now, install Rails an all it’s dependencies:

1sudo gem install rails --include-dependencies

If you get an error message complaining that the ‘rails’ gem cannot be found include the –remote option.

Oh no! More gems!

Next, let’s install some essential Ruby Gems that will make your life quite a bit easier. Here we’ll install the following gems:

  • mysql - For good MySQL connectivity
  • capistrano - Just to have it handy when needed
  • mongrel - Rails server
  • mongrel-cluster - To operate mongrel clusters
1sudo gem install mysql capistrano mongrel mongrel-cluster --include-dependencies

You’ll be asked several times to choose a version for different gems. Always choose the latest available version for ‘ruby’. (Don’t choose win32. I don’t need to explain why.)

Test Rails and MySQL operability

Before you continue you may want to take some time to test Rails and MySQL. It’s not essential, but I recommend it because it will save you a lot of trouble later on.

Create a new Rails application in your homedir and a create the corresponding MySQL database. Also edit config/database.yml to reflect your root password!

1mysqladmin -u root -p create testapp_development
2$ mkdir testapp
3$ rails testapp
4$ cd testapp
5$ vi config/database.yml
6$ rake db:migrate

If you get any errors from that last command, check the previous steps. Normally, all should be fine and you can continue safely.

Most people configure a ‘socket’ in their config/database.yml for MySQL. Note that this socket is in different places for different distribution. Ubuntu and Debian keep it in: /var/run/mysqld/mysqld.sock. You may need to update your configuration in order to connect to the database.

Apache 2.2

The good thing about Ubuntu/Debian is that they both include Apache 2.2.x now. This branch of Apache includes the a balancing proxy, which allows you to distribute your workload over several Mongrel servers (in your mongrel cluster). I’ll come back to that later.

1sudo apt-get install apache2

Before you continue, enable several modules which we’ll be using later on.

1sudo a2enmod proxy_balancer
2sudo a2enmod proxy_ftp
3sudo a2enmod proxy_http
4sudo a2enmod proxy_connect
5sudo a2enmod rewrite

That’s it for now on Apache. Let’s move along.

Prepping the Rails application

Okay, let’s prepare the Rails application ‘myapp’ for deployment now, shall we?

First, create a production database on your server, and configure it in config/database.yml:

1mysql -u root -p
1create database myapp_production;
2grant all privileges on myapp_production.* to 'myapp'@'localhost'
3identified by 'secret_password';

The next step is to install capistrano. You can install it on your development machine as a gem, as demonstrated above. Next, apply capistrano to your application:

1sudo gem install capistrano
2cap --apply-to myapp

Take a look at myapp/config/deploy.rb. This file contains the configuration for the deployment of your application. Take special care of the following, here’s an example for ‘myapp’:

 1require 'mongrel_cluster/recipes'
 2set :application, "myapp"
 3set :repository, "{application}/trunk"
 4# We only have one host
 5role :web, ""
 6role :app, ""
 7role :db,  "", :primary => true
 8# Don't forget to change this
 9set :deploy_to, "/home/ariejan/apps/#{application}"
10set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"

As you can see, I’ve already included several lines that enable the use of mongrel, we’ll get to that next.

Make sure you adapt this file to your own needs. is the address of the server you’re going to deploy your application to. the mongrel_cluster.yml file will be created in a moment.

On the server, make sure you create the ‘apps’ directory. You can now setup a basic file structure for the deployment:

1cd myapp
2cap setup

On your server, you’ll notice that the /home/ariejan/apps/myapp directory was created, including some subdirectories.

If you are annoyed with entering your SSH password every time, create and upload your public SSH key to automate this. (I’ll write something up about that later on.)

Now, configure mongrel. For a normal setup, with moderate traffic, you can handle all traffic with two mongrel instances. The mongrel servers will only be accessable through ’localhost’ on the server on non-default ports. Apache will do the rest later.

In your rails app, run the following command:

1mongrel_rails cluster::configure -e production -p 9000 \
2-a -N 2 -c /home/ariejan/apps/myapp/current

The configuration file we saw earlier has been created. Check-in all new files in to subversion now, and cold deloy your application!

1cap cold_deploy

The deployment will checkout the most recent code from Subversion and start the mongrel servers.

After the deployment, migrate your production database and restart the mongrel cluster:

1cap migrate
2cap restart

To check that your application is running, issue the following command on your server. It should return you the HTML code from your app:

1curl -L

Configure Apache and the Balacing Proxy

You have two mongrel servers running, ready to handle incoming requests. But, you want your visitors to use ‘’ and not an IP address with different port numbers. This is where apache comes in.

Create a new file in /etc/apache2/sites-availbale named ‘myapp’ and add the following:

 2  BalancerMember
 3  BalancerMember
 7  ServerName
 8  ServerAlias
10  DocumentRoot /home/ariejan/apps/myapp/current/public
12  <directory>
13    Options FollowSymLinks
14    AllowOverride None
15    Order allow,deny
16    Allow from all
17  </directory>
19  RewriteEngine On
21  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
22  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
23  RewriteRule ^.*$ /system/maintenance.html [L]
25  RewriteRule ^/$ /index.html [QSA]
27  RewriteRule ^([^.]+)$ $1.html [QSA]
30  RewriteRule ^/(.*)$ balancer://myapps_mongrel_cluster%{REQUEST_URI} [P,QSA,L]
32  ErrorLog /home/ariejan/apps/myapps/shared/log/tankfactions_errors_log
33  CustomLog /home/ariejan/apps/myapps/shared/log/tankfactions_log combined

Now enable this new site in apache:

1sudo a2ensite myapp
2/etc/init.d/apache2 force-reload

In some cases you may need to make a small change to /etc/apache2/mods-enabled/proxy.conf and swap

1Order deny,allow
2Deny from all


1Order allow,deny
2Allow from all

That’s all, you can now access your app on!

Maintaining your application

Now, happily develop your application and make update (you check them in to Subversion). To update your web server run:

1cap deploy

If you made changes in the database, you may want to run a long_deploy:

1cap long_deploy

And if for some reason, your mongrel cluster dies, just restart it.

1cap restart

That’s it! Happy hacking :)

Please digg this story to help spread the word! Thanks a lot!

Comments can be posted here or in my new and shiny Google Group!