Product docs and API reference are now on Akamai TechDocs.
Search product docs.
Search for “” in product docs.
Search API reference.
Search for “” in API reference.
Search Results
 results matching 
 results
No Results
Filters
Deploy a Ruby on Rails App with Unicorn and nginx on Ubuntu 18.04
- Deprecated guides:
- Ubuntu 14.04
Traducciones al EspañolEstamos 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.
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 18.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 Creating a Compute Instance and Setting Up and Securing a Compute Instance guide.
sudo
. If you’re not familiar with the sudo
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 Node.js
Some of the features in Rails, such as the Asset Pipeline, depend on a JavaScript Runtime and Node.js provides this functionality.
Install Node.js using a PPA (personal package archive) maintained by NodeSource:
curl -sL https://deb.nodesource.com/setup_8.x -o nodesource_setup.sh
Run the script:
sudo bash nodesource_setup.sh
Install the Node.js package:
sudo apt-get install nodejs
Check the version of Node.js:
nodejs -v
Install Yarn
Configure the repository to install Yarn using Debian package repository:
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
Install Yarn:
sudo apt update && sudo apt install yarn
Install Ruby
Install Ruby dependencies:
sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev libsqlite3-dev
Download the latest version of Ruby. At the time of writing this article, the current, most recent and stable version is 2.7, but you can check for the latest version here :
wget https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.gz
Unpack the tarball:
tar -xzvf ruby-2.7.0.tar.gz
Move to the extracted directory:
cd ruby-2.7.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.- 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:- File: /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:
- File: /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.
This page was originally published on