breaking into the unknown…

rails on ubuntu with rvm nginx passenger

Leave a comment

I have explained deploying rails app with mina in this post. But the script just deploy my source code to the server and set my rvm, gemset and run the bundle. Though, that is great and good enough feature provided by the mina gem, I’ am still short of configuring my deployment with apache or nginx. It was, my first trial with nginx and passenger configuration. I tried a lot of things, get into a no of trouble, which I will post some other day. Here, I will list down the things which worked for me.

To get it work, I surfed a lot of blogs, read original documentation at nginx , nginxwiki and passenger, tried out a no of things myself and realized that what work for other may not work for you, simply because they are on some other environment, using different machine, different version of gem or rvm or ruby or so on. But, no doubt all these reading clear a lot of concepts and give you a direction to proceed.

NOTE : The rails+rvm+nginx+passenger configuration and installation using passenger gem is explained in this post.

STEP 1: Prerequisite

So, first thing first. I have used my friend suresh’s machine as server and deployed my source code using mina script. This is the detail of my overall environment.

-> ubuntu 12.04 # the version of Ubuntu determine which version of certain thing get installed in your system through apt-get install command, say older version of ubuntu may install older version of nginx as compared to this one. generally repository installation is always outdated. example it install nginx 1.1.9 for me when current available version is 1.2.3. don’t worry, I have uninstalled it and manage to install the latest version

-> rvm 1.13.8 (stable) # my rvm is installed as non root user and located  in /home/suresh/.rvm , don’t worry if you have installed as root user, infact many have suggested that it is better to have all server installation as root user. You may have any other version of .rvm, but that version must provide wrappers support i,e there should be wrappers folder in your rvm folder. for example: Iam having  /home/suresh/.rvm/wrappers. If you do not find this in your rvm, the implementation here will not work for you, so upgrade your rvm

-> ruby 1.9.3 # You may have any other ruby version. The thing is that passenger in common case work with only one ruby(though, you can make it work with multiple ruby), which you should able to provide it in its configuration file

-> using gemset : myapp # you may have different name for your gemset

-> my source code is present at /var/www/ # it may be present at any location, but as a common practice put it here. mina deploy my code to current folder  in /var/www/ and back up the older code to releases folder

-> I have set my application environment to staging # You need to specify the environment in server configuration of nginx, else it will run the default production environment

STEP 2: Understanding the approach

-> You need to have nginx installed on your server

-> You need to have passenger installed on your server

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

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

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

STEP 3: installing nginx and passenger

There is a no of methods available for installing both of them like: from source code, through debian i,e apt-get install , gem install etc. I tried all of them and find that, installing through debian is the best as it do a lot of configuration for you itself and place everything at the default locations. It is almost like, you have run the command and you are done when it is finished. But, its drawback is that, it install from system repository which is always outdated, for example recent ubuntu version (12.04) will install nginx 1.9.1 for you when the recent available version is 1.2.3. So people prefer to install from the source code. I have tried it here. But it give you a lot of configuration headaech and you may mess up thing many a time if missed something.

But, the good thing is that brightbox provide a updated passenger repository with nginx and apache mod which you can install as debian. Thus, you will get the updated versions as well as all the automatic configuration done by debian. For, nginx with passenger just run the below command
$ sudo apt-add-repository ppa:brightbox/passenger-nginx #it will add passenger-nginx repository to your system
$ sudo apt-get update # update the debian of the new repository.
$ sudo apt-get install nginx-full # not down the last few line of the log as will tell you where some file are installed, which will help you in understanding the configuration in general

Unpacking replacement nginx-common …
Preparing to replace nginx-full 1.1.19-1ubuntu0.1 (using …/nginx-full_1%3a1.2.3-1~36~precise1_i386.deb) …
Unpacking replacement nginx-full …
Processing triggers for man-db …
Processing triggers for ufw …
Processing triggers for ureadahead …
Setting up libreadline5 (5.2-11) …
Setting up libruby1.8 ( …
Setting up ruby1.8 ( …
update-alternatives: using /usr/bin/ruby1.8 to provide /usr/bin/ruby (ruby) in auto mode.
Setting up ruby (4.8) …
Setting up ruby-rack (1.3.5-1) …
Setting up librack-ruby1.8 (1.3.5-1) …
Setting up passenger-common (3.0.17+110~precise1) …
Setting up nginx-common (1:1.2.3-1~36~precise1) …
Installing new version of config file /etc/nginx/nginx.conf …
Installing new version of config file /etc/nginx/sites-available/default …
Installing new version of config file /etc/nginx/uwsgi_params …
Installing new version of config file /etc/init.d/nginx …
Installing new version of config file /etc/logrotate.d/nginx …
Setting up nginx-full (1:1.2.3-1~36~precise1) … # so it installed the most current version for you

NOTE : It will also install passenger at /usr/lib/phusion-passenger

STEP 4: Testing nginx
$ sudo /etc/init.d/nginx start # this will start nginx
full list of the available options are {start|stop|restart|reload|force-reload|status|configtest}

Now, go to browser and type localhost or ip address of your system in my case. I got

500 internal server error

I checked the /var/log/nginx/error.log file for error, which show the below error
rewrite or internal redirection cycle while internally redirecting to /index.html nginx

Solution: edit the default file in site-enabled folder as below

$ sudo su – # change to root user
$ gedit /etc/nginx/sites-enabled/default # it will open the default file for editing

There you will find a no of lines, but most of them are commented out and it just for instruction how to write different type of configuration. They are worth reading, infact you can read them as reference when needed,so better backup it, if you want to change it considerably. For now, just find the below line in the opened file.
root   /usr/share/nginx/www;
try_files   $uri $uri/ /index.html;

Replace, them with below line
root   /usr/share/nginx/html; # the www folder is not there, instead html folder is present so corrected the default root
try_files   $uri $uri/ /index.html =404; # some one suggested it here

Again, Restart the server and type localhost in browser. Yepieee…..nginx is up. You will see

Welcome to nginx!

STEP  5: Configuring your app with nginx
All nginx configuration goes to nginx.conf file. It follow nested block structure(http -> server -> location),with configuration in higher block passing to lower one







But, since you have installed, nginx through debian, it has structured your thing such that in nginx.conf file you write only http block where as server and location blocks goes to file in site-enabled and conf.d folder. all these files are loaded within http block of nginx.conf with below line, already there.
include /etc/nginx/conf.d/*.conf; #it load all file within sconf.d folder with .conf extension.
include /etc/nginx/sites-enabled/*; # it load all file in site-enabled folder. site related configuration should be placed in individual file in this folder

We will add my_app file within /etc/nginx/sites-enabled folder and add below configuration

server {

listen 8080; #port at which our server will listen
server_name; #name of our server
root /var/www/; # <— be sure to point to ‘public’!
passenger_enabled on; # it will set passenger as our application server
rails_env staging; # set the environment. default is production


STEP 6: configuring passenger

We will add passenger.conf file in /etc/conf.d folder and add below line to this

passenger_root /usr/lib/phusion-passenger; # it will tell where passenger is present, modify it if it is at some other location
passenger_ruby /home/suresh/.rvm/wrappers/ruby-1.9.3-p194@myapp/ruby; # the ruby path must be from wrappers folder, otherwise will throw error no such file to load — rubygems (LoadError) . I find this blog useful in clearing some of my concept

When, you see the wrappers folder, you will find a separate folders for every ruby you installed and gemset you have created within them. the folder name is combination of ruby_name@gemset_name. within them you can find files for gem, ruby, rake etc. What I feel that, these wrapper automatically, load correct gemset for passenger

STEP 7: Test Your App running

restart the server with the below command
$ sudo /etc/init.d/nginx start # this will start nginx

Go to browser and type IP_of_server:8080 for me it is 168:192:173:52:8080

Yepieeee…. My Home Page is UP  🙂


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.

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