uWSGI

uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C.

There are alternatives written in Python such as gunicorn.

Installation

Install the uwsgi package. Plugins need to be installed separately (their package names start with uwsgi-plugin-).

Configuration

Web applications served by uWSGI are configured in /etc/uwsgi/, where each of them requires its own configuration file (ini-style). Details can be found in the uWSGI documentation.

Alternatively, you can run uWSGI in Emperor mode (configured in /etc/uwsgi/emperor.ini). It enables a single uWSGI instance to run a set of different apps (called vassals) using a single main supervisor (called emperor).

Note: The plugins must be explicitly loaded before their options can be used, otherwise the options will not be recognized. This can be done with the --plugins command-line option or with the plugins variable in the configuration file.

Web applications

uWSGI supports many different languages and thus also many web applications. As an example the configuration file /etc/uwsgi/example.ini and the prior installation of the plugin needed for your web application is assumed.

Python

The following is a simple example for a Python application.

/etc/uwsgi/example.ini
[uwsgi]
chdir = /srv/http/example
module = example
plugins = python

It is also possible to run uWSGI separately with the following syntax for instance:

$ uwsgi --socket 127.0.0.1:3031 --plugin python2 --wsgi-file ~/foo.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --uid --gid

You should avoid running this command as root.

Note: Pay attention to operational mode in use, preforking without --lazy-apps may cause non-obvious behavior. By default the Python plugin does not initialize the GIL. This means your app-generated threads will not run. If you need threads, remember to enable them with enable-threads. Running uWSGI in multithreading mode (with the threads options) will automatically enable threading support. This "strange" default behaviour is for performance reasons, no shame in that.

PHP

The following is a simple example for a PHP based website.

Web server

uWSGI can be the backend to many web servers, that support the forwarding of access. The following are examples for configurations.

Nginx

nginx can redirect access towards unix sockets or ports (on localhost or remote machine), depending on your web application.

Nginx (in chroot)

First create ini file that will point to your application:

/etc/uwsgi/application1.ini
[uwsgi]
chroot = /srv/http
chdir = /www/application1
wsgi-file = application1.py
plugins = python
socket = /run/application1.sock
uid = http
gid = http
threads = 2
stats = 127.0.0.1:9191
vacuum = true

Since we are chrooting to /srv/http above configuration will result in following unix socket being created

You will need to disable notifications within your service file:

After modification make sure to reload to incorporate the new or changed units.

You are then free to enable and start .

Edit and add new section within it that would contain at least following:

Make sure to now restart to have your application1 be served at 127.0.0.1.

Running uWSGI

If you plan on using a web application all the time (without it being activated on demand), you can simply start and enable .

If you plan on having your web application be started on demand you can start and enable .

To use the Emperor mode, start and enable .

To use socket activation of this mode start and enable .

Tips and tricks

Some functionality, that uWSGI offers is not accessible by using the systemd service files provided in the official repositories. Changes to them are explained in the following sections. For further information see .

Socket activation

Using socket activation, you want to

  • direct your web server to a unix socket and thereby start your uWSGI instance running the application
  • you most likely want to have the application be closed by uWSGI after a certain idle time
  • you want your web server be able to start the application again, once it is accessed

uWSGI offers settings, with which you can have the instance close the application:

The current file however does not allow this, because systemd treats non-zero exit codes as failure and thereby marking the unit as failed and additionally the directive makes a closing after idle time useless. A fix for this is to add the exit codes, that uWSGI may provide after closing an application by itself to a list, that systemd will treat as success by using the SuccessExitStatus directive (for further information see ).

This will allow for proper socket activation with kill-after-idle functionality.

Hardening uWSGI service

Web applications are exposed to the wild and depending on their quality and the security of their underlying languages, some are more dangerous to run, than others. A good way to start dealing with possible unsafe web applications is to jail them. systemd has some functionality, that can be put to use. Have a look at the following example (and for further information see systemd.exec(5) and ):

Accessibility of uWSGI socket

The default (per application) socket unit () in uwsgi allows read and write access to any user on the system. However, systemd allows for a more finely granulated access management (see ), with which the access to a unix socket can be made more restrictive.

By creating it below a webapp specific directory below (needs to be created using tmpfiles beforehand - for reference see Web application package guidelines) and modifying its group and file permissions, the socket is only accessible to root and the web server and allows the web application to run as its own user:

Troubleshooting

AH00957: uwsgi: attempt to connect to 127.0.0.1:0 (*) failed

The default uWSGI port (3031) does not work (currently?) with Apache httpd server. See for details.

gollark: It would be important to make it reasonably easy to add and update packages.
gollark: Well, it would be less useful if there wasn't a good central repo too.
gollark: "Search packages" is `pacman -Ss [whatever]`, "install" is `pacman -S [whatever]`, "update repos and update all packages" (it is apparently unsafe to update only individual packages) is `pacman -Syu`.
gollark: You pick a "subcommand" with a capital-letter flag like `-S` (sync, which seems to be a fancy word for "Install packages"), `-Q` (query information aboud stuff) and then pass extra flags to configure how that works.
gollark: > what's a pacman-like CLI?Arch Linux (btw I use that) has a neat package manager called `pacman`.> what counts as package updating support?Updating packages without breaking things horribly, including not overwriting user-edited (config) files.> and library interface as in an API you can use from scripts?Precisely.

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.