Ruby on Rails with NGINX On Debian 9

Select distribution:
Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

This guide has been deprecated and is no longer being maintained.

Ruby on Rails is a web framework that allows web designers and developers to implement dynamic, fully featured web applications. When deploying a Rails app in production, developers can choose from several popular app servers including Puma, Unicorn, and Passenger. This guide will use Passenger, because of its convenient integration with NGINX.

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. Follow the Getting Started and Securing the Server guides, and set the Linode’s hostname.

    To check the hostname run:

    hostname -f

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

  2. Update the system:

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

Install Dependencies

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

    sudo apt-get install build-essential dirmngr gnupg ruby ruby-dev zlib1g-dev libruby libssl-dev libpcre3-dev libcurl4-openssl-dev rake ruby-rack

Install Ruby

Use the Ruby Version Manager (RVM) to install Ruby. Be sure to install a Ruby version that is compatible with the version of Rails in your Gemfile. This guide will use Rails 5.1.4 and Ruby 2.4.2.

  1. Install the mpapis GPG key:

    gpg --keyserver hkp:// --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 | 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 Rails

Use the Rubygems package manager to install Rails. Replace the version below with the appropriate version for your app:

gem install rails -v 5.1.4

Install NGINX And Passenger

  1. Install NGINX:

    sudo apt install nginx
  2. Phusion hosts a repository containing the latest version of Phusion Passenger. To add this to the package manager, first install the Phusion PGP key:

    sudo apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
    sudo sh -c 'echo deb stretch main > /etc/apt/sources.list.d/passenger.list'
  3. Enable HTTPS support for APT:

    sudo apt-get install apt-transport-https ca-certificates
  4. Update the local package database and install Phusion Passenger:

    sudo apt-get update
    sudo apt-get install libnginx-mod-http-passenger

Enable Passenger Support and Start NGINX

  1. NGINX is now installed on the system, but support for Phusion Passenger is not enabled. As root, or with the sudo command, open the file /etc/nginx/conf.d/mod-http-passenger.conf and verify that the following two lines are present and uncommented:

    File: /etc/nginx/conf.d/mod-http-passenger.conf
    passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /usr/bin/passenger_free_ruby;
    If the file does not already exist, you will need to create it and add the lines manually.
  2. Restart NGINX:

    sudo systemctl restart nginx
  3. To verify that Passenger support has been installed and enabled correctly:

    sudo passenger-memory-stats

    If Passenger is running, a few running processes should be displayed under the “Passenger processes” section:

    ----- Passenger processes -----
    PID    VMSize    Private  Name
    14337  420.8 MB  1.1 MB   Passenger watchdog
    14340  559.3 MB  1.4 MB   Passenger core
    14345  292.5 MB  1.2 MB   Passenger ust-router

Install MySQL Support (Optional)

If the application deployed uses MySQL, install the database server by following our MySQL on Debian 8 guide. Once it’s installed and configured properly, issue the following command:

sudo apt-get install libmysqlclient-dev

Deploy Rails App

  1. Copy your Rails app to your Linode. Navigate to the app’s root directory and install any dependencies:

    cd railsapp
    bundle install
  2. Rails requires a JavaScript runtime. Install Node.js:

    sudo curl -sL | sudo -E bash -
    sudo apt install nodejs
    If your Gemfile already includes therubyracer, or you have another JavaScript runtime on your system, you can skip this step.
  3. Open /etc/nginx/sites-available/default in a text editor and remove default_server from the first two lines of the server block:

    File: /etc/nginx/sites-available/default
    server {
      listen 80;
      listen [::]:80;
       . . .
  4. Since you are using RVM, you will need to specify which version of Ruby should be used by Passenger:

     rvm use
     passenger-config --ruby-command

    The passenger-config command will generate several lines of output, similar to:

    passenger-config was invoked through the following Ruby interpreter:
      Command: /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby
      Version: ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
      To use in Apache: PassengerRuby /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby
      To use in Nginx : passenger_ruby /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby
      To use with Standalone: /home/username/.rvm/gems/ruby-2.4.2/wrappers/ruby /home/username/.rvm/gems/ruby-2.4.2/gems/passenger-5.1.11/bin/passenger start

    Copy the NGINX line for use in the next step.

  5. Configure a new site for your Rails app. Create /etc/nginx/sites-available/railsapp in a text editor and add the following content:

    File: /etc/nginx/sites-available/railsapp
    server {
      listen 80 default_server;
      passenger_ruby /home/path/to/ruby/installation;
      passenger_enabled on;
      root /path/to/app/public;

Set the server_name to the public IP address or FQDN of your Linode and replace the root path with the path to your Rails application. Paste the output of the passenger-config command to replace the passenger_ruby line.

  1. Create a symlink to sites-enabled to activate the new site:

    sudo ln -s /etc/nginx/sites-available/railsapp /etc/nginx/sites-enabled/railsapp
  2. Restart NGINX:

    sudo systemctl restart nginx
  3. In a web browser, navigate to your Linode’s public IP address. Your Rails app should now be live.

Next Steps

Now that your app is running, consider using build tools such as Capistrano, or continuous integration (CI) tools such as Travis or Jenkins, to speed up your deployment workflow.

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 page was originally published on

Your Feedback Is Important

Let us know if this guide was helpful to you.

Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.