gunicorn.service: 在步骤 CHDIR 时失败:*/env/bin/gunicorn:没有那个文件或目录。

3

gunicorn.service无法打开WorkingDirectory和gunicorn可执行文件。我认为这是有关权限的问题,但我不知道如何解决。

sudo systemctl status gunicorn:

Mar 15 12:12:42 ns1.localhost.com systemd[1]: Started gunicorn daemon.
Mar 15 12:12:42 ns1.localhost.com systemd[16439]: gunicorn.service: Changing to the requested working directory failed: No such file or directory
Mar 15 12:12:42 ns1.localhost.com systemd[16439]: gunicorn.service: Failed at step CHDIR spawning /home/radarkes-api/env/bin/gunicorn: No such file or directory
Mar 15 12:12:42 ns1.localhost.com systemd[1]: gunicorn.service: Main process exited, code=exited, status=200/CHDIR
Mar 15 12:12:42 ns1.localhost.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.

/etc/systemd/system/gunicorn.service:

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

[Service]
User=root
Group=www-data
WorkingDirectory=/home/radarkes-api/src
ExecStart=/home/radarkes-api/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/radarkes-api/src/core.sock core.wsgi:application

[Install]
WantedBy=multi-user.target

我可以手动运行/home/radarkes-api/env/bin/gunicorn,以下是输出结果,这意味着我可以访问gunicorn可执行文件:

usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: No application module specified.

我尝试了:

  • 在ExecStart的开头添加“source /home/radarkes-api/env/bin/activate &&”。
  • chmod -R 777 /home/radarkes-api

tree -d -L 2 /home/radarkes-api/:

radarkes-api/
├── env
│   ├── bin
│   └── lib
└── src
    ├── api
    └── core

我该怎样解决这个问题?

你能解决它吗?如果可以的话,请友好地通知我。我目前也遇到了同样的问题。 - undefined
我解决了,但是我不记得怎么做了,抱歉。 - undefined
我之前使用了一篇2016年的过时Digital Ocean文章。最后,通过阅读这篇更新的文章,我终于解决了问题。在这篇文章中,gunicorn的设置更简单、更直接:https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04 - undefined
1个回答

2

ExecStart必须是WorkingDirectory的子路径

在你的例子中,/etc/systemd/system/gunicorn.service会是这样的:

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

[Service]
User=root
Group=www-data
WorkingDirectory=/home/radarkes-api/src
ExecStart=/home/radarkes-api/src/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/radarkes-api/src/core.sock core.wsgi:application

[Install]
WantedBy=multi-user.target

PS:

  1. 在你的情况下,最好是创建一个新的虚拟环境,而不是将现有的env文件夹移动到src文件夹中(以避免路径问题)。
  2. 确保你的所有pip3依赖都在requirements.txt文件中可用。
  3. 确保你的.gitignore文件包含了新的虚拟环境文件夹。

然后执行:

$ cd /home/radarkes-api/src
$ python3 -m venv MyEnvName
$ echo 'MyEnvName/' >> .gitignore
$ pip3 install -r requirements.txt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接