我正在关注如何在 Ubuntu 18.04 指南上使用 Postgres、Nginx 和 Gunicorn 设置 Django。
我创建了以下文件 .socket
sudo nano /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
我创建了以下文件 .service
sudo nano /etc/systemd/system/gunicorn.service
指南中的原始 RECOMENDED_FORMATTING-s
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=sammyRECOMENDED_FORMATTING
Group=www-data
WorkingDirectory=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING
ExecStart=/home/sammyRECOMENDED_FORMATTING/myprojectdirRECOMENDED_FORMATTING/myprojectenvRECOMENDED_FORMATTING/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
myprojectRECOMENDED_FORMATTING.wsgi:application
[Install]
WantedBy=multi-user.target
我如何格式化我自己的版本我在服务器上的项目文件夹之外拥有我的虚拟环境
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=SERVER_USER
Group=www-data
WorkingDirectory=/home/SERVER_USER/MAIN_PROJECT_FOLDER
ExecStart=/home/SERVER_USER/ven/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/home/SERVER_USER/MAIN_PROJECT_FOLDER/MAINAPPLICATION_FOLDER.sock \
MAINAPPLICATION_FOLDER.wsgi:application
[Install]
WantedBy=multi-user.target
我也尝试过按照最初的建议保留这些
--bind unix:/run/gunicorn.sock \
比我尝试执行以下代码
sudo systemctl start gunicorn
错误信息 1
Failed to start gunicorn.service: Unit gunicorn.service not found.
我也试过2
sudo systemctl start gunicorn.socket
错误信息 2
Failed to start gunicorn.socket: Unit gunicorn.socket is not loaded properly: Invalid argument.
See system logs and 'systemctl status gunicorn.socket' for details.
为了解决这个问题,我尝试过
- https://stackoverflow.com/questions/40711747/failed-to-start-gunicorn-service-unit-gunicorn-service-not-found这指向了我正在做的完全相同的指南,除了旧版本的 linux .
- 这不是相同的代码并且没有回答Unit gunicorn.service failed to load: No such file or directory
- 运行:
systemctl status gunicorn.socket
结果:Warning: The unit file, source configuration file or drop-ins of gunicorn.socket changed on disk ● gunicorn.socket - gunicorn daemon Loaded: error (Reason: Invalid argument) Active: inactive (dead)
- 运行:
systemctl is-enabled gunicorn.socket
结果:enabled
- 运行:
systemctl is-enabled gunicorn.service
结果:Failed to get unit file state for gunicorn.service: No such file or directory
您的服务中有 [Install] 部分,这意味着您需要运行命令
sudo systemctl enable gunicorn.service
以在系统启动时启动服务。运行命令后,将在目录中创建指向该文件的符号链接
/etc/systemd/system/multi-user.target.wants/
。请注意,您还需要创建名为的套接字文件
gunicorn.socket
并需要启用它。启用后,您可以通过运行命令来激活套接字sudo systemctl start gunicorn.socket
。您不需要运行该服务,因为 systemd 会找出依赖关系。您可以使用 来检查服务的状态
systemctl status gunicorn.service
。就我而言,服务器没有创建
gunicorn.socket
文件。所以,我必须自己创建文件并启用它。go to:
/etc/systemd/system
并通过命令检查所有可用的文件和目录ls
。sudo nano ~/etc/systemd/system/gunicorn.socket
如果文件不存在,请尝试通过:或.从第一个文件重新创建文件sudo nano gunicorn.socket
。然后先通过: 启用它
sudo systemctl enable gunicorn.socket
,在启用 do: 之后sudo systemctl start gunicorn.socket
。如果一切正常,你会看到文件的状态
gunicorn.socket
是活动的,运行命令后sudo systemctl status gunicorn.socket
你会看到: