Install Ruby on Rails with Apache on Debian 9

Updated by Jared Kobos Written by Linode

Contribute on GitHub

Report an Issue | View File | Edit File

Ruby on Rails with Apache on Debian

What is Ruby on Rails?

Ruby on Rails is a server-side web application framework. It maintains a curated set of components and a “convention over configuration” philosophy that makes it possible to develop applications quickly and without large amounts of boilerplate. This guide will show you how to deploy Rails applications on your Linode using Phusion Passenger. Passenger allows you to embed Rails apps directly in Apache applications without needing to worry about FastCGI or complex web server proxies.

Before You Begin

  1. Familiarize yourself with our Getting Started guide and complete the steps for setting your Linode’s hostname and timezone.

  2. This guide will use sudo wherever possible. Complete the sections of our Securing Your Server to create a standard user account, harden SSH access and remove unnecessary network services.

  3. Update your system:

    sudo apt-get update && sudo apt-get upgrade
    

Install Apache

  1. Install Apache and its dependencies:

    sudo apt-get install apache2 apache2-doc apache2-utils
    
  2. Copy the default site configuration file:

    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
    
  3. Disable the default site:

    sudo a2dissite 000-default.conf
    

Install RVM and Ruby

Ruby will be installed with the Ruby Version Manager (RVM), which makes it easy to install and manage different versions of Ruby on the same system.

  1. Install the mpapis GPG key:

    gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
    

    If this does not work, your system may not have dirmngr installed by default. Install it to correct the error:

    sudo apt install dirmngr
    
  2. Run the official RVM installation script:

    curl -sSL https://get.rvm.io | bash -s stable --ruby
    
  3. The installation process will output a command that must be run before RVM can be used:

    source /home/username/.rvm/scripts/rvm
    
  4. Check the requirements for rvm:

    rvm requirements
    
  5. Install a version of Ruby and set it as the default version for your system:

    rvm install ruby
    rvm --default use ruby
    

    If your project requires a different version of ruby, install that version explicitly:

    rvm install ruby-2.5.0
    rvm --default use ruby-2.5.0
    

Install Passenger and Dependencies

  1. Install Passenger and other required packages:

    sudo apt-get install build-essential libapache2-mod-passenger ruby ruby-dev libruby zlib1g-dev libsqlite3-dev
    
  2. Rails requires a working JavaScript runtime on your system in order to run. If you do not already have one installed, use Node.js:

    sudo curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
    sudo apt install nodejs
    

Install Ruby on Rails

  1. Use the Rubygems package manager to install Rails:

    gem install rails --version=5.1.4
    
  2. Move your Rails app to your Linode, or create a new app if you don’t have one yet. Replace example-app with a descriptive name:

    rails new example-app
    

Configure Apache to Work with Passenger

  1. Check the path that Passenger is using to access Ruby:

    sudo passenger-config about ruby-command
    
    Note
    Make sure that Passenger reports the version of Ruby that you installed with RVM. Normally RVM uses paths similar to ~/.rvm/wrappers/ruby-X.X.X/ruby.
  2. Open /etc/apache2/sites-available/example.com.conf in a text editor and edit it as follows. Substitute the path to your Rails app, path to your Ruby interpreter (from the previous step), hostname or IP address, and any other information as necessary.

    /etc/apache2/sites-available/example.com.conf
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    <VirtualHost *:80>
        ServerName www.example.com
    
        ServerAdmin webmaster@localhost
        DocumentRoot /home/username/example-app/public
        RailsEnv development
        PassengerRuby /path-to-ruby
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        <Directory "/home/username/example-app/public">
            Options FollowSymLinks
            Require all granted
        </Directory>
    </VirtualHost>
  3. Activate the Rails site:

    sudo a2ensite example.com.conf
    
  4. Restart Apache:

    sudo systemctl restart apache2
    
  5. Navigate to your Linode’s public IP address in a browser. You should see the default Rails page displayed.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Join our Community

Find answers, ask questions, and help others.

This guide is published under a CC BY-ND 4.0 license.