Setup Rails with Nginx using Passenger

17 / Jun / 2016 by Rahul Kumar Jaiswal 0 comments



The demo aims at running rails application behind Nginx using Passenger. Nginx is a high performance webserver. Passenger is a free web server/application server with support for Rails, Python, Node.js. Passenger is highly stable and fast already service over 350,000 websites.

1. Installing the deployment tools

Installing Development tools:-

[js]yum install "Development Tools" -y[/js]

Installing EPEL repository for installing Nginx:-

# Enable EPEL Repository
sudo su -c ‘rpm -Uvh’

# Update everything, once more.
yum -y update


2. Setting up Ruby and Rails

RVM is a perfect tool for installing and managing Ruby/Rails.

[js]curl -L | bash -s stable
source /etc/profile.d/[/js]

After the RVM installed, Install Ruby using RVM:-

[js]rvm install 2.1.0[/js]

Nodejs is also required as rails requires a javascript interpreter.

[js] yum install nodejs npm –enablerepo=epel
gem install bundler rails[/js]


3. Installing Server applications

3.1 Installing Passenger

We will be using gem to install passenger instead of the yum repository for the latest gem installation.

[js]gem install passenger[/js]

Run the command passenger to verify the passenger installation:-

[js]Phusion Passenger Standalone, the easiest way to run web apps.

Available commands:

passenger start Start Phusion Passenger Standalone.
passenger stop Stop a Phusion Passenger instance.
passenger status Show the status of a running Phusion Passenger instance.

Run ‘passenger <COMMAND> –help’ for more information about each command.

3.2 Setting up Nginx

Nginx must be compiled with Passenger for compatibility. This method is more preferred in comparision of installation from default repository. Installation can be done via Passenger application itself.

Compiling and installing Nginx with Passenger compatibility:-


Confirm the choice of language (in our case Ruby). Usually it detects and selects the languages already installed on the server.

Use <space> to select.
If the menu doesn’t display correctly, ensure that your terminal supports UTF-8.

‣ ⬢ Ruby
⬢ Python
⬢ Node.js
⬡ Meteor

In next step, select 1st option i.e.

“Yes download and install Nginx for me
Press enter to continue.”


4. Creating a sample Rails app/upload the source code

Perform the below steps to create a sample application:-

# Create a sample Rails app
cd /usr/local/src
mkdir app
cd app
rails new testapp
#This will create the basic directory and file structure for rails app
# app –> application
# bin –> any binaries/executables
# config –> config dir
# db –> DB (SQLLite)
# Gemfile
# Gemfile.lock
# lib –> library dir
# log –> logs
# public
# Rakefile
# README.rdoc
# test
# tmp
# vendor

cd testapp

#create controllers and pages to be used as home page in our app
rails generate controller pages

Edit the file app/controllers/pages_controller.rb and add the following lines to create the home action

[js]class PagesController < ApplicationController
def home
@response= "Hello World!"
end [/js]

Create the home route in file config/routes.rb

[js] root to: ‘pages#home'[/js]

Edit the file app/views/pages/home.html.erb

[js]<%= @response %> [/js]

In the above app we have created a variable response in pages_controller.rb which has the value "Hello World!. This variable is used to display the Hello World from the file app/views/pages/home.html.erb

Execute the following command to set the RAILS_ENV.



Test your app :

[js]cd /usr/local/src/app/testapp
rails s
#visit the url http://yourip:3000


5. Configuring Nginx

Edit /opt/nginx/conf/nginx.conf and add the following directive after the passenger_root and passenger_ruby directives inside http {.....}:-

[js]passenger_app_env development;[/js]

Further delete/comment the below section in server {....}

[js]# location / {
# root html;
# index index.html index.htm;
# }

and add the below lines:-

[js]root /usr/local/src/app/testapp/public;
passenger_enabled on;

Start the Nginx server:-

[js] /opt/nginx/sbin/nginx [/js]


Now, visit the ip of you server http://yourserverip. The Rails default welcome page should be up now. Hope this blog was useful in setting up Rails with Nginx.


Leave a Reply

Your email address will not be published. Required fields are marked *