Sub topic 11
Keeping the App Running with systemd
Assume you know basic Linux commands, file permissions, HTTP status codes, and how to run your PHP or Python application manually. This chapter uses a Python application served by Gunicorn. PHP-FPM follows the same service-management ideas, but its package usually provides the unit file for you.
Why use systemd?
systemd is Linux's service manager. A systemd service is a supervised process with a defined user, working directory, startup command, restart policy, and log destination.
Running Gunicorn from an SSH session is useful for testing, but the process may stop when the session ends. A systemd unit starts the application during boot and can restart it after a failure.
A typical request path looks like this:
Create a service unit
Store a site-specific unit in /etc/systemd/system/myapp.service. The service runs as the unprivileged www-data user and uses a virtual environment.
[Unit]
Description=Gunicorn for myapp
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/srv/myapp
RuntimeDirectory=gunicorn
ExecStart=/srv/myapp/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn/app.sock app:application
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.targetImportant directives include:
| Directive | Purpose |
|---|---|
| `User` and `Group` | Run the application without root privileges |
| `WorkingDirectory` | Set the directory used for imports and relative paths |
| `ExecStart` | Define the complete command that starts the app |
| `RuntimeDirectory` | Create a temporary runtime directory under `/run` |
| `Restart=on-failure` | Restart the process after an unexpected failure |
The app:application value means “load the application callable from app.py.” Make sure the service user can read the application and virtual-environment files. It must also be able to create or access the Unix socket directory.
Load, start, and inspect the service
After creating or changing a unit, make systemd reread its configuration:
sudo systemctl daemon-reload
sudo systemctl enable --now myapp.service
sudo systemctl status myapp.serviceenable adds the service to the boot sequence. --now starts it immediately. Check the service before testing through Nginx.
Useful commands are:
sudo systemctl restart myapp.service
sudo systemctl stop myapp.service
sudo journalctl -u myapp.service -n 50 --no-pager
sudo journalctl -u myapp.service -fjournalctl reads the systemd journal. The -f option follows new messages as they arrive.
Common failures
If Nginx returns 502 Bad Gateway, first check whether the service is active and whether the socket exists. A wrong WorkingDirectory, missing virtual environment, import error, or permission problem can stop Gunicorn before it creates the socket.
If the service starts and immediately exits, inspect the journal:
sudo journalctl -u myapp.service -b --no-pagerThe -b option limits output to the current boot. Test the ExecStart command manually as the service user when possible. This often reveals missing files or environment variables.
Exercise
Why should an application service normally use an unprivileged user instead of root?
Show answer
A compromised application then has fewer permissions, limiting the files and system resources an attacker can access.