How to run uWSGI using /etc/rc.local on Ubuntu 18.04

How to run uWSGI using /etc/rc.local on Ubuntu 18.04

What is WSGI?

WSGI is the Web Server Gateway Interface. It is a specification that describes how a web server communicates with web applications, and how web applications can be chained together to process one request.

WSGI is a Python standard described in detail in PEP 3333. (source: https://wsgi.readthedocs.io/en/latest/what.html)

What is uWSGI?

uWSGI, pronounced "mu wiz gee", is a Web Server Gateway Interface (WSGI) server implementation that is typically used to run Python web applications. (source: https://www.fullstackpython.com/uwsgi.html)

How to set it up?

Ubuntu 18.04 doesn't ship with /etc/rc.local so we need to create it first:

sudo vi /etc/rc.local

Paste in following:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

Now make it executable:

chmod +x /etc/rc.local

You can now reboot your machine:

sudo reboot

and check the status of the service:

sudo systemctl status rc-local.service

Create the vassals directory:

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals

Symlink from the default config directory to your config (Django, Flask, ...) file:

sudo ln -s /var/www/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/

And make a test run the emperor:

sudo uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

Now that we've set up all the bits and pieces, we can finalize the uWSGI setup. Add the following before the end 'exit 0' line in the /etc/rc.local file:

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data