The goal of this series of articles is to show how to deploy a production-level Rails server, using Ubuntu Server (I'm sure Debian will be fairly identical). This is the first in the series, and I will cover installing Ubuntu Server 7.1, installing and configuring Rails 2.0.2 , and installing MySQL 5 . After that, I will create a very quick Rails application to show that it is working.
The end goal of this series will be to have a pack of Mongrels (mongrel_cluster ) being balanced by Apache's mod_proxy_balancer . The second part of the series will be to setup the mongrel_cluster, and the third will be Apache.
I would greatly welcome any critical feedback if there are better methods to the ones I am showing.
FYI - I will be deploying this into a Parallels virtual machine, so your hardware requirements may differ. I will also go over the one issue with getting Ubuntu Server to work on Parallels; so if you are not using Parallels, please skip that step.
Installing Ubuntu
Naturally the first thing we need to do is install Ubuntu server . Since I'm installing onto a virtual machine, there is no need for me to burn the ISO to a disc first, but if you aren't you will need to take care of that. You could choose various download options . Personally I would recommend the BitTorrent option, the file transfer seems to usually be quite clean (at least I've had great success with it).
Once the installer starts up, choose to install Ubuntu. It will ask you to choose a language , and keyboard ...I would recommend manually selecting the keyboard, but that's because it doesn't detect very well through Parallels. You will then need to set your hostname , I will leave it as the default ubuntu :
After that you will need to partition the disc. This can be done in a variety of ways, and usually depends on what you're intending to do with the server. Since I'm just using a virtual machine, I'm going to use the whole disc as a single partition with a swap:
When it asks, select a username you will be using as the administrator, and set the password. This user will be a sudoer, so set everything appropriately.
When you get to the Software selection screen, choose nothing and just continue. We're going to manually setup everything...keeping the server as minimal as we can.
Then just continue on, and the server will reboot and be ready for use.
The Parallels Issue
Unless you're running Parallels! Which in that case you probably got the following error on boot:
Kernel panic: CPU too old for this kernel
If this is the case, go through the following steps, if not, skip to Installing Rails.
There seems to be something wrong with the linux-server kernel when it comes to running under Parallels. No matter, we can just quickly install another kernel.
Boot off your Ubuntu CD/ISO again, and this time choose to Rescue . Go through the steps as before, and when it asks you what volume you want to mount into a rescue shell, choose /dev/sda1 . Then issue the following commands:
$ apt-get update
$ apt-get install linux-386
$ apt-get remove linux-server
$ exit
Choose to reboot, (make sure you're rebooting to the hard drive and not the CD/ISO) and everything is working just fine now.
Installing Rails
You should be able to log in to your server with the username and password you set before:
The first thing we're going to need to install is Ruby , Rails is nothing without it; so issue the following command (and enter your password when prompted):
$ sudo apt-get install ruby rdoc irb libyaml-ruby libzlib-ruby ri libopenssl-ruby
And that's it, Ruby is installed.
Now we're going to install RubyGems , a package management system for Ruby. There are issues when installing it with apt , so we will manually install it; but it's a very simple install. Issue the following commands:
$ wget http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
$ tar -zxvf rubygems-1.0.1.tgz
$ cd rubygems-1.0.1
$ sudo ruby setup.rb
$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
$ sudo gem update --system
RubyGems is now installed.
We can now, using gem , install Rails with one simple command:
$ sudo gem install rails
Ruby on Rails is now up and running!
Installing MySQL
We need a database server, and personally I love and recommend MySQL. It is also easily installed, with one command:
$ sudo apt-get install mysql-server
It'll go through the installation automatically, but will prompt you to enter your desired password for MySQL's root user (not to be confused with the system's root user):
Choose an appropriate password, and then you are done. The MySQL server is running.
Testing What We've Done So Far
Time for something fun: writing a quick Rails application to test that all the parts we have just installed are working correctly.
First we need to create the Rails application. Starting in Rails 2.0, MySQL is no longer the default database system, so we will need to specify it when we create our app (make sure you are in the directory you want this application to be before issuing the following command):
$ rails -d mysql myapp
$ cd myapp
This will create a directory called myapp , which is where our newly created Rails application lives, and bring us into that directory for the following commands.
If you set a password for the MySQL root user, you will need to put that into the Database Configuration file. (Feel free to setup another user in MySQL if you do not want to use the root user; since I'm creating an isolated development machine, there is no need to setup another user.) Using vim , or another text editor of your choice, edit the database.yml file:
$ vim config/database.yml
Set the username: and password: variables under the development: section, write-out and close the file.
Next, we will need to create the databases, which now in Rails 2.0, we can create the Development, Test, and Production databases with one single rake command:
$ rake db:create:all
Now that we have our databases ready, let's create a new Scaffold (which will create the Model, Controller, and Views), and then update the database for the new model:
$ script/generate scaffold Story title:string body:text
$ rake db:migrate
This created a Story model and controller, which contains a title (which is a string), and a body (which is text), and migrated the database to know about the new model.
Now, start the server:
$ script/server
This will start the server process on TCP port 3000.
Point a browser at your server's port 3000 (ie. http://10.211.55.6:3000/), and you should see it up and running.
And, if you point to the stories controller (ie. http://10.211.55.6:3000/stories/), you can see everything is working as expected.
That's it for this part, I should post Part 2 within a week later today.
Click here for Part 2
Resources