How to install and configure Ruby on Rails

Ruby on Rails is an open source web framework for developing database-backed web applications. It is optimised for sustainable productivity of the programmer since it lets the programmer to write code by favouring convention over configuration. This guide explains how to install and configure Ruby on Rails for an Ubuntu system with Apache2 and MySQL.

Prerequisites

Before installing Rails you should install Apache (or a preferred web server) and a database service such as MySQL.

Install rails

Once you have a web server and a database service installed and configured, you are ready to install the Ruby on Rails package, rails, by entering the following in the terminal prompt.

sudo apt install rails

This will install both the Ruby base packages, and Ruby on Rails.

Configure the web server

You will need to modify the /etc/apache2/sites-available/000-default.conf configuration file to set up your domains.

The first thing to change is the DocumentRoot directive:

DocumentRoot /path/to/rails/application/public

Next, change the <Directory "/path/to/rails/application/public"> directive:

<Directory "/path/to/rails/application/public">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        Order allow,deny
        allow from all
        AddHandler cgi-script .cgi
</Directory>

You should also enable the mod_rewrite module for Apache. To enable the mod_rewrite module, enter the following command into a terminal prompt:

sudo a2enmod rewrite

Finally, you will need to change the ownership of the /path/to/rails/application/public and /path/to/rails/application/tmp directories to the user that will be used to run the Apache process:

sudo chown -R www-data:www-data /path/to/rails/application/public
sudo chown -R www-data:www-data /path/to/rails/application/tmp

If you need to compile your application assets run the following command in your application directory:

RAILS_ENV=production rake assets:precompile

Configure the database

With your database service in place, you need to make sure your app database configuration is also correct. For example, if you are using MySQL the your config/database.yml should look like this:

# Mysql 
production:
  adapter: mysql2
  username: user
  password: password
  host: 127.0.0.1 
  database: app

To finally create your application database and apply its migrations you can run the following commands from your app directory:

RAILS_ENV=production rake db:create
RAILS_ENV=production rake db:migrate

That’s it! Now your Server is ready for your Ruby on Rails application. You can daemonize your application as you want.

Further reading