How to Install Rails and Nginx With Passenger on Ubuntu?

How To Install Rails and Nginx with Passenger on Ubuntu?

Ruby on Rails is an open source web application framework which runs on the Ruby programming language. It allows the developers creating pages and applications that gather information from the web server, talk to or query the database, and render templates out of the box directly to the internet browsers.

As a result, Rails features a routing system that is independent of the web server. The web server is used to render the content only. The choice for that would be Nginx. Nginx is fast webserver with a strong focus on high concurrency, high performance and low memory usage.

Install Ruby and Rails

Before we proceed with installation, we should make sure that our system repositories are up to date:

1
sudo apt-get update

Once we are up to date with the latest available packages, the next step is to install Ruby Version Manager. It is application that allows to manage several different ruby versions easily, we can install RVM and then load it with:

1
2
curl -L get.rvm.io | bash -s stable
source /usr/local/rvm/scripts/rvm

We must make sure that we have all dependencies from RVM. To make sure that we have all required dependencies, we execute the following command:

1
rvm requirements

It will make sure that we are up to date and install the missing requirements (if any). Once we have RVM installed and configured, we can proceed to install and configure Ruby.

1
2
rvm install 2.1.1
rvm use 2.1.1 --default

These two commands will install Ruby and set the system to use version 2.2.1 by default. The next step is to make sure we have all components for Ruby on Rails. Ruby Gems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries, a tool designed to easily manage the installation of gems, and a server for distributing them, we can install it with this command and then use it to install Rails:

1
2
rvm rubygems current
gem install rails

This process could take some time, but after it finish, Ruby on Rails is installed.

Install Passenger and Nginx

We need to make sure that we can easily deploy Ruby on rails to any web server. We will install and use Passenger for that. It will serve as interface or bridge for communication between Ruby and the web server, you can install it with the following command:

1
gem install passenger

Once passenger is installed, the rest of the required setup is fully automated. We execute the command:

1
rvmsudo passenger-install-nginx-module

Once we do this, it checks for all dependencies automatically and install those that are missing. If some manual user action is required, Passenger will tell us, as well as give us detailed instructions how to do it.

Now we need to configure nginx to “talk” to Passenger. In order to do that, we need to open the nginx configuration file (/opt/nginx/conf/nginx.conf), using our favorite editor and add the following:

nginx.conf
1
2
3
4
5
6
server {
  listen 80;
  server_name example.com;
  passenger_enabled on;
  root /var/www/rails_app/public;
}

In order to create our rails app, we need to install Node.js first:

1
sudo apt-get install nodejs

Once that is that, we should go to our directory (in this case it is /var/www/rails_app/public) and create the application. After all this is installed and configured, we simply need to start Nginx.

1
2
rails new my_first_rails_app
sudo service nginx start

We can try and access our new Ruby on Rails application using our browser. It seems that it was much easier to setup the environment and create our first Ruby on Rails project than we thought.

So far so good, That’s it!!! See ya!!! :)