<em>Please digg this story to spread the word! Thanks!</em>
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:
<ul>
<li>Ruby 1.8.5</li>
<li>Ruby on Rails 1.2.3</li>
<li>Subversion 1.4</li>
<li>MySQL 5.x Server</li>
<li>Apache 2.2.x</li>
<li>Mongrel Cluster</li>
</ul>
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.
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:
<em>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.</em>
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.
I'll install ruby gems the conventional way (so I'm not going to use Ubuntu's packages here). Download the <ahref="http://rubyforge.org/frs/?group_id=126">latest Gems .tgz here</a>.
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.)
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!
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.
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.
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:
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':
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. myapp.com 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:
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.
You have two mongrel servers running, ready to handle incoming requests. But, you want your visitors to use 'myapp.com' 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:
<strong><ahref="http://digg.com/programming/Rails_production_server_setup_and_deployment_on_Ubuntu_Debian">Please digg this story</a> to help spread the word! Thanks a lot!</strong>
<em>Comments can be posted here or in my new and shiny <ahref="http://groups.google.com/group/ariejannet">Google Group</a>!</em>