Python Web Application Server

uWSGI is a web application server container

The uWSGI project aims at developing a full stack for building hosting services. It is our choice for serving web applications written in the Python programming language on our Nginx web server.

Prerequisites

python-dev

python-dev is an operating-system level package which contains extended development tools for building Python modules.

Run the following command to install python-dev:

$ sudo apt install python-dev

pip

pip is a package manager which will help us to install the application packages that we need.

Run the following commands to install the probably outdated pip from the Ubuntu software repository and subsequently let it upgrade itself:

$ sudo apt install python-pip
$ sudo -H pip install --upgrade pip

virtualenv

It is best to contain a Python application within its own environment together with all of its dependencies. An environment can be best described (in simple terms) as an isolated location (a directory) where everything resides. For this purpose, a tool called virtualenv is used.

Run the following to install virtualenv using pip:

$ sudo -H pip install virtualenv

uWSGI Installation

uWSGI has a really fast development cycle, so the Ubuntu software packages may not be up to date.

Nevertheless we install it from the Ubuntu software packages:

$ sudo apt install uwsgi uwsgi-emperor uwsgi-plugin-python uwsgi-plugin-python3

After installation completes, there will be …

  • uWSGI binaries
    • /usr/bin/uwsgi_python
    • /usr/bin/uwsgi_python27
    • /usr/bin/uwsgi_python3
    • /usr/bin/uwsgi_python35
  • uWSGI configuration directory in /etc/uwsgi-emperor/
  • SysV service objects in
    • /etc/init.d/uwsgi
    • /etc/init.d/uwsgi-emperor
    • /etc/default/uwsgi and
    • /etc/default/uwsgi-emperor
  • logorotate scripts
    • /etc/logrotate.d/uwsgi
    • /etc/logrotate.d/uwsgi-emperor
  • Defaults and samples in /usr/share/uwsgi/
  • Documentation
    • /usr/share/doc/uwsgi
    • /usr/share/doc/uwsgi-core
    • /usr/share/doc/uwsgi-emperor
    • /usr/share/doc/uwsgi-plugin-python
    • /usr/share/doc/uwsgi-plugin-python3

Debian (and Ubuntu) specific configuration and runtime behavior information is found in /usr/share/doc/uwsgi/README.Debain.gz:

::
$ zless /usr/share/doc/uwsgi/README.Debain.gz

Apps Configuration & Activation

Place your uWSGI app configuration files in the directory /etc/uwsgi/apps-available/

To activate them add a symlink in the directory /etc/uwsgi/apps-enabled/:

$ ln -s /etc/uwsgi/apps-available/example-app.ini /etc/uwsgi/apps-enabled/

It then should be available as system service and started after every reboot:

$ sudo service start uwsgi example-app

Nginx Backend

Nginx can connect to any app by using its distinct socket (/run/uwsgi/app/example-app/socket) as backend server.

Place the following lines outside of any virtual server configuration:

#
# Example uWSGI app backend
#
upstream example-app {
    server unix:/run/uwsgi/app/example-app/socket;
}

This is the general idea, it may vary accroding to instructions of the app.