根据此设置,我已将 gunicorn 设置为服务器两个 Django 站点。这很好用。如果我运行:
# systemctl start gunicorn@my_website.service
然后一切都很好,我的网站正在按预期提供服务。所以我开始在启动时启用它:
# systemctl enable gunicorn@my_website.service
让我们检查它是否已启用:
# systemctl is-enabled gunicorn@my_website.service
enabled
看起来不错。现在,让我们重新启动...
原来我的网站没有启动。nginx 正在工作,似乎 gunicorn 没有做这件事。让我们调查一下:
# systemctl status gunicorn@my_website.service
● gunicorn@my_website.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
Active: inactive (dead)
嗯……这看起来很奇怪。检查自启动以来的日志:
# journalctl -u gunicorn@my_website.service -b
-- Logs begin at Mon 2019-04-08 06:04:03 UTC, end at Thu 2020-10-15 13:23:30 UTC. --
-- No entries --
非常令人费解......甚至没有日志条目!当我手动启动服务时,我们又恢复了操作:
# systemctl start gunicorn@my_website.service
# systemctl status gunicorn@my_website.service
● gunicorn@my_website.service - gunicorn daemon
Loaded: loaded (/etc/systemd/system/[email protected]; indirect; vendor preset: enabled)
Active: active (running) since Thu 2020-10-15 13:25:29 UTC; 30s ago
Main PID: 1272 (gunicorn)
Tasks: 4 (limit: 1151)
CGroup: /system.slice/system-gunicorn.slice/gunicorn@my_website.service
├─1272 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
├─1294 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
├─1297 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
└─1298 /usr/bin/python3 /usr/local/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/my_website/gunicorn.sock my_website.wsgi:application
Oct 15 13:25:29 web systemd[1]: Started gunicorn daemon.
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Starting gunicorn 19.9.0
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Listening at: unix:/home/my_website/gunicorn.sock (1272)
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1272] [INFO] Using worker: sync
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1294] [INFO] Booting worker with pid: 1294
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1297] [INFO] Booting worker with pid: 1297
Oct 15 13:25:29 web gunicorn[1272]: [2020-10-15 13:25:29 +0000] [1298] [INFO] Booting worker with pid: 1298
事实上,我的网站现在可以运行了!但是,为什么它在启用时似乎甚至没有在启动时运行?如何调试这个?
运行 Ubuntu 18.04。
根据要求,[email protected]
文件内容如下:
# cat /etc/systemd/system/[email protected]
[Unit]
Description=gunicorn daemon
After=network.target
PartOf=gunicorn.target
# Since systemd 235 reloading target can pass through
ReloadPropagatedFrom=gunicorn.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/%i/
ExecStart=/usr/local/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/home/%i/gunicorn.sock \
%i.wsgi:application
[Install]
WantedBy=gunicorn.target