HOME | ARTICLES | PROJECTS | MEMORIES | ABOUT | CONTACT |
---|
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 https://rvm.io/mpapis.asc | gpg --import
curl -sSL https://rvm.io/pkuczynski.asc | gpg --import
curl -sSL https://get.rvm.io | 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 https://rpm.nodesource.com/setup_6.x | 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/2.0.17.1
uwsgi --build-plugin "plugins/rack" rack
mkdir /usr/lib64/uwsgi
mv rack_plugin.so /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
[uwsgi]
emperor=/etc/uwsgi.d
Then, configure the uWSGI vassal for your Rails application.
This file will be in /etc/uwsgi.d/railsdemo.ini
[uwsgi]
uid=root
gid=root
project=railsdemo
project_dir=/var/www/railsdemo
chdir=/var/www/railsdemo
socket=/var/www/railsdemo/railsdemo.socket
touch-reload=/var/www/railsdemo/railsdemo.reload
logto=/var/www/railsdemo/uwsgi.log
plugins=rack
processes=10
chmod-socket=666
vacuum=true
master=true
post-buffering=4096
lazy-apps=true
rack=config.ru
rbrequire=rubygems
rbrequire=bundler/setup
env=BUNDLE_GEMFILE=Gemfile
env=RAILS_ENV=development
gemset=default
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!