How to set-up Ruby on Rails server on a Linux cloud (Ubuntu 10.04 LTS)

Setting up Ruby on Rails using passenger on a Linux server is a fascinating experience….provided everything works well :). I had to try several times before getting it right. I would like to share what I learned the hard way; so that, doing the same for you becomes a breeze.

I am assuming that you have some familiarity with Apache web server, Ruby on Rails and basic Linux commands. So let’s get started…

When you sign-up for a cloud server or a VPS, you get the server IP address, username and password. This is all we need! Log-in to your Linux server using the SSH protocol.

I have chosen Ubuntu 10.04 LTS (Lucid Lynx) as the Linux flavor. I prefer Ubuntu due to it’s simplicity compared to other Linux flavors.

Step 0: Update your Linux

Update your Linux source repository:

sudo aptitude update

In order to make sure our packages install correctly we need to set the locale information for Perl correctly:

sudo aptitude install language-pack-en-base

Now to keep the server secure, we will update the server using following command:

sudo aptitude update

Your Linux server is ready for some real action!

Step:1: Install Apache

You will be serving your website over the web and you need a web server. So, here is the command to install Apache web server:

sudo aptitude install apache2 apache2.2-common apache2-mpm-prefork apache2-utils libexpat1 ssl-cert

Now open the main apache config

sudo vim /etc/apache2/apache2.conf

At the bottom of the file add the following line

ServerName demo

In the above line change the text “demo” with your ServerName of your Cloud Server hostname or a FQDN. Remember for the sake of example I am using the Server Name  as “demo”

Once done, save the file and restart Apache gracefully using the following command:

sudo apache2ctl graceful

Now, if you navigate to your cloud server IP address, you will find the default Apache welcome screen “It works”


Step 2: Install MySql

Use this command to install MySql server:

sudo aptitude install mysql-server

When you are prompted for your root password, enter it and press OK

When you are prompted for your root password, enter it and press OK

After the installation is finished, it begins to run your server. Your MySql server is ready.

Securing MySql

The default MySql server set up is unsafe especially when your server is going to be exposed in public. It’s important to secure your MySql server installation. Let’s secure it by using the following command:

sudo /usr/bin/mysql_secure_installation

When you are asked for your current root password, enter the password you chose earlier and press Enter.

Because you have already chosen a root password, press N and then Enter.

You will be prompted to remove the MySQL anonymous users. For security reasons we want to do this. The text above the question explains this in more detail. Press Y and then Enter.

When you are asked if you would like to disallow remote login for the root user and only allow connections from the server itself, press Y and then Enter to keep the server secure.

MySQL ships with a default database called test. You do not need this and can delete it. Press Y and then Enter to delete the test database and it’s associated users.

This step reloads the user settings (called privilege tables) so that all user changes take effect. Press Y and then Enter to continue.

That’s it your MySql server is now secure and ready for production use.

Step 3: Install Ruby, Ruby Gems and Rails

Note: we will be installing Ruby 1.8.7 in this example. If you want to choose the latest version of ruby simply find out the latest version and replace it in the following command.

sudo aptitude install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl-ruby

Once the Ruby packages are installed, we need to symlinks from the installs to the locations every program would work:

sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby
sudo ln -s /usr/bin/ri1.8 /usr/bin/ri
sudo ln -s /usr/bin/rdoc1.8 /usr/bin/rdoc
sudo ln -s /usr/bin/irb1.8 /usr/bin/irb

Once done, use the following command to verify if everything is working correctly:

ruby -v

You will probably get something like the following in return:

ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]

Now let’s go ahead and install Ruby Gems from source. The reason why we are doing this is to ensure that we get the latest and most stable version of Ruby Gems onto the Cloud Server and the easiest way to do that is by installing from source.

Let’s create a folder called “sources” in our root directory

mkdir ~/sources

Now go inside the “source” directory

cd ~/sources

And download the latest version of Ruby Gems. At the time of writing this article, the latest version was 1.8.11


Once the file is downloaded, unpack  it and go inside the unpacked directory

tar xzvf rubygems-1.8.11.tgz
cd  rubygems-1.8.11

Now we can compile it:

sudo ruby setup.rb

Once the compile and install process is complete (it will take a couple of minutes or less) you will get the following message:

RubyGems installed the following executables:

If `gem` was installed by a previous RubyGems installation, you may need
to remove it by hand.

Now lets create another symlink

sudo ln -s /usr/bin/gem1.8 /usr/bin/gem

Once done, check the version of Ruby Gems installed by typing the following command

gem -v

We need to update the gems to the latest version

sudo gem update --system

Now the foundation is ready, so let’s go ahead and install Rails:

sudo gem install rails

You can now install any gems specific to your rails application. I will leave that up to you and lets’ move down to the last and the most important part of this installation i.e. Passanger a.k.a mod_rails installation.

Step 4: Mod Rails Installation

Mod Rails Installation can get tricky at times if you do not follow the instructions carefully. Let’s install the passanger Gem first:

sudo gem install passenger

Once completed, we need to install the Apache2 Module for passanger. Without installing this module Apache will never know that it should route the incoming requests to Passanger. So let’s go ahead and install it:

sudo passenger-install-apache2-module

Now, here is the surprise. The system will not allow you to install Passanger module for Apache.  Apache 2 module requires some dependencies before it can install the Passanger module. The good part is, it will tell us which dependencies are required and the command line to install those dependencies. So carefully copy/paste all the commands mentioned in the error screen and start installing the dependencies one by one.

Important note:  This is the area most susceptible to problems. If some dependencies do not get installed using the command mentioned in the error output; instead of using the usual “Aptitude” Linux command use the “Apt-get” command to install those dependencies. I could figure this out after wasting a lot of time!

Once all dependencies are installed, we can try installing the Passanger apache 2 module again:

sudo passenger-install-apache2-module

Everything should go well. At the end of installation, you will get an output similar to the following:

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6/ext/apache2/
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.6
   PassengerRuby /usr/bin/ruby1.8

Copy these lines and paste them at the end of your “apache2.conf” file.

Note: The above output is specific to every installation. So do not copy/paste the above output. Instead copy/paste the output which you got from your installation.

Once done, restart Apache:

/etc/init.d/apache2 restart

Bingo, you just installed a Ruby on Rails Server from scratch. Now start deploying and sharing your app to the world!

Thanks for coming this far….Let me know how your experience was following instructions in this article and if I can improve any aspect of it further.