/ web-server

Getting started with Nginx

Nginx is a web server software like apache. The purpose of a web server software to serve web page in response to a browser request. Nginx pronounced as engine-x. It is faster than apache web server for concurrent requests because it does not create a thread for request like apache. Here is a detail comparison of the two https://www.nginx.com/blog/nginx-vs-apache-our-view/ .

Installing Nginx with passenger on ubuntu

1.Install rvm and ruby

First update the packages in the system

sudo apt-get update

Then install the rvm and ruby if not installed using this commands

curl -L get.rvm.io | bash -s stable

and install latest ruby version

rvm install ruby-2.3.1

2.Install passenger

To deply rails application we can use passenger gem. To install passenger gem you simply need to

gem install passenger 

3.Create a rails application

gem install rails


rails new blog # here blog is my application name

4.Install nginx with passenger module
With passenger module you just need to enter this command

rvmsudo passenger-install-nginx-module

Here note that we used rvmsudo instead of sudo user because sudo does not contains environment variables like $PATH, $GEM_HOME etc, which are required. To run this(or any other) command you might need your root password.

It may be possible that your system has some libraries missing. For that passenger will prompt you a message with command. You should install the required library and then again run the above command.

After the above command you should see option for continue or cancel. Press enter to continue. Then it will show you options for installation using ruby, python, node and meteor. Option 1 is for ruby which is by default selected (see screenshot)

enter image description here

It will also give you options for default or customisation. By default nginx install in the /opt/ directory, you can change it's path by entering the location.

Nginx has it's configuration file in /opt/nginx/conf/nginx.conf if its installed in/opt. By default passenger modify the configuration file and specify the options passenger_root and passenger_ruby. But sometimes if it does not specify the correct path you can modify this options to set the correct path

 http {
  ...
  passenger_root /home/kiprosh008/.rvm/gems/ruby-2.3.1/gems/passenger-5.0.30;
  passenger_ruby /home/kiprosh008/.rvm/gems/ruby-2.3.1/wrappers/ruby;
  ...
 }

5.Add your application path to nginx config file
Open nginx config file using nano/vim editor

sudo nano /opt/nginx/conf/nginx.conf

In the server block set the root option to public directory of your rails application. Your config file would be like

server {
    listen       80;
    server_name  localhost
    root /var/www/blog
   ..
 }

You can set your domain name in server_name and other configurations in this file depending on your requirements.

6.Start Nginx
That's all you just need to start nginx server

     sudo service nginx start 
Punit Jain

Punit Jain

Hey this is Punit Jain.I brought up in Indore, currently in Mumbai having 4.5 years of software development experience.When not messing with the code I would like to enjoy the m...

Read More