uWSGI is a new-style daemon for systemd.
It can notify status change and readyness.
When uWSGI detects it is running under systemd, the notification system is enabled.
Adding the Emperor to systemd¶
One approach to integrate uWSGI apps with your init system is using the Emperor.
Your init system will talk only with the Emperor that will rule all of the apps itself.
Create a systemd service file (you can save it as /etc/systemd/system/emperor.uwsgi.service)
Be careful with some systemd versions (e.g. 215 in Debian Jessie), since SIGQUIT signal will trash the systemd services. Use KillSignal=SIGTERM + “die-on-term” UWSGI option there.
[Unit] Description=uWSGI Emperor After=syslog.target [Service] ExecStart=/root/uwsgi/uwsgi --ini /etc/uwsgi/emperor.ini # Requires systemd version 211 or newer RuntimeDirectory=uwsgi Restart=always KillSignal=SIGQUIT Type=notify StandardError=syslog NotifyAccess=all [Install] WantedBy=multi-user.target
Then run it
systemctl start emperor.uwsgi.service
And check its status.
systemctl status emperor.uwsgi.service
You will see the Emperor reporting the number of governed vassals to systemd (and to you).
emperor.uwsgi.service - uWSGI Emperor Loaded: loaded (/etc/systemd/system/emperor.uwsgi.service) Active: active (running) since Tue, 17 May 2011 08:51:31 +0200; 5s ago Main PID: 30567 (uwsgi) Status: "The Emperor is governing 1 vassals" CGroup: name=systemd:/system/emperor.uwsgi.service ├ 30567 /root/uwsgi/uwsgi --ini /etc/uwsgi/emperor.ini ├ 30568 /root/uwsgi/uwsgi --ini werkzeug.ini └ 30569 /root/uwsgi/uwsgi --ini werkzeug.ini
You can stop the Emperor (and all the apps it governs) with
systemctl stop emperor.uwsgi.service
emperor.ini could look like this (www-data is just an anonymous user)
NOTE: DO NOT daemonize the Emperor (or the master) unless you know what you are doing!!!
[uwsgi] emperor = /etc/uwsgi/vassals uid = www-data gid = www-data
If you want to allow each vassal to run under different privileges, remove the
gid options from the emperor configuration (and please read the Emperor docs!)
Using the previous service file all of the Emperor messages go to the syslog. You can avoid it by removing the
If you do that, be sure to set a
--logto option in your Emperor configuration, otherwise all of your logs will be lost!
Putting sockets in /run/¶
On a modern system, /run/ is mounted as a tmpfs and is the right place to put sockets and pidfiles into. To have systemd automatically create a /run/uwsgi/ subdirectory with the correct user/group ownership, as well as cleaning up the directory when the daemon is stopped, add
to the [Service] section of your systemd uwsgi unit file. This
RuntimeDirectory parameter requires systemd version 211 or newer. For older versions of systemd, create a systemd-tmpfiles configuration file (you can save it as /etc/tmpfiles.d/emperor.uwsgi.conf):
d /run/uwsgi 0755 www-data www-data -
Starting from uWSGI 0.9.8.3 socket activation is available. You can setup systemd to spawn uWSGI instances only after the first socket connection.
[Unit] Description=Socket for uWSGI Emperor [Socket] # Change this to your uwsgi application port or unix socket location ListenStream=/tmp/uwsgid.sock [Install] WantedBy=sockets.target
Then disable the service and enable the socket unit.
# systemctl disable emperor.uwsgi.service # systemctl enable emperor.uwsgi.socket
When using Systemd socket activation, you do not need to specify any socket in your uWSGI configuration; the instance will inherit the socket from Systemd.
To have uWSGI serve HTTP (instead of the binary uwsgi protocol) under Systemd socket activation,
http; for instance, in an INI, do this:
[uwsgi] protocol = http wsgi = ... ...
One service per app in systemd¶
Another approach is to let systemd handle starting individual apps while taking advantage of systemd template unit files, and of course socket activation. Each app will run under its own user.
[Unit] Description=Socket for uWSGI app %i [Socket] ListenStream=/var/run/uwsgi/%i.socket SocketUser=www-%i SocketGroup=www-data SocketMode=0660 [Install] WantedBy=sockets.target
[Unit] Description=%i uWSGI app After=syslog.target [Service] ExecStart=/usr/bin/uwsgi \ --ini /etc/uwsgi/apps-available/%i.ini \ --socket /var/run/uwsgi/%i.socket User=www-%i Group=www-data Restart=on-failure KillSignal=SIGQUIT Type=notify StandardError=syslog NotifyAccess=all
Now, adding a new app to your system is a matter of creating the appropriate user and enabling the socket and the service. For instance, if one were to configure cgit:
Then configure the ini file
[uwsgi] master = True cheap = True idle = 600 die-on-idle = True # If app is not used often, it will exit and be launched # again by systemd requested by users. manage-script-name = True plugins = 0:cgi cgi = /usr/lib/cgit/cgit.cgi
And last, if applicable, configure your HTTP server the usual way.