如何修复 "Failed to restart gunicorn.service: Unit gunicorn.socket not found." 错误?

How to fix "Failed to restart gunicorn.service: Unit gunicorn.socket not found." error?

我正在尝试将 Django 应用程序部署到 DigitalOcean Droplet。我创建了一个系统服务来在启动时启动 gunicorn。

这是我的配置文件:(/etc/systemd/system/gunicorn.service)

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
Environment="DJANGO_SETTINGS_MODULE=core.settings.production"
WorkingDirectory=/home/myproject-api/src
ExecStart=/home/myproject-api/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock core.wsgi:application

[Install]
WantedBy=multi-user.target

当我直接在终端上运行“ExecStart”行时,它起作用了。但是我无法启动gunicorn服务。

当我尝试启动 gunicorn 时出现此错误: 无法启动 gunicorn.service:未找到单位 gunicorn.socket。

我检查了 gunicorn 可执行文件,它存在:

test -f /home/myproject-api/env/bin/gunicorn && echo "Gunicorn exists."

我可以使用 gunicorn --bind 0.0.0.0:8000 core.wsgi 命令 运行 服务器。当我运行这样时,我可以使用服务器的IP地址访问服务器。

通常,套接字文件应该在我启动服务器时创建。我还尝试使用“touch /run/gunicorn.sock”创建套接字文件,但没有成功。

我仔细检查了文件名和目录名。没记错。

我该如何解决这个问题?

我通过创建 /etc/systemd/system/gunicorn.socket 文件解决了这个问题:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

在 Ubuntu 20 中,我们必须为 运行 gunicorn 服务创建此文件。