Alibaba Cloud

May 30, 2018

5 min read

How to Deploy Ruby on Rails with Passenger and Nginx on Ubuntu 16.04

By Hitesh Jethva, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Ruby on Rails is a free, open source server-side web application framework that allows web designers and developers to implement dynamic fully featured web applications. Ruby is easy to learn, easy to extend, and object-oriented. This means that it involves less coding and fewer bugs. That is why Ruby is gaining popularity all over the world.

You can develop a web application at least ten times faster with Rails than you could with a typical Java framework. Ruby is based on the Model View Controller architecture that enables the data to be separated from presentation. You can develop customized and search engine friendly URLs using Ruby on Rails. Ruby on Rails comes with built-in tools that make common development tasks easier. You can deploy Ruby on Rails based application with a database server like, MySQL or PostgreSQL and an Apache web server with Phusion Passenger module.

In this tutorial, we will explain how to deploy Ruby on Rails with Passenger and the Nginx web server on an Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 16.04 server.


  1. A root password is setup to your instance

Launch Alibaba Cloud ECS Instance

Once you are logged into your Ubuntu 16.04 instance, run the following command to update your base system with the latest available packages.

apt-get update -y

Install Required Dependencies

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

Once all the required dependencies are installed, you can proceed to the next step.

Install Ruby and Rails

`gpg — keyserver hkp:// — recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
curl -sSL | bash -s stable`

Once RVM is installed, you will need to add all users that will be using RVM to rvm group, then log out and log in again. You can do this with the following command:

usermod -a -G rvm root

Next, check and install the required packages to install Ruby by running the following command:

rvm requirements

Once all the required packages are installed, you can install Ruby (version 2.4.2) with the following command:

rvm install 2.4.2

Once RVM installed successfully, you should see the following output:

ruby-2.4.2 - #extracting rubygems-2.6.14....
ruby-2.4.2 - #removing old rubygems.........
ruby-2.4.2 - #installing rubygems-2.6.14...........................
ruby-2.4.2 - #gemset created /usr/local/rvm/gems/ruby-2.4.2@global
ruby-2.4.2 - #importing gemset /usr/local/rvm/gemsets/global.gems...............................................
ruby-2.4.2 - #generating global wrappers........
ruby-2.4.2 - #gemset created /usr/local/rvm/gems/ruby-2.4.2
ruby-2.4.2 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.4.2 - #generating default wrappers........
ruby-2.4.2 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.4.2 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri

Next, set the default RVM version using the following command:

rvm use 2.4.2 --default

Next, install Rails (version 5.1.4) by running the following command:

gem install rails -v 5.1.4

Once Rails is installed, you can proceed to the next step.

Install Passenger and Nginx

First, install a PGP key and HTTPS support for the package manager by running the following command:

apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
apt-get install -y apt-transport-https ca-certificates

Next, add the repository to the APT with the following command:

echo "deb xenial main" >> /etc/apt/sources.list

Next, update the APT cache with the following command:

apt-get update -y

Next, install Nginx and Passenger by running the following command:

apt-get install nginx-extras passenger

Nginx and Passenger are now installed, but support for Phusion Passenger is not enabled. You can do this by editing /etc/nginx/nginx.conf file:

nano /etc/nginx/nginx.conf

Find and uncomment the following lines:

include /etc/nginx/passenger.conf;

Save and close the file when you are finished.

Here, you are using RVM, so you will need to specify which version of Ruby should be used by Passenger in /etc/nginx/passenger.conf file.

You can find the version of the Ruby used by Passenger with the following command:

rvm use
passenger-config --ruby-command

You should see the following output:

passenger-config was invoked through the following Ruby interpreter:
Command: /usr/local/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 /usr/local/rvm/gems/ruby-2.4.2/wrappers/ruby
To use in Nginx : passenger_ruby /usr/local/rvm/gems/ruby-2.4.2/wrappers/ruby
To use with Standalone: /usr/local/rvm/gems/ruby-2.4.2/wrappers/ruby /usr/bin/passenger start

Now, copy Nginx line from above output and paste in /etc/nginx/passenger.conf file:

nano /etc/nginx/passenger.conf

Make the following changes:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/gems/ruby-2.4.2/wrappers/ruby;

Save and close the file, then restart Nginx with the following command:

systemctl restart nginx

Deploy Rails App

You can install rails gem by running the following command:

gem install --no-rdoc --no-ri rails

Next, create a new app with the name railapp by running the following command:

cd /var/www/html
rails new railapp --skip-bundle

Next, change the directory:

cd railapp

Next, you will need to install a JavaScript execution environment. First, open
the Gemfile:

nano Gemfile

Uncomment the following line:

gem 'therubyracer', platforms: :ruby

Save the file, then run Bundler with the following command:

bundle install

Next, you will need to disable Nginx default virtual host file and create a new virtual host file for railapp.

You can do this with the following command:

rm -rf /etc/nginx/sites-available/default
nano /etc/nginx/sites-available/railapp

Add the following lines (replace the variable Your_Server_IP with the actual IP address of your ECS instance):

server {
listen 80 default_server;
server_name Your_Server_IP;
passenger_enabled on;
passenger_app_env development;
root /var/www/html/railapp/public;

Save and close the file, then enable virtual host with the following command:

ln -s /etc/nginx/sites-available/railapp /etc/nginx/sites-enabled/

Finally, restart Nginx service to apply changes:

systemctl restart nginx

Your Rail app is now up and running, open your web browser and navigate to URL http://Your_Server_IP, you will be redirected to the Rail app dashboard as shown below (replace the variable Your_Server_IP with the actual IP address of your ECS instance):

Related Alibaba Cloud Products

Alibaba Cloud Web Application Firewall (WAF) can be used to provide protection against web-based attacks, including SQL injections, Cross-site scripting (XSS), Malicious BOT, command execution vulnerabilities, and other common web attacks. WAF filters out a large number of malicious access attempts and alleviates the performance impact of Hypertext Transfer Protocol (HTTP)/HTTP Secure (HTTPS) flood attacks on servers.