Tuesday 25 April 2017

Deploy Ruby on Rails project with Passenger and Ngnix

Assuming that you have installed ruby and rails at your server and have a Ruby on rails project to deploy with Passenger and Ngnix



Step 1 - First of all we need to add a Sudo User to our system so user can have privilege to access the files.

create a user if not exists, in this example We will explain to create a root user

tecorb@tecorb-Lenovo-B40-80:~$  adduser root
(this will ask few questions like your password etc.)


Step 2 - Now we have to add this new user to the sudo group

Syntex:
tecorb@tecorb-Lenovo-B40-80:~$ gpasswd -a root sudo

Step 3 - Now we need to install Passenger and Nginx

First of all we will install a PGP (Pretty Good Privacy) key:

tecorb@tecorb-Lenovo-B40-80:~$  sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 561F9B9CAC40B2F7

Now we have to create an APT (Advanced Packaging Tool) source file

tecorb@tecorb-Lenovo-B40-80:~$  sudo vim /etc/apt/sources.list.d/passenger.list

And insert the following line in the file:

deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main

Press ESC then :wq and hit enter, Now our file has been saved through vi editor

Now we need to change the owner and permissions for this passenger.list file:

tecorb@tecorb-Lenovo-B40-80:~$  sudo chown root: /etc/apt/sources.list.d/passenger.list
tecorb@tecorb-Lenovo-B40-80:~$  sudo chmod 600 /etc/apt/sources.list.d/passenger.list

Now we have to update our APT cache:

tecorb@tecorb-Lenovo-B40-80:~$   sudo apt-get update

And finally, we will install Passenger with Nginx:

tecorb@tecorb-Lenovo-B40-80:~$  sudo apt-get install nginx-extras passenger

Step 4- In this step we will open the Nginx configuration file for some changes:

tecorb@tecorb-Lenovo-B40-80:~$  sudo vim /etc/nginx/nginx.conf

Now file will be open in vi editer, Uncomment given line, These lines will look like this:

# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;

uncomment these lines and update the path mentioned in the passenger_ruby line and add default user :

passenger_default_user root;
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/rvm/gems/ruby-2.3.0/wrappers/ruby;


Note if you are unable to get what is your path to ruby then follow these steps:

tecorb@tecorb-Lenovo-B40-80:~$   which passenger-config
this will return a path like: /usr/bin/passenger-config

If you are on RVM and use Ruby 2.2.1 (version can be whatever you are using)

tecorb@tecorb-Lenovo-B40-80:~$  rvm use 2.2.1

Now finally, we will invoke passenger-config with its full path, passing --ruby-command as parameter like:

tecorb@tecorb-Lenovo-B40-80:~$  /usr/bin/passenger-config --ruby-command

This will show you something like:

  To use in Apache: PassengerRuby /usr/local/rvm/wrappers/ruby-1.8.7-p358/ruby
  To use in Nginx : passenger_ruby /usr/local/rvm/wrappers/ruby-1.8.7-p358/ruby
  To use with Standalone: /usr/local/rvm/wrappers/ruby-1.8.7-p358/ruby /opt/passenger/bin/passenger start

From here you will get path for your passenger_ruby (Nginx). This path will use at passenger_ruby in nginx.conf

Now finally we will save our file and exit by Press ESC then type :wq and hit enter.

Step 5- Now we have all most done, we need to disable the default Nginx configuration at Nginx config file:

tecorb@tecorb-Lenovo-B40-80:~$  sudo vim /etc/nginx/sites-available/default

Findout these lines and comment them:

# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;

Save this config file, Press ESC then type :wq and hit enter.

Step 6- Now we will create an Nginx configuration file for our application (like our app name is tecorbapp):

tecorb@tecorb-Lenovo-B40-80:~$  sudo vim /etc/nginx/sites-available/tecorbapp

Add the following server block into this file:

server {
  listen 80 default_server;
  server_name tecorbdomain.com www.tecorbdomain.com;
  passenger_enabled on;
  passenger_app_env development;
  root /home/rails/tecorbapp/public;
}

In this file we enable listening on port 80, set our domain, enable Passenger, set the application environment here we have used development environment and set the root to the public directory of our ruby on rails project.
If you don't want to add your domain to the application, you can skip the server_name line from this file, or if you want to use any IP address you can replace
tecorbdomain.com www.tecorbdomain.com with your ip address.

Now we will save this config file, Press ESC then type :wq and hit enter.

Step 7- Now finally we will create a symlink this file:

tecorb@tecorb-Lenovo-B40-80:~$   sudo ln -s /etc/nginx/sites-available/tecorbapp /etc/nginx/sites-enabled/tecorbapp

and Restart our Nginx:

tecorb@tecorb-Lenovo-B40-80:~$  sudo nginx -s reload

Now our application should be accessible from our domain, go to your browser and hit the domain.


Good Luck!

1 comment: