Setup Rails with Nginx using Passenger

17 / Jun / 2016 by Rahul Jaiswal 0 comments

nginx_passenger_eyecatcher

 

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:-

[js]
# Enable EPEL Repository
sudo su -c ‘rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm’

# Update everything, once more.
yum -y update
[/js]

 

2. Setting up Ruby and Rails

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

[js]curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh[/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.
[/js]

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:-

[js]passenger-install-nginx-module[/js]

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

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

‣ ⬢ Ruby
⬢ Python
⬢ Node.js
⬡ Meteor
[/js]

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:-

[js]
# 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
# config.ru
# 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
[/js]

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
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.

[js]RAILS_ENV=development

[/js]

Test your app :

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

 

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;
# }
[/js]

and add the below lines:-

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

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.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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