Setup Rails with Nginx using Passenger
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:-
yum install "Development Tools" -y
Installing EPEL repository for installing Nginx:-
# 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
2. Setting up Ruby and Rails
RVM is a perfect tool for installing and managing Ruby/Rails.
curl -L get.rvm.io | bash -s stable source /etc/profile.d/rvm.sh
After the RVM installed, Install Ruby using RVM:-
rvm install 2.1.0
Nodejs is also required as rails requires a javascript interpreter.
yum install nodejs npm --enablerepo=epel gem install bundler rails
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.
gem install passenger
Run the command passenger
to verify the passenger installation:-
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:-
passenger-install-nginx-module
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 # 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
Edit the file app/controllers/pages_controller.rb
and add the following lines to create the home action
class PagesController < ApplicationController def home @response= "Hello World!" end end
Create the home route in file config/routes.rb
root to: 'pages#home'
Edit the file app/views/pages/home.html.erb
<%= @response %>
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
.
RAILS_ENV=development
Test your app :
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 {.....}
:-
passenger_app_env development;
Further delete/comment the below section in server {....}
# location / { # root html; # index index.html index.htm; # }
and add the below lines:-
root /usr/local/src/app/testapp/public; passenger_enabled on;
Start the Nginx server:-
/opt/nginx/sbin/nginx
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.