12/08/2018, 13:24

Rails and Nginx

What we are going to do in this post is to look for way to run our rails app in a production-like environment. Nginx is just the right server to do this job. We assume that we are going to use Ubuntu 14 or higher as an OS as Nginx is also an open source server. What is Nginx? NGINX is a free, ...

What we are going to do in this post is to look for way to run our rails app in a production-like environment. Nginx is just the right server to do this job. We assume that we are going to use Ubuntu 14 or higher as an OS as Nginx is also an open source server.

What is Nginx?

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Javascripts, CSS and images will be served through Nginx server and Rails logic go through the rails app itself.

How to Download and install

Go to https://phusionpassenger.com and download the open source version which is free. Don't forget to choose Ubuntu as an operating system, and ofcourse, Nginx to install. The configuration might be a little bit different depending on your OS version as you follow the instructions on the website. But I am going to assume we are using ubuntu 14.04. It will lead to another page which show how to install. Enter the following command in your terminal.

 # Install our PGP key and add HTTPS support for APT
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 trusty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

# Install Passenger + Nginx
sudo apt-get install -y nginx-extras passenger

Next edit /etc/nginx/nginx.conf and uncomment passenger_root and passenger_ruby as below.

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/chivorn/.rbenv/shim/ruby;

To know where we install ruby we type:

 $ which ruby

It will show the path of ruby.

When finished you need to restart server:

 sudo service nginx restart

After installation, we should validate the install by running

 sudo /usr/bin/passenger-config validate-install
 * Checking whether this Phusion Passenger install is in PATH... ✓
 * Checking whether there are no other Phusion Passenger installations... ✓

All checks should pass. If you have some problem, please refer to Nginx document page.

How to configure with rails app

To make everything clear we will create a new demo rails app:

 rails new demo

We will create a new file which I call nginx.conf in config folder.

And add the following configuration:

 server {
     listen 80 default_server;
     root /home/chivorn/projects/demo/public;
     passenger_enabled on;
 }

You need to know where you store your rails project. For me it is: /home/chivorn/projects/demo

Then we need to make nginx.conf available under nginx server by creating the link below:

sudo ln -s /home/chivorn/projects/demo/config/nginx.conf /etc/nginx/sites-enabled/demo

Then we need to restart nginx server:

sudo service nginx restart

One more thing we need to do is comment out the following file: /etc/nginx/sites-enabled/default If you type:

ls /etc/nginx

You will see there are 2 files in folder: default and demo

Restart the server again:

sudo service nginx restart

And we are ready to go. The last thing we need to do is to configure our demo app to have all the assets precompiled and run our app in production.

We need to configure database.yml to run in production.

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

Now go to our project root folder and enter the following command to precompile all our assets into a single file. We will get 2 files 1 for js and 1 for css.

RAIL_ENV=production rake assets:precompile

Now without doing anything else, and if everything goes correctly we will get to our site by just typing:

localhost

without port 3000 in the browser url.

We can further configure nginx to work with postgresql. Yep, we finally get site hosted by using nginx!

0