متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 104System · systemd

How to create a systemd service for your app

An app started with nohup … & dies at the next reboot and has no logs, no restart and no limits. A systemd service fixes all of that: it starts at boot, runs as its own user, restarts after a crash, sends logs to the journal, and can be locked down with a few security lines. This how-to writes a production-ready unit for a Python app (the same works for Node, Java or Go) and checks its security score.

  • 5 الدروس
  • 13 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

104.1

The quick answer

/etc/systemd/system/shop-api.service
[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.target
systemctl daemon-reload
systemctl enable --now shop-api
systemctl status shop-api --no-pager

الأوامر في هذا الدرس

104.2

Step by step

  1. A user for the app — never root:
useradd --system --home-dir /opt/shop --shell /usr/sbin/nologin shop
chown -R shop:shop /opt/shop
  1. The unit file (quick answer). The important lines:
LineWhy
After= / Wants=network-online.targetStart 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-failureCome back after a crash.
WantedBy=multi-user.targetStart at boot (with enable).
  1. Lock it down — add these lines to [Service] and test the app still works:
/etc/systemd/system/shop-api.service (part)
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=yes
ReadWritePaths=/var/lib/shop /var/log/shop
ProtectKernelTunables=yes
ProtectControlGroups=yes
RestrictSUIDSGID=yes

ProtectSystem=strict makes the whole system read-only for the app, except the paths in ReadWritePaths.

  1. 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
  1. Logs: the app logs to stdout/stderr; read them with:
journalctl -u shop-api -f
  1. Change it later with systemctl edit shop-api (a drop-in), then daemon-reload and restart.

104.3

Check that it worked

systemctl is-enabled shop-api
enabled
systemctl is-active shop-api
active
curl -sI http://127.0.0.1:8000/healthz | head -1
ps -o user,pid,cmd -C gunicorn | head -3

The service is enabled and active, it answers, it runs as shop, and after a reboot it is up again.

104.4

Common problems

ProblemCauseFix
status=203/EXECWrong path in ExecStart, or not executable.Full path; ls -l; on RHEL check SELinux labels (restorecon).
status=217/USERThe user does not exist.useradd --system ….
Starts, then stops at onceThe program goes to the background (forks).Run it in the foreground, or Type=forking with PIDFile=.
Works by hand, not as a serviceDifferent user, env or working dir.EnvironmentFile, WorkingDirectory; test with sudo -u shop ….
Read-only file system after hardeningProtectSystem=strict.Add the path to ReadWritePaths=.
Changes have no effectNo daemon-reload.systemctl daemon-reload && systemctl restart shop-api.

104.5

Go further

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=.

كل الأوامر في هذا الفصل