Use Unicorn and Nginx to Configure Ruby on Rails Applications on Ubuntu 14.04
Updated by Alex Fornuto Contributed by Vaibhav Rajput
DeprecatedThis guide has been deprecated and is no longer being maintained.Please refer to the updated version of this 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.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed withsudo
. If you’re not familiar with thesudo
command, you can check our Users and Groups guide.
Before you install any package, ensure that your hostname is correct:
hostname hostname -f
Make sure your system is up to date:
sudo apt-get update && apt-get upgrade
Install Ruby
Install Ruby dependencies:
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
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:
wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.gz
Unpack the tarball:
tar -xzvf ruby-2.3.0.tar.gz
Move to the extracted directory:
cd ruby-2.3.0
Configure and install Ruby from source:
./configure make sudo make install
Install and Create a Rails Application
Install Rails on the server using
gem
(the package management framework for Ruby):sudo gem install rails
Before creating your project, move to the home directory:
cd
Create a new Rails project. You will be using
example
as your project name:rails new example
Move to the project directory:
cd example
Install and Configure Unicorn
Install Unicorn on the server using
gem
:sudo gem install unicorn
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 = 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"
Now, create the directories mentioned in the Unicorn config file:
mkdir -p shared/pids shared/sockets shared/log
Note
Please note that we are still in the Rails application directory.
Install and Configure Nginx
Install nginx:
sudo apt-get install nginx
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; }
Note
Editusername
andexample
with appropriate values.Remove the default nginx site configuration:
sudo rm /etc/nginx/sites-enabled/default
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; }
Note
Make sure you change the username and example with the appropriate values.Create a symlink to nginx’s
sites-enabled
directory to enable your site configuration file:sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled
Restart nginx:
sudo service nginx restart
Start Unicorn
To start Unicorn in the development environment:
sudo unicorn -c config/unicorn.rb -E development -D
To start Unicorn in the production environment:
sudo unicorn -c config/unicorn.rb -E production -D
Note
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:
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.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.