使用 gunicorn 部署 django 应用程序,gunicorn 日志中的 Importerror

Deploying django app with gunicorn, Importerror in gunicorn log

我正在尝试根据本教程使用 gunicorn 和 nginx 部署一个 django 应用程序:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04

但是我在让我的 gunicorn 正常工作时遇到了一些问题,

这是我的 /etc/init/gunicorn.conf:

description "Gunicorn application server handling myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid me
setgid www-data


exec myprojectenv/bin/gunicorn --workers 3 --bind  unix:/myproject/myproject.sock myproject.wsgi:application

在我的虚拟环境中 运行:

gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

一切正常。 但是每次我启动 gunicorn 时,我的 gunicorn 错误日志中都会出现此错误:

ImportError: No module named 'myproject'

我检查了所有可能的选项,但没有任何改变。 我有什么遗漏吗? 提前致谢。

编辑:

我在 exec 之前添加了一个 chdir 行:

chdir /myproject/myprojectenv

我还在

ImportError: No module named 'myproject'

当您手动 运行 时,您可能已经在正确的目录中,因此 gunicorn 可以找到 myproject.wsgi 模块。但是当 运行ning 在 upstart 下时,它不会在那个目录中,所以不知道在哪里可以找到该文件。

您可以通过在 exec 行之前添加 chdir /path/to/my/virtualenv 来解决此问题。