هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
[Unit]
Description=Shop API
After=network-online.target
Wants=network-online.target
[Service]
User=shop
Group=shop
WorkingDirectory=/opt/shop
EnvironmentFile=/etc/shop/api.env
ExecStart=/opt/shop/.venv/bin/gunicorn --bind 127.0.0.1:8000 --workers 3 app:app
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.targetStep by step
- A user for the app — never root:
- The unit file (quick answer). The important lines:
| Line | Why |
|---|---|
After= / Wants=network-online.target | Start after the network is really up. |
User=, Group= | Run without root rights. |
WorkingDirectory= | The folder the app expects. |
EnvironmentFile= | Settings and secrets from a 600 file, not in the unit. |
ExecStart= | Full path, the program runs in the foreground (no &, no nohup). |
Restart=on-failure | Come back after a crash. |
WantedBy=multi-user.target | Start at boot (with enable). |
- Lock it down — add these lines to
[Service]and test the app still works:
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/shop /var/log/shop
ProtectKernelTunables=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yesProtectSystem=strict makes the whole system read-only for the app, except the paths in ReadWritePaths.
- See the security score (lower is better) and what is still open:
systemd-analyze security shop-api | tail -1
→ Overall exposure level for shop-api.service: 3.8 OK- Logs: the app logs to stdout/stderr; read them with:
journalctl -u shop-api -f- Change it later with
systemctl edit shop-api(a drop-in), thendaemon-reloadandrestart.
Check that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
status=203/EXEC | Wrong path in ExecStart, or not executable. | Full path; ls -l; on RHEL check SELinux labels (restorecon). |
status=217/USER | The user does not exist. | useradd --system …. |
| Starts, then stops at once | The program goes to the background (forks). | Run it in the foreground, or Type=forking with PIDFile=. |
| Works by hand, not as a service | Different user, env or working dir. | EnvironmentFile, WorkingDirectory; test with sudo -u shop …. |
Read-only file system after hardening | ProtectSystem=strict. | Add the path to ReadWritePaths=. |
| Changes have no effect | No daemon-reload. | systemctl daemon-reload && systemctl restart shop-api. |
Go further
- Command pages: systemctl, systemd-analyze, journalctl, useradd.
- Next how-tos: Give a service its environment variables, Limit CPU and memory of a service, Restart a service automatically when it crashes, Create a Python virtual environment.
- Learn it: RHCSA — Service unit, RHCSA — Your own service, RH124 — Unit files.
- Practise: Lab: run a rootless web container as a systemd service.
1Why must ExecStart run the app in the foreground?
systemd watches the main process; a program that goes to the background looks finished and is stopped or restarted.
2What does ProtectSystem=strict do?
The whole file system is read-only for the service, except the paths listed in ReadWritePaths=.