Nginx,Gunicorn.Django:找不到应用程序:'blog_venv.wsgi'

时间:2016-07-17 16:02:59

标签: python django nginx gunicorn

文件结构

├── /var/www/
│   ├── blog_venv
│   |   |── blog_project
        |   |── manage.py
        |   |── blog
        |   |── blog_venv
        |      |── settings.py
        |      |── wsgi.py
        ├── gunicorn_start.sh
        ├── run
        └── venv
            |── bin
            |── include
            └── lib

这是我的wsgi.py

import os

from django.core.wsgi import get_wsgi_application
import django.core.handlers.wsgi
os.environ["DJANGO_SETTINGS_MODULE"] = "blog_venv.settings"
try:
    application = get_wsgi_application()
except Exception:
    print('SWGI exception*******************')

这是我的nginx.conf(int /etc/nginx/nginx.conf)

http {

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

upstream test_server {
  server unix:/var/www/blog_venv/run/gunicorn.sock fail_timeout=10s;
}

....

location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
        proxy_pass http://test_server;
        break;
    }
 }
}

这是我的gunicorn_start.sh

#!/bin/bash
NAME="blog_venv"                              
DJANGODIR=/var/www/blog_venv/blog_project   
SOCKFILE=/var/www/blog_venv/run/gunicorn.sock       
USER=nginx                                      
GROUP=www-data                                   
NUM_WORKERS=1                                    
DJANGO_SETTINGS_MODULE=blog_venv.settings             
DJANGO_WSGI_MODULE=blog_venv.wsgi                    

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /var/www/blog_venv/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves     (do not use --daemon)
exec /var/www/blog_venv/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user $USER \
--bind=unix:$SOCKFILE

当我编写“sudo ./gunicorn_start.sh”命令时,我收到此错误

(venv) jie@ubuntu:/var/www/blog_venv$ sudo ./gunicorn_start.sh 
[sudo] password for jie: 
Starting blog_venv as root
[2016-07-17 07:44:33 -0700] [8450] [INFO] Starting gunicorn 19.6.0
[2016-07-17 07:44:33 -0700] [8450] [INFO] Listening at:     unix:/var/www/blog_venv/run/gunicorn.sock (8450)
[2016-07-17 07:44:33 -0700] [8450] [INFO] Using worker: sync
[2016-07-17 07:44:33 -0700] [8457] [INFO] Booting worker with pid: 8457
SWGI exception*******************
Failed to find application: 'blog_venv.wsgi'
[2016-07-17 14:44:33 +0000] [8457] [INFO] Worker exiting (pid: 8457)
[2016-07-17 07:44:33 -0700] [8450] [INFO] Shutting down: Master
[2016-07-17 07:44:33 -0700] [8450] [INFO] Reason: App failed to load.

然后,我尝试编写“./gunicorn_start.sh”命令并收到其他错误

(venv) jie@ubuntu:/var/www/blog_venv$ ./gunicorn_start.sh 
Starting blog_venv as jie
[2016-07-17 07:41:00 -0700] [8433] [INFO] Starting gunicorn 19.6.0
[2016-07-17 07:41:00 -0700] [8433] [ERROR] Retrying in 1 second.
[2016-07-17 07:41:01 -0700] [8433] [ERROR] Retrying in 1 second.
[2016-07-17 07:41:02 -0700] [8433] [ERROR] Retrying in 1 second.
[2016-07-17 07:41:03 -0700] [8433] [ERROR] Retrying in 1 second.
[2016-07-17 07:41:04 -0700] [8433] [ERROR] Retrying in 1 second.
[2016-07-17 07:41:05 -0700] [8433] [ERROR] Can't connect to   /var/www/blog_venv/run/gunicorn.sock

那么,在这种情况下我该怎么办?使用whic命令是当前的,如何解决问题?

0 个答案:

没有答案