Deploy Ruby Rack Web Apps with uWSGI and Nginx

Advertisement

Advertisement

Introduction

This tutorial demonstrates how to deploy a Ruby web app using the Sinatra framework using uWSGI and nginx as the web server. This uses the config.ru standard for creating a web app. You can use this method to work with several other Ruby web frameworks including Rails. For a more general tutorial of nginx, check out my Nginx Tutorial.

This was tested on Fedora 30. Slight modification might be needed for other distributions.

Prepare the Ruby web app

First, let's create a simple Ruby web application with Sinatra that will be used to test the uWSGI and nginx configuration. Since we are using the config.ru standard, Sinatra is not the only web application framework we can use, but the one chosen for this example. You can see some examples of other frameworks config.ru settings here.

Install Sinatra

For this example, Sinatra framework will be used.

dnf install rubygem-sinatra

Create the project directory

mkdir /opt/rubytest

Create the Ruby Rack web app

This should apply to any Ruby Rack web application, but for this example Sinatra will be used. You have a couple choices with the Sinatra web app. You can put the application code right in to the config.ru file, or use the config.ru file to load a separate file. Choose one of the following methods to create the web app.

Single file

# /opt/rubytest/config.ru
require 'sinatra'

get '/' do
  'Hello'
end

run Sinatra::Application

Multiple files

Note that config.ru is a convention that multiple frameworks support including Rails. Sinatra is just the preferred example for this tutorial. You can see a few examples using different frameworks here.

If you want to use config.ru to simply load up a larger app, you can do it like this with Sinatra:

# /opt/rubytest/config.ru

# Load the `webapp.rb` file from the same directory as `config.ru`
require File.expand_path('webapp', File.dirname(__FILE__))

# Run the class named `WebApp` that should now be loaded
run WebApp

The main web application can be contained in a separate file:

# /opt/rubytest/webapp.rb
require 'sinatra/base'

class WebApp < Sinatra::Base
  get "/" do
    "Hello"
  end
end

Prepare uWSGI

Now let's look at the steps needed to get uWSGI ready to serve up the Ruby web application from the config.ru file.

Install uWSGI

Install uWSGI and the Ruby Rack plugin.

dnf install uwsgi uwsgi-plugin-rack

Configure uWSGI

Create a wsgi ini file in /etc/uwsgi.d/, for example, /etc/uwsgi.d/rubytest.ini.

# /etc/uwsgi.d/rubytest.ini

[uwsgi]
plugins=rack
socket=127.0.0.1:8002
chdir=/opt/rubytest
rack=/opt/rubytest/config.ru
pidfile=/run/uwsgi/rubytest.pid
master=True
vacuum=True

Verify permissions on uwsgi.d

Change ownership of the uwsgi configuration directory so that the uwsi user can have proper access.

chown -R uwsgi:uwsgi /etc/uwsgi.d/

Restart uWSGI

After adding the new uwsgi configuration file and checking the permissions, you should restart the uwsgi service.

systemctl restart uwsgi

Prepare nginx

Now that the Ruby web application is ready with a config.ru file, and uWSGI is ready, nginx is the last piece that needs configuration.

Install nginx

Next, after uwsgi is all set, the final piece is to set up nginx to act as a reverse proxy to the uwsgi socket.

dnf install nginx

Configure nginx

Create a configuration file to act as a reverse proxy for the uWSGI Ruby app.

Make a file in /etc/nginx/conf.d, for example,

/etc/nginx/conf.d/rubytest.conf

server {
  listen 0.0.0.0:80;
  # server_name example.com;
  location / {
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:8002;
  }
}

If you omit the server_name configuration, you might need to turn off the default server block in /etc/nginx/nginx.conf. Otherwise you should set a server_name or configure some more parameters. If you want to add more advanced configuration, like adding SSL, redirecting non-www to www, and more, check out my Nginx Tutorial to learn more. The important part of the configuration is the uwsgi_pass that tells nginx where to get the upstream data.

Restart nginx

After adding the new nginx configuration file, you need to restart nginx.

systemctl restart nginx

Test the server

Everything should now be ready, and you can test it out by opening it with the web browser or using curl to test it out.

curl http://localhost

Check logs

If you need to troubleshoot, or just want to verify things are working well, you can view the logs using journalctl or looking in /var/log/.

journalctl -u uwsgi
journalctl -u nginx
ls /var/log/nginx

Conclusion

You should now have an idea of how to deploy a Ruby web application using nginx, uWSGI, and Sinatra. This knowledge should be useful when deploying other Ruby applications that conform to the config.ru standard.

Reference

Advertisement

Advertisement