【问题标题】:project wsgi not found when started automatically but not manually自动启动但未手动启动时未找到项目wsgi
【发布时间】:2014-03-14 16:14:56
【问题描述】:

我不明白为什么当主管尝试自动启动应用程序时(例如在服务器启动时),我收到关于找不到我的“project.wsgi”模块的错误。

2014-02-15 05:13:05 [1011] [INFO] Using worker: sync
2014-02-15 05:13:05 [1016] [INFO] Booting worker with pid: 1016
2014-02-15 05:13:05 [1016] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
    worker.init_process()
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
    self.wsgi = self.app.wsgi()
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
    self.callable = self.load()
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
    return self.load_wsgiapp()
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/var/local/sites/myproject/venv/local/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
    __import__(module)
ImportError: No module named myproject.wsgi

而我没有收到此错误,当我手动执行时它工作正常:

sudo supervisorctl start myapp

有什么不同?

谢谢

更新:

主管配置文件:

[program:myproject]
command=/var/local/sites/myproject/run/gunicorn_start                       ; Command to start app
user=myproject                                             ; User to run as
autostart=true
autorestart=true
loglevel=info
redirect_stderr=false
stdout_logfile=/var/local/sites/myproject/logs/supervisor-myproject-stdout.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/local/sites/myproject/logs/supervisor-myproject-stderr.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB

/var/local/sites/myproject/run/gunicorn_start:

#!/bin/bash

NAME="myproject_app"         # Name of the application
USER=myproject          # the user to run as
GROUP=myproject        # the group to run as

NUM_WORKERS=3                       # how many worker processes should Gunicorn spawn

# Logs config
LOG_LEVEL=info
ACCESS_LOGFILE=/var/local/sites/myproject/logs/gunicorn-myproject-access.log
ERROR_LOGFILE=/var/local/sites/myproject/logs/gunicorn-myproject-error.log

echo "Starting $NAME"        

exec envdir /var/local/sites/myproject/env_vars /var/local/sites/myproject/venv/bin/gunicorn myproject.wsgi:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --log-level=$LOG_LEVEL \
  --bind=unix:/tmp/myproject.gunicorn.sock \
  --access-logfile=$ACCESS_LOGFILE \
  --error-logfile=$ERROR_LOGFILE

【问题讨论】:

  • 显示你的 site.conf 管理员配置
  • 我已将其添加到我的帖子中。谢谢。
  • 我还添加了主管使用的 gunicorn 启动脚本。您认为这可能与我使用 envdir 的事实有关吗?但是我仍然不明白为什么当我手动运行脚本时它会起作用......谢谢

标签: django gunicorn supervisord


【解决方案1】:

我认为您应该将directory 添加到您的主管配置文件中。这是我的模板。我在每个项目中都使用它并且工作正常:

[program:PROJECT_NAME]
command=/opt/sites/PROJECT_NAME/env/bin/gunicorn -c /opt/sites/etc/gunicorn/GUNICORN_CONF.conf.py PROJECT_NAME.wsgi:application
directory=/opt/sites/PROJECT_NAME
environment=PATH="/opt/sites/PROJECT/env/bin"
autostart=true
autorestart=false
redirect_stderr=True
stdout_logfile=/tmp/PROJECT_NAME.stdout

【讨论】:

  • 我试过了 => 没改。服务器启动时,应用程序无法自动启动。当我手动执行时,它是否与使用 sudo 运行它有关?
  • 您无法通过使用 sudo su - username 然后运行 ​​gunicorn_start 文件来测试您使用的用户是否工作
【解决方案2】:

我以前也有同样的问题。我在 gunicorn_start 中使用以下行而不是 envdir。我在位于 /env/nafd/ 的虚拟环境中运行 django 应用程序,而我的 django 应用程序位于 /env/nafd/nafd_proj

..
DJANGODIR=/to/path/app_proj
cd $DJANGODIR
source ../bin/activate`

exec ../bin/gunicorn nafd_proj.wsgi:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --log-level=$LOG_LEVEL \
  --bind=unix:/tmp/myproject.gunicorn.sock \
  --access-logfile=$ACCESS_LOGFILE \
  --error-logfile=$ERROR_LOGFILE`

【讨论】:

    【解决方案3】:

    很明显,但值得一提:

    检查“supervisord”守护进程是否正在运行(服务主管状态)。

    这是我在这里的以下设置,使用带有 WSGI (Gunicorn) 的 Flask 应用程序,通过 Supervisor 控制,它运行良好。

    Flask 应用

    root@ilg40:~# ll /etc/tdm/flask/
    total 1120
    drwx------ 5 root root   4096 Jan 24 19:47 ./
    drwx------ 3 root root   4096 Jan 23 00:20 ../
    -r-------- 1 root root   1150 Aug 31 17:54 favicon.ico
    drw------- 2 root root   4096 Jan 13 22:51 static/
    -rw------- 1 root root 883381 Jan 23 20:09 tdm.log
    -rwx------ 1 root root  73577 Jan 23 21:37 tdm.py*
    -rw------- 1 root root  56445 Jan 23 21:37 tdm.pyc
    drw------- 2 root root   4096 Jan 23 20:08 templates/
    -rw-r--r-- 1 root root    493 Jan 23 22:42 wsgi.py
    -rw-r--r-- 1 root root    720 Jan 23 22:42 wsgi.pyc
    srwxrwxrwx 1 root root      0 Jan 24 19:47 wsgi.sock=
    

    主管配置文件

    root@ilg40:~# cat /etc/supervisor/conf.d/wsgi_flask.conf 
    [program:wsgi_flask]
    command = gunicorn --preload --bind unix:/etc/tdm/flask/wsgi.sock --workers 4 --pythonpath /etc/tdm/flask wsgi 
    process_name = wsgi_flask
    autostart = true
    autorestart = true
    stdout_logfile = /var/log/wsgi_flask/wsgi_flask.out.log
    stderr_logfile = /var/log/wsgi_flask/wsgi_flask.err.log
    

    向 Supervisord 更新新流程

    root@ilg40:~# supervisorctl update
    wsgi_flask: added process group
    

    检查进程状态

    root@ilg40:~# supervisorctl status wsgi_flask
    wsgi_flask                       RUNNING    pid 1129, uptime 0:29:12
    

    注意:在上面的设置中,我没有使用virtualenv,我认为这可能需要为进程配置目录变量,而且,为 command 变量配置环境 PATH(添加 env PATH="$PATH:/the/app/path" gunicorn...),因为只有 gunicorn、flask 等位于在 virtualenv 内部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      相关资源
      最近更新 更多