Use Unicorn and Nginx to Configure Ruby on Rails Applications on Ubuntu 14.04

Updated by Alex Fornuto Contributed by Vaibhav Rajput

Contribute on GitHub

View Project | View File | Edit File

This is a Linode Community guide. Write for us and earn $250 per published guide.


Ruby on Rails is a popular web-application framework that allows developers to create dynamic web applications. This guide describes how to deploy Rails applications on servers using Unicorn and nginx on Ubuntu 14.04.

Unicorn is an HTTP server, just like Passenger or Puma. Since Unicorn cannot be accessed by users directly we will be using nginx as the reverse proxy that will buffer requests and response between users and Rails application.

Before You Begin

Before starting this guide, make sure that you have read through and completed our Getting Started and Securing Your Server guides.

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.

  1. Before you install any package, ensure that your hostname is correct:

    1
    2
    hostname
    hostname -f
    
  2. Make sure your system is up to date:

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

Install Ruby

  1. Install Ruby dependencies:

    1
    sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev nodejs
    
  2. Download the latest version of Ruby. At the time of writing this article, the current, most recent and stable version is 2.3, but you can check for the latest version here:

    1
    wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.gz
    
  3. Unpack the tarball:

    1
    tar -xzvf ruby-2.3.0.tar.gz
    
  4. Move to the extracted directory:

    1
    cd ruby-2.3.0
    
  5. Configure and install Ruby from source:

    1
    2
    3
    ./configure
    make
    sudo make install
    

Install and Create a Rails Application

  1. Install Rails on the server using gem (the package management framework for Ruby):

    1
    sudo gem install rails
    
  2. Before creating your project, move to the home directory:

    1
    cd
    
  3. Create a new Rails project. You will be using example as your project name:

    1
    rails new example
    
  4. Move to the project directory:

    1
    cd example
    

Install and Configure Unicorn

  1. Install Unicorn on the server using gem:

    1
    sudo gem install unicorn
    
  2. Create the file config/unicorn.rb which contains the unicorn configuration and paste the following configuration in the file.

    /home/username/example/config/unicorn.rb
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # set path to the application
    app_dir git File.expand_path("../..", __FILE__)
    shared_dir = "#{app_dir}/shared"
    working_directory app_dir
    
    # Set unicorn options
    worker_processes 2
    preload_app true
    timeout 30
    
    # Path for the Unicorn socket
    listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
    
    # Set path for logging
    stderr_path "#{shared_dir}/log/unicorn.stderr.log"
    stdout_path "#{shared_dir}/log/unicorn.stdout.log"
    
    # Set proccess id path
    pid "#{shared_dir}/pids/unicorn.pid"
    
  3. Now, create the directories mentioned in the Unicorn config file:

    1
    mkdir -p shared/pids shared/sockets shared/log
    

    Please note that we are still in the Rails application directory.

Install and Configure Nginx

  1. Install nginx:

    1
    sudo apt-get install nginx
    
  2. We need to configure nginx to work as the reverse proxy. Edit the config file /etc/nginx/nginx.conf and paste the following configuration in the HTTP block:

    /etc/nginx/nginx.conf
    1
    2
    3
    4
    upstream rails {
    # Path to Unicorn socket file
    server unix:/home/username/example/shared/sockets/unicorn.sock fail_timeout=0;
    }
    

    Edit username and example with appropriate values.

  3. Remove the default nginx site configuration:

    1
    sudo rm /etc/nginx/sites-enabled/default
    
  4. Create new nginx site configuration file for the Rails application:

    /etc/nginx/sites-available/example
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    server {
    listen 80;
    server_name localhost;
    
    root /home/username/example;
    
    try_files $uri/index.html $uri @rails;
    
    location @rails {
       proxy_pass http://rails;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
       proxy_redirect off;
    }
    
    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
    }
    

    Make sure you change the username and example with the appropriate values.

  5. Create a symlink to nginx’s sites-enabled directory to enable your site configuration file:

    1
    sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled
    
  6. Restart nginx:

    1
    sudo service nginx restart
    

Start Unicorn

  • To start Unicorn in the development environment:

    1
    sudo unicorn -c config/unicorn.rb -E development -D
    
  • To start Unicorn in the production environment:

    1
    sudo unicorn -c config/unicorn.rb -E production -D
    

    Make sure you are in the application directory; otherwise, you will need to type in the whole path name.

  • To stop Unicorn, issue the following command:

    1
    sudo pkill unicorn
    

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.