Ruby on Rails with Apache on Debian 8

Updated by Elle Krout

Contribute on GitHub

View Project | View File | Edit File

Ruby on Rails is a rapid development web framework that allows web designers and developers to implement dynamic fully featured web applications. This guide deploys Rails applications using the Phusion Passenger or mod_rails method. Passenger allows you to embed Rails apps directly in Apache applications without needing to worry about FastCGI or complex web server proxies.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

    To check your hostname run:

    1
    2
    hostname
    hostname -f
    

    The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

  2. Have a working Apache server. Follow the Apache Web Server on Debian 8 guide if needed.

  3. Update your system:

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

Installing Passenger and Dependencies

  1. Install the system packages required for using Ruby, building Ruby modules, and running Rails applications:

    1
    sudo apt-get install build-essential libapache2-mod-passenger apache2 ruby ruby-dev libruby zlib1g-dev libsqlite3-dev
    
  2. Using the gem package manager for Ruby modules, install the fastthread gem:

    1
    sudo gem install fastthread
    
  3. Install Ruby On Rails:

    1
    sudo gem install rails
    

    If you require a specific version of Ruby On Rails, issue one of the following commands for the version you need:

    1
    2
    3
    4
    gem install rails --version 2.1.2
    gem install rails --version 2.2.2
    gem install rails --version 2.3.5
    gem install rails --version 3.0.4
    

    This will install the appropriate versions of all required packages including ruby, rack, and other dependencies needed for basic Rails development.

  4. (Optional) Install additional dependencies for your application, such as MySQL support:

    1
    2
    sudo apt-get install mysql-server libmysqlclient-dev mysql-client mysql-common
    sudo gem install mysql
    

    Any other dependencies can be installed mimicking the steps above: Install the needed packages, then the needed gem.

  5. Add rails to your $PATH environment variable. Make sure to replace VERSION with the version of Ruby you are running:

    1
    2
    ls /var/lib/gems
    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/VERSION/bin"
    

    The step above will only add this PATH to your current session. To retain the change persistently, add the PATH to your local \~/.bashrc file:

    1
    echo "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/var/lib/gems/VERSION/bin" >> ~/.bashrc
    
  6. In your website’s public folder start a new rails project, to ensure everything is properly configured. Replace application with your application’s name:

    1
    sudo rails new /var/www/html/example.com/public_html/application
    

Configuring Apache to Work with Passenger

If your Apache virtual hosts file(s) mimics the ones create in the Apache Web Server on Debian 8 guide, you will have a <VirtualHost> block containing a DocumentRoot value similar to /var/www/html/example.com/public_html/.

  1. Open the file in a text editor, and edit the DocumentRoot to reflect the public directory of your application:

    /etc/apache2/sites-available/example.com.conf
    1
    DocumentRoot /var/www/html/example.com/application/public
    
  2. Restart Apache to ensure all settings have been loaded:

    1
    sudo systemctl restart apache2
    

Deploying Multiple Rails Apps

There are a number of strategies for deploying more than one Rails application using Passenger. The simplest approach requires running multiple distinct virtual hosts configured as above to host a single Rails app each. Alternatively, you may host multiple Rails apps within a single virtual host. This section covers the latter option.

  1. Add RailsBaseURI directives that specify the path to your Rails application within the VirtualHost configuration as in the following example:

    /etc/apache2/sites-available/example.com.conf
    1
    2
    3
    4
    DocumentRoot /var/www/html/example.com/public_html/
    RailsBaseURI /app1
    RailsBaseURI /app2 
    RailsBaseURI /app3
    

    These directives configure Passenger to run three Rails apps on the example.com site at the three locations specified.

  2. Link the public/ directory of the application to a directory within the public_html/ directory:

    1
    2
    3
    ln -s /var/www/html/example.com/app1/public/ /var/www/html/example.com/public_html/app1/
    ln -s /var/www/html/example.com/app1/app2/ /var/www/html/example.com/public_html/app2/
    ln -s /var/www/html/example.com/app1/app3/ /var/www/html/example.com/public_html/app3/
    

Congratulations! You have successfully deployed Ruby On Rails applications with the Apache Web server and Passenger.

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.

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