How to configure Ruby on Rails in CentOS 7 with Nginx, uWSGI and Ruby 2.3

How to configure Ruby on Rails in CentOS 7 with Nginx, uWSGI and Ruby 2.3

In this howto, we will create a Ruby on Rails application and serve it with Nginx and uWSGI from our CentOS 7 server.

Nginx is the web server and uWSGI is the application server container which will be executing the ruby code.

Nginx natively supports the WSGI protocol used to comunicate with uWSGI, so there is no more dependencies.

You can choose to use either MariaDB or PostgreSQL with Rails. So, first install the required base packages needed
to build the gems and then the packages required for either one of the databases.

Install the base packages:

yum install nginx uwsgi gcc make zlib zlib-devel gcc-c++ patch readline-devel libyaml-devel
libffi-devel openssl-devel bzip2 autoconf automake libtool bison curl sqlite-devel uwsgi-devel uuid-devel libuuid-devel
libcap-devel libxml2-devel libxslt-devel

If you want MariaDB, install these:

yum install mariadb mariadb-server mariadb-devel

Or, if you want PostgreSQL, install these:

yum install postgresql postgresql-server postgresql-devel

Now, we need to install RVM.

RVM, or Ruby Version Manager, help us manage our installed rubies.

curl -sSL | gpg --import
curl -sSL | gpg --import
curl -sSL | bash -s stable --ruby
source /usr/local/rvm/scripts/rvm
rvm get stable --autolibs=enable

Next, install Ruby. In this case, we are going for 2.3.1:

rvm install ruby-2.3.1

Now, set our ruby as default:

rvm --default use ruby-2.3.1

We are ready to update out gems:

gem update --system

Also, you can disable building the documentation for each gem being installed, because this takes time:

echo "gem: --no-document" > ~/.gemrc

Ruby on Rails requires NodeJS for some components, so we need to install it too:

curl -sL | bash
yum install nodejs

Now, if you chose MariaDB as the database, install the gem:

gem install mysql2

But, if it was PostgreSQL, install:

gem install pg

Finally, install Rails. We are going for 5.0.0

gem install rails -v 5.0.0

With this, we have Rails installed at last. But now comes the application and web server.

Because i use Django too, i'm biased towards uWSGI, so uWSGI it is! But you can use Puma too!

cd /usr/src/uwsgi/
uwsgi --build-plugin "plugins/rack" rack
mkdir /usr/lib64/uwsgi
mv /usr/lib64/uwsgi

Now, we only need to create our app and configure uWSGI and Nginx.

First, create the rails application in the directory of your choice:

mkdir /var/www
cd /var/www
rails new railsdemo

Next, we need to modify the uWSGI main configuration file to use it in Emperor mode.
The file is located in /etc/uwsgi.ini


Then, configure the uWSGI vassal for your Rails application.
This file will be in /etc/uwsgi.d/railsdemo.ini


Now, the Nginx configuration for the site, located in /etc/nginx/conf.d/railsdemo.conf

upstream railsdemo {
server unix:///var/www/railsdemo/railsdemo.socket;
server {
listen 80;
server_name default_server;
location / {
uwsgi_pass railsdemo;
include /etc/nginx/uwsgi_params;

Almost there. Now enable and restart the services.

systemctl enable nginx
systemctl restart nginx
systemctl enable uwsgi
systemctl restart uwsgi

And done! Now you can open the IP address of your web server in your browser to be greeted by the Rails
start page!