![uWSGI 无法在 Ubuntu 16.04 下与 systemd 一起启动](https://linux22.com/image/701329/uWSGI%20%E6%97%A0%E6%B3%95%E5%9C%A8%20Ubuntu%2016.04%20%E4%B8%8B%E4%B8%8E%20systemd%20%E4%B8%80%E8%B5%B7%E5%90%AF%E5%8A%A8.png)
我正在进行一个有点痛苦的迁移过程,将我们的临时和生产服务器从 Ubuntu 12.04 迁移到 16.04。我正在临时测试迁移,除了让 uWSGI 在 systemd 下启动外,它基本上可以正常工作(它以前在 Upstart 下运行良好)。这没有问题:
uwsgi --ini /etc/uwsgi/my_wsgi.ini
但是运行以下命令不起作用(uWSGI 没有启动,但没有产生错误):
sudo systemctl start uwsgi
我在 /etc/systemd/system/uwsgi.service 中创建了以下服务:
[Unit]
Description=uWSGI Service
[Service]
ExecStart=/usr/local/bin/uwsgi --ini /etc/uwsgi/my_wsgi.ini
Restart=always
RestartSec=5
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
并且 my_wsgi.ini 具有以下内容:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/project/hidden
# Django's wsgi file
module = wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 8
# the socket (use the full path to be safe)
socket = /path/to/socket/hidden
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
# Mercilessly kill this worker if it takes longer than this time to reload
reload-mercy = 8
# Reload workers after the specified amount of managed requests (avoid memory leaks)
max-requests = 5000
# Every request that will take longer than the seconds specified in the harakiri timeout will be dropped and the corresponding worker is thereafter recycled.
harakiri = 90
# Log everything and make daemon
daemonize = /var/log/uwsgi/my.log
# use custom settings file
env = DJANGO_SETTINGS_MODULE=settings.staging
# set pid file for starting/stopping server
pidfile = /path/to/pid/hidden.pid
# See https://docs.newrelic.com/docs/python/python-agent-and-uwsgi
enable-threads = true
single-interpreter = true
运行systemd-analyze verify uwsgi.service
不返回任何内容并sudo journalctl
返回以下内容:
Starting uWSGI Service...
[uWSGI] getting INI configuration from /etc/uwsgi/my_wsgi.ini
Started uWSGI Service.
uwsgi.service: Service hold-off time over, scheduling restart.
Stopped uWSGI Service.
... (repeats a few times) ....
我怀疑这uwsgi.service: Service hold-off time over, scheduling restart.
可能指出了问题所在,但我还没能弄清楚。
答案1
这uwsgi 文档明确指出:
注意:除非您知道自己在做什么,否则不要将 Emperor (或 Master) 变为恶魔!!!
不幸的是,你不知不觉地将 Emperor 变成了恶魔。从你的问题来看:
# Log everything and make daemon daemonize = /var/log/uwsgi/my.log
问题在于 uwsgi 是 systemd 感知的,并且 systemd 单元告诉 systemd,当 uwsgi 准备好开始处理请求时,uwsgi 将通知 systemd。默认情况下,在 systemd 系统上,uwsgi 以此模式启动,除非你告诉它守护进程。在这种情况下,uwsgi才不是与 systemd 对话。这就是为什么 systemd 等待了一段时间并最终放弃等待 uwsgi 告诉 systemd 它已经准备好了。
文档建议,与其让 uwsgi 守护进程直接将日志记录到文件中,不如让它记录到默认的标准错误中,然后让 systemd 拾取日志并记录下来。如果您最终要将日志发送到其他地方(例如 ELK 堆栈),则需要这样做。例如:
[Service]
StandardError=syslog
或者您可以直接从 uwsgi 登录,而无需守护进程。
logto = /var/log/uwsgi/my.log