uWSGI is still in the field — large, mature, and surprisingly fast when configured correctly. Teams that picked it years ago often have no reason to migrate to Gunicorn, but the configuration surface is wide enough that operators inherit weird tunings. This article sets up uWSGI with the binary uwsgi protocol, Nginx in front, and Emperor mode if you run more than one app on the box.
How to verify
Look at the current uWSGI install and its status:
which uwsgi && uwsgi --version
systemctl status uwsgi --no-pager | head
systemctl status uwsgi-emperor --no-pager | head 2>/dev/null
ls -la /run/uwsgi/ 2>/dev/null
ls /etc/uwsgi/apps-enabled/ 2>/dev/null
sudo ss -lxn | grep uwsgi
If both uwsgi.service and uwsgi-emperor.service are loaded, only one should be enabled — running both produces duplicate workers binding the same sockets.
What’s happening
uWSGI speaks the binary uwsgi protocol over a UNIX socket and Nginx has a built-in uwsgi_pass directive that uses it. That handoff is faster than HTTP because there is no header parsing on either side — the framing is fixed and the protocol overhead is a few bytes per request.
Two operational shapes exist. Single-app mode runs uwsgi --ini /etc/uwsgi/app.ini under one systemd unit. Emperor mode runs a single supervisor process that watches a directory (/etc/uwsgi/vassals/) and spawns a “vassal” per .ini file, restarting any vassal whose .ini is modified. Emperor mode is the right choice when you host multiple Python apps on one VM; single-app is simpler when you do not.
The protocol matters too. http mode makes uWSGI speak plain HTTP — easier to debug, slightly slower. socket mode (socket = /run/uwsgi/app.sock) uses the binary protocol. Use binary in production, HTTP only for local testing.
The procedure
-
Install uWSGI into the project venv (system packages exist but are usually out of date):
sudo -iu deploy /opt/venvs/app/bin/pip install "uwsgi" /opt/venvs/app/bin/uwsgi --version -
Create the app
.ini./etc/uwsgi/apps-available/app.ini:[uwsgi] chdir = /var/www/app module = app.wsgi:application home = /opt/venvs/app master = true processes = 5 threads = 2 socket = /run/uwsgi/app.sock chmod-socket = 660 chown-socket = deploy:www-data vacuum = true die-on-term = true harakiri = 60 max-requests = 1000 buffer-size = 32768 log-master = true logto = /var/log/uwsgi/app.logharakiri = 60kills a worker stuck on one request for 60 s — uWSGI’s equivalent of Gunicorn’stimeout.vacuum = trueremoves the socket file when the master exits. -
For single-app mode, drop a systemd unit at
/etc/systemd/system/uwsgi-app.service:[Unit] Description=uWSGI for app After=network.target [Service] User=deploy Group=www-data RuntimeDirectory=uwsgi ExecStart=/opt/venvs/app/bin/uwsgi --ini /etc/uwsgi/apps-available/app.ini ExecReload=/bin/kill -HUP $MAINPID KillSignal=SIGQUIT Restart=on-failure NotifyAccess=all [Install] WantedBy=multi-user.targetKillSignal=SIGQUITis what uWSGI expects for a clean shutdown. -
For Emperor mode, install Emperor as a separate unit and put vassal
.inifiles in/etc/uwsgi/vassals/:# /etc/systemd/system/uwsgi-emperor.service [Service] ExecStart=/opt/venvs/app/bin/uwsgi --emperor /etc/uwsgi/vassals --master --die-on-term --uid deploy --gid www-data Restart=on-failure KillSignal=SIGQUITtouch /etc/uwsgi/vassals/app.initriggers a graceful reload of just that vassal. -
Point Nginx at the socket using
uwsgi_pass, notproxy_pass:location / { include uwsgi_params; uwsgi_pass unix:/run/uwsgi/app.sock; uwsgi_read_timeout 90s; client_max_body_size 25m; }uwsgi_paramsships with the Nginx distro and sets the protocol’s expected envelope variables.
Operational notes
threads > 1only helps when the WSGI app does meaningful I/O. CPU-bound code with threads will be slower than the same code with moreprocessesbecause of the GIL.- The
master = truedirective turns on the master process that supervises workers. Without it, you have N workers and nothing watching them — never run withoutmaster = truein production. harakirikills workers but does not log the request payload that triggered it. Pair it withharakiri-verbose = trueand you get the URL in the log; without that, you only know “a request was slow”.- Emperor mode broadcasts vassal failures up the chain — set
vassal-config-fallback-throttle = 30so a broken.inidoes not respawn 100 times a second. - uWSGI does not graceful-reload across binary upgrades by default. Use
touch /tmp/uwsgi-app.fifowith themaster-fifooption for chain-reload to roll workers one at a time without dropping connections.
For most new projects Gunicorn is the friendlier choice (see the Gunicorn article) — but inherited uWSGI is fine to keep running if it is tuned. In the engagements Stack Harbor takes over, we standardize the supervisor and the protocol so a host you operated last year still feels like the same one today — that consistency is part of managed operations.