How to setup ROR project with nginx + passenger.







This blog is about how you can setup Exiting Ruby on rails Application with Nginx and Passenger. This setup is done on ubuntu 18.04. So below ubuntu 18.04 you may find some dependency issue. In this blog I have setup first Nginx and Passenger. Then after Installed Ruby using RVM.  then installed rails. Also I have installed Postgres. In the end, I have added setups to setup Nginx config file for pointing ruby on rails application.


Step :- 1 Installation of Nginx

#install nginx for passenger
sudo apt-get update
sudo apt install nginx-extras
sudo service nginx start


Step :- 2 Install nginx and passenger module


# install our PGP key and add HTTPS support for APT

sudo apt-get install -y dirmngr gnupg
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

#Add our APT repository

sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

#install nginx +passenger module
sudo apt-get install -y libnginx-mod-http-passenger


Step :-3 enable the Passenger Nginx module and restart Nginx

if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load/etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi
sudo ls /etc/nginx/conf.d/mod-http-passenger.conf

# Ingnore below step if you successfully run above command

# If you don't see a file at /etc/nginx/conf.d/mod-http-passenger.conf; then you need to create it yourself and set the passenger_ruby and passenger_root config options. For example write below lines in file you have created:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/passenger_free_ruby;
sudo service nginx restart


Step :-4 To Check installation if nginx working on passenger or not

sudo /usr/bin/passenger-config validate-install
sudo /usr/sbin/passenger-memory-stats


Step :- 5 Installation of Ruby ( Node.js and Yarn repositories )

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update
sudo apt-get install git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn


Step :- 6 Installation of Ruby with rvm and installation of rails


sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm

# if you want other versions of ruby simply change 3.0.0 with your requirement
# for example rvm install 2.4.0

rvm install 3.0.0
rvm use 3.0.0 --default
ruby -v

#To install bundler
gem install bundler

#To install rails
gem install rails -v 6.1.1


Step :- 7 Installaiton of postgres

#To install the latest version
sudo apt-get install libpq-dev
sudo apt install postgresql postgresql-contrib

# to create postgres db user ( nikunj is username )
sudo -u postgres createuser nikunj -s

# If you would like to set a password for the user, you can do the following
sudo -u postgres psql

Step :- 8 Set-up of Nginx config file

# Before you begin, you need to tell Passenger which Ruby command it should use to run your app, just in case there are multiple Ruby interpreters on your system.

passenger-config about ruby-command

#Add this ruby path in /etc/nginx/conf.d/mod-http-passenger.conf
#file and replace ruby with your ruby path.
# For example:-

passenger_ruby /usr/bin/passenger_free_ruby;

# Replace with
passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.4.0/wrappers/ruby


# Now Edit /etc/nginx/site-enabled/default file with this

server {
listen 80;
server_name yourserver.com;
# Tell Nginx and Passenger where your app's 'public' directory is
root /path-to-app/public;
# Turn on Passenger
passenger_enabled on;
}

#To check nginx configuration
sudo nginx -t

#now restart nginx after setup is done
sudo service nginx restart







Comments

Popular posts from this blog

Different Ways to change .Pem file of AWS instance Part : - 2 ( Using Reset-Access Document )

Contact Me

Name

Email *

Message *