Wednesday, October 28, 2015

Deploy Ruby On Rails Application with Nginx server using Phusion Passenger gem

Here I will show you that how to deploy ruby on rails application with passenger and nginx on digitalocean OR amazon aws server.

Here I have used Ubuntu as operating system.

First of all you can login with root user. For deploy application its better to create new user.

Step 1.) create new user for our application. 
adduser sachin(username)
Step 2.) Give that user to sudo permission so that user will able to do some administrative work
gpasswd -a sachin sudo
Step 3.) Generate ssh key for root user
Step 4.) Then read generated key with this and copy it somewhere else or clipboard for further use
cat ~/.ssh/
Step 5.) Now you can login into created new user(sachin)
su - sachin
Step 6.) Then we can add root user's key into this user's authorization key and for that we can follow this pattern:
mkdir .ssh
chmod 700 .ssh/
nano .ssh/authorized_keys 
Now insert your public key (which should be in your clipboard or anywhere else you have copied earlier in step 4) by pasting it into the editor. 
chmod 600 .ssh/authorized_keys
With exit you will logout from user(sachin) and now you are into root user.

Step 7.)  Then again login in sachin user
su - sachin (Make sure you do all later operation with this user)
Step 8.) Set Up Your Domain(Optional)
In order to ensure that your site will be up and visible, you need to set up your DNS records to point your domain name towards your new server. You can find more information on setting up a hostname by following the link. 
Step 9.) Now you need to setup environment for ruby as well rails. We can install ruby from latest source code as well rvm.

Step 10.) Now we need to setup passenger with nginx
        First, install a PGP key:
sudo apt-key adv --keyserver --recv-keys 561F9B9CAC40B2F7
Create an APT source file (you will need sudo privileges):
sudo nano /etc/apt/sources.list.d/passenger.list
And insert the following line in the file:
deb trusty main
Press CTRL+x to exit, type y to save the file, and then press ENTER to confirm the file location.
Change the owner and permissions for this file:
sudo chown root: /etc/apt/sources.list.d/passenger.list
sudo chmod 600 /etc/apt/sources.list.d/passenger.list
Update the APT cache:
sudo apt-get update
Finally, install Passenger with Nginx:
sudo apt-get install nginx-extras passenger
        Note : If you have used rvm then don't do this:
This step will overwrite our Ruby version to an older one. To resolve this, simply remove the incorrect Ruby location and create a new symlink to the correct Ruby binary file:
sudo rm /usr/bin/ruby
sudo ln -s /usr/local/bin/ruby /usr/bin/ruby 
Step 11.) Setup the web server
Open the Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Find the following lines, in the http block:
# passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
# passenger_ruby /usr/bin/ruby;
Uncomment both of them. Update the path in the passenger_ruby line. They should look like this:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/local/bin/ruby;
Then save file.

You will get path for passenger_root with
passenger-config --root
You will get path for passenger_ruby with
which ruby 
Note: For passenger_ruby
If you have used ruby source code then keep as it is
If you have used rvm without gemset then it should like this:
passenger_ruby /home/sachin/.rvm/gems/ruby-2.2.0/wrappers/ruby
If you have used rvm with gemset named testapp then it should be like this:
passenger_ruby /home/sachin/.rvm/gems/ruby-2.2.0@testapp/wrappers/ruby
Step 12.) Now we need to configure nginx
First, We need to disable the default Nginx configuration. Open the Nginx config file:
sudo nano /etc/nginx/sites-available/default
Find the lines:
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
Comment them out, like this:
# listen 80 default_server;
# listen [::]:80 default_server ipv6only=on;
then Save the file.

        Now, create an Nginx configuration file for our app:
sudo nano /etc/nginx/sites-available/testapp
        Add the following server block. The settings are explained below.(Nginx redirect rules)
        If you have domain name then set it as server_name else add ip address for the server_name. If you are using more then one application with same server with different domain the don't add default_server into listen 80
server {
listen 80 default_server;
passenger_enabled on;
passenger_app_env production;
root /home/sachin/testapp/public;
         Then create a symlink for it:
sudo ln -s /etc/nginx/sites-available/testapp /etc/nginx/sites-enabled/testapp 

Step 13.) Reload & Restart nginx
Reload: sudo nginx -s reload
Restart: sudo service nginx restart


Post a Comment