breaking into the unknown…

rails on nginx or apache with passenger gem

1 Comment

I have explained deploying your rails app with rvm, nginx and passenger in this post, but it was through brightbox repository for ubuntu. Here, I will explain, installing nginx + passenger and nginx + appache using passenger gem . The detail instruction guide for nginx+passenger is available here and for apache+passenger  here  .

NOTE : this post explained in terms of nginx + passenger but flow will remain same for nginx + appache. The only difference will be in step 5 where you need to run passenger-install-apache2-module instead of passenger-install-nginx-module

STEP 1: Understanding the Flow

-> You need to have nginx or apache installed on your server

-> You need to have passenger installed on your server

-> You need to configure nginx or apache to load your app and suites your other need

-> You need to tell nginx or apache to work with passenger as application server

-> You need to tell passenger, where is your ruby and gemset

STEP 2:Prerequisite

=> You should have access to your server. For, learning you can try all the steps below, on your local machine also , considering it as a server

=> You should have rvm installed on your server. It is good, if you have installed it as root user. For non root user, you need to grant some permission to some files and folders.

=> Set the default RVM. Say, you are using ruby ruby-1.9.3-p194 and gemset threepiller for your app, set them as default, so that, the correct ruby and gemset get loaded automatically , whenever you login to the terminal. You can do it with below command.

$ rvm –default ruby-1.9.3-p194@threepiller # replace ruby-1.9.3-p194 with your ruby version and threepiller with your gemset

=> When you will try to install nginx module, it will get installed in opt/nginx folder by default. Since we will keep the defaults, we need to give write permission to opt folder. infect, even if you specify separate path say /etc/nginx or usr/local/nginx, these folder should have write permission for the user, with which you are loged in and trying to install the module. If you are root user, you do not need to do anything, as you have all the permissions. For, other users, say you have installed RVM as non root user arun, then you need to give write permission to arun on the opt directory of the file system. So, follow the below step

$ sudo su – # will login you as root user, you can grant permission to opt directory only if U r root user
[sudo] password for arun:

# cd ../../ # It will login you to the File System

# ls -l
total 116
drwxr-xr-x   3 root     root      4096 Aug  6  2012 boot
drwxr-xr-x   2 root     root      4096 Aug  6  2012 cdrom
drwxr-xr-x   4 root     root      4096 Sep 27 15:57 opt # write permission not available to owner and group

So, you can see that opt do not have write permission for group and other, we will assign the permission as below

# chmod 777 -R opt # it will add write permission to all as you can see below

# ls -l
total 116
drwxr-xr-x   3 root     root      4096 Aug  6  2012 boot
drwxr-xr-x   2 root     root      4096 Aug  6  2012 cdrom
drwxrwxrwx  4 root     root      4096 Sep 27 15:57 opt # write permission added to all the users

=>Install, some library needed for passenger

$ sudo apt-get install libcurl4-openssl-dev

STEP 3: Installing the gem

You may, install the gem as usual by adding it to Gemfile and running bundle install. But, I suggest you to not install, it through gem file.I can give below reasons for this

1 -> this gem is not used in your code, but required externally.

2-> if you put it in gemfile, it will be available in scope of your gemset. You need to install it again for some other gemset

3-> You are forcing the user to install the gem, even if he want to install passenger by some other method as in this post.

O.K, so we are not going to add the gem to gem file but add it manually from the terminal. Further, we will add the passenger to global gemset. The global gemset get created by default for ever ruby installed, and any gem within it will be available to all gemset within a particular ruby

$ rvm gemset list
gemsets for ruby-1.9.3-p194 (found in /home/arun/.rvm/gems/ruby-1.9.3-p194)

So, we can see 4 gemsets docusign global sinatra_app and threepiller. The threepiller is currently used gemset as indicated by =>. let us switch the gemset to global as below

$ rvm gemset use global # change the gemset to global
Using ruby-1.9.3-p194 with gemset global

$ rvm gemset list # list the gemset again and you will find global being selected now
gemsets for ruby-1.9.3-p194 (found in /home/arun/.rvm/gems/ruby-1.9.3-p194)

Now, we will install the gem in global gemset as below

$ gem install passenger
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while…
Fetching: daemon_controller-1.1.1.gem (100%)
Fetching: rack-1.5.2.gem (100%)
Fetching: passenger-3.0.19.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-1.1.1
Successfully installed rack-1.5.2
Successfully installed passenger-3.0.19
4 gems installed
Installing ri documentation for fastthread-1.0.7…
Installing ri documentation for daemon_controller-1.1.1…
Installing ri documentation for rack-1.5.2…
Installing ri documentation for passenger-3.0.19…
Installing RDoc documentation for fastthread-1.0.7…
Installing RDoc documentation for daemon_controller-1.1.1…
Installing RDoc documentation for rack-1.5.2…
Installing RDoc documentation for passenger-3.0.19…

Now, if you list the gem, you can find passenger there

$ gem list # listing the gem within a gemset

*** LOCAL GEMS ***

bundler (1.1.5)
daemon_controller (1.1.1)
fastthread (1.0.7)
passenger (3.0.19)
rack (1.5.2)
rake (
rubygems-bundler (1.0.3)
rvm (

So, passenger is there, now if you move to any other gemset, it will inherit from global and so passenger available there

STEP 4:  Understanding the passenger location and available modules

$ which passenger # will show where passeger is installed
/home/arun/.rvm/gems/ruby-1.9.3-p194@global/bin/passenger # so passenger is installed in bin folder at the shown path

$ cd /home/arun/.rvm/gems/ruby-1.9.3-p194@global/bin # move to bin folder

$ ls # list the files
bundle          passenger-install-apache2-module  passenger-status  rubygems-bundler-uninstaller
passenger      passenger-install-nginx-module    rackup          ruby_noexec_wrapper
passenger-config  passenger-memory-stats        rake

So, you can see a no of files in the bin folder. The important one for us is passenger-install-apache2-module and  passenger-install-nginx-module, we will install, them depending on we want nginx or apache for passenger

STEP 5: Installing nginx

We, will go with the default, so you just need to keep pressing enter or select proper options ,where ever asked .

$ ./passenger-install-nginx-module # it will install passenger for you, exposing a UI to you

NOTE : If, this module is not installed or broken You will get below error

Unable to start the Phusion Passenger watchdog because its executable (/usr/lib/phusion-passenger/agents/PassengerWatchdog) does not exist. This probably means that your Phusion Passenger installation is broken or incomplete, or that your 'PassengerRoot' directive is set to the wrong value. Please reinstall Phusion Passenger or fix your 'PassengerRoot' directive, whichever is applicable.

Solution : just reinstall the module, with the above command

This will be the first screen you will see

nginx-passenger module step 1

nginx-passenger module step 1

Press Enter, You will see the below screen

nginx-passenger module step 2

nginx-passenger module step 2

Passenger , do everything for you, so go with choice 1 i,e let passenger to install nginx. It will download and install nginx for you, as you can see in the below screen

nginx download and install

nginx download

In the end it will ask you to install nginx at /opt/nginx , the default path or to any other place. just go with the default and press enter.

You will see, the next screen showing that nginx is installed and passenger has been configured for you.

nginx installed

nginx installed

Here, You note down that, your nginx configuration file is present at /opt/nginx/conf/nginx.conf. Also, The passenger root is configured for you in it. Press enter to get the final screen, telling you how to configure your app and other things

configuring rails on nginx

configuring rails on nginx

STEP 5:  Configuring application with nginx

Your nginx configuration file is present at /opt/nginx/conf/nginx.conf file in this case ( it varies depending on how you install).If you open this file, you will see a number of lines, most of them are commented out and more like guidance for different configuration you can apply. But the basic and clean configuration i,e removing all the unwanted line, keeping only those being used currently should look as below.

worker_processes  1;

events {
worker_connections  1024;

http {
passenger_root /home/arun/.rvm/gems/ruby-1.9.3-p194@global/gems/passenger-3.0.19;
passenger_ruby /home/arun/.rvm/wrappers/ruby-1.9.3-p194@threepiller/ruby;

include       mime.types;
default_type  application/octet-stream;

sendfile        on;
keepalive_timeout  65;
server {
listen 8081;
server_name  localhost;
root /home/arun/Projects/myfinance/public;
passenger_enabled on;


If you have purchased a domain name for your server, you can replace localhost with that.
Also, If port is already in use you can use any other port, In my case, 8080 is used by tomcat so I used 8081

STEP 6: creating nginx init.d file

The above installation, do not create the nginx init file for you. You can create it as below

-> download the file by clicking this link.

-> Correcting the path in the downloaded file.

You need to correct the nginx path in the downloaded source. To me the downloaded file have below value



But our, nginx is installed at /opt/nginx folder , so these three constants are corrected as below



Also, there are some comments related to use of the file. remove all the
comments above the line '#! /bin/sh' . this should be the 
first line of the file

-> create nginx file in /etc/init.d folder and copy the modified content to it

-> grant execute permission to this after login as root user

# chmod 777 /etc/init.d/nginx

-> Run below command on terminal

$ sudo /usr/sbin/update-rc.d -f nginx defaults # it will update your system init to include nginx

NOTE : You may get below error while running above command. solution is explained here

System start/stop links for /etc/init.d/nginx already exist.

STEP 7: starting nginx

$ sudo  /etc/init.d/nginx restart
* Stopping Nginx Server…                                                                                                          [ OK ]
* Starting Nginx Server…                                                                                                             [ OK ]

STEP 8: Loading the app in browser

Go to browser and type localhost:8081 , you can see your home page  🙂

In case you get the error : 403 forbidden nginx , check the below things

-> root in nginx.conf file of step 5 should be set to public folder of your project

root /home/arun/Projects/myfinance/public; # will work

root /home/arun/Projects/myfinance; # will throw the above error

-> You Project folder i,e myfinance here should have read and execute permission. You can set it as below.

$ cd /home/arun/Projects # go to directory containing the source code myfinance in this case

$ chmod  777  -R  myfinance

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

One thought on “rails on nginx or apache with passenger gem

  1. Thank you, I have recently been looking for info approximately this subject
    for ages and yours is the best I have found out till now.
    But, what about the bottom line? Are you sure concerning the source?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s