Django,Virtualenv,nginx + uwsgi导入模块wsgi错误

6

我正在尝试在使用nginx、virtualenv和uwsgi的staging服务器上设置我的Django项目,但是我一直收到导入模块wsgi错误的报错。

如果有社区可以找到答案,那么在这里...提前感谢大家。

这是我的配置文件:

我的Django项目中的uwsgi.py:

import os
import sys 
import site

site.addsitedir(os.path.join(os.environ['WORKON_HOME'],'project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Nginx配置

# nginx configuration for project.maumercado.com

server {
    server_name project.maumercado.com;
    access_log /home/ubuntu/logs/project/nginx/access.log;
    error_log /home/ubuntu/logs/project/nginx/error.log;

    location / {
            uwsgi_pass unix:/tmp/uwsgi.sock;
            include /etc/nginx/uwsgi_params;
    }

    location /static {
            root /home/ubuntu/django-projects/project/project/media;
    }
    location /media {
            root /home/ubuntu/django-projects/project/project/media;
    }
}

还有,我的uwsgi.conf文件

# file: /etc/init/uwsgi.conf 
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid www-data \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging/ \
--socket /tmp/uwsgi.sock \
--chmod-socket \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

Nginx日志除了在access.log中记录500错误外,没有任何其他信息,因此这里是uwsgi.log:

Mon Feb  6 13:58:23 2012 - *** Starting uWSGI 1.0.2.1 (32bit) on [Mon Feb  6 13:58:23 2012] ***
Mon Feb  6 13:58:23 2012 - compiled with version: 4.4.5 on 06 February 2012 12:32:36
Mon Feb  6 13:58:23 2012 - current working directory: /
Mon Feb  6 13:58:23 2012 - detected binary path: /home/ubuntu/ve/project/bin/uwsgi
Mon Feb  6 13:58:23 2012 - setuid() to 1000
Mon Feb  6 13:58:23 2012 - your memory page size is 4096 bytes
Mon Feb  6 13:58:23 2012 - chmod() socket to 666 for lazy and brave users
Mon Feb  6 13:58:23 2012 - uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Mon Feb  6 13:58:23 2012 - Python version: 2.6.6 (r266:84292, Sep 15 2010, 16:02:57)  [GCC 4.4.5]
Mon Feb  6 13:58:23 2012 - Set PythonHome to /home/ubuntu/ve/project
Mon Feb  6 13:58:23 2012 - Python main interpreter initialized at 0x9a9d740
Mon Feb  6 13:58:23 2012 - your server socket listen backlog is limited to 100 connections
Mon Feb  6 13:58:23 2012 - *** Operational MODE: preforking ***
Mon Feb  6 13:58:23 2012 - added /home/ubuntu/django-projects/project/ to pythonpath.
ImportError: No module named wsgi
Mon Feb  6 13:58:23 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)
Mon Feb  6 13:58:23 2012 - *** no app loaded. going in full dynamic mode ***
Mon Feb  6 13:58:23 2012 - *** uWSGI is running in multiple interpreter mode ***
Mon Feb  6 13:58:23 2012 - spawned uWSGI master process (pid: 551)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 1 (pid: 588, cores: 1)
Mon Feb  6 13:58:23 2012 - spawned uWSGI worker 2 (pid: 589, cores: 1)

我不知道我设置项目的方式是否与此有关,但无论如何,这是我用来重定向Django实用工具的管理文件:

manage.sh

#!/bin/bash

python ./project/configs/${DEPLOYMENT_TARGET:="common"}/manage.py $*

如果需要,这是我如何设置Django项目:

project
|-manage.sh -> this fellow is redirected to settings.py (production, common or staging)
|-requirements.txt
|-README
|-dashbard.py
|-project.sqlite
|- project/
    |- apps
        |- accounts
        |-other internal apps
    |- configs 
        |- common -> for local development
            |-settings.py
            |-manage.py
            |-urls
        |-staging
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
        |-production
            |-manage.py
            |-settings.py
            |-wsgi.py
            |-logging.conf
    |-media
    |-templates
2个回答

5

我已经更新了wsgi.py文件,使其变为如下所示:

import os
import sys
import site

site.addsitedir(os.path.join('/home/ubuntu/ve','project/lib/python2.6/site-packages'))
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../../'))
sys.path.append(os.path.join(os.path.realpath(os.path.dirname(__file__)), '../../'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.configs.staging.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

现在我的uwsgi.conf文件看起来像这样:

# file: /etc/init/uwsgi.conf
description "uWSGI starter"

start on (local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn

# home - is the path to our virtualenv directory
# pythonpath - the path to our django application
# module - the wsgi handler python script

exec /home/ubuntu/ve/project/bin/uwsgi \
--uid ubuntu \
--pythonpath /home/ubuntu/django-projects/project/project/configs/staging \
-H /home/ubuntu/ve/project \
--socket /tmp/uwsgi.sock \
--chmod-socket 644 \
--module wsgi \
--logdate \
--optimize 2 \
--processes 2 \
--master \
--logto /home/ubuntu/logs/project/uwsgi.log

我的nginx site-available文件如下:

# file: /etc/nginx/sites-available/yourdomain.com
# nginx configuration for project.maumercado.com

server {
        listen 80;
        charset utf-8;
        server_name project.maumercado.com;
        access_log /home/ubuntu/logs/project/nginx/access.log;
        error_log /home/ubuntu/logs/project/nginx/error.log;

        location ^~ /cache/ {
                root /home/ubuntu/django-projects/project/project/media;
                expires max;
        }

        location / {
                uwsgi_pass unix:/tmp/uwsgi.sock;
                include /etc/nginx/uwsgi_params;
        }
}

现在它已经完美地工作了,我之前遇到了一些问题,因为css文件中使用了像ñ这样的奇怪字符导致样式出现了问题。

现在我想知道当我需要在同一台服务器上运行更多项目时应该怎么做,使用uwsgi?


1
我为多个项目所使用的方法是使用uwsgi Debian包。 它带有init脚本,一些默认的.ini设置文件,并管理每个项目。 您只需要为每个文件创建一个配置和一个不同的套接字(由默认处理),它们将都在同一系统上良好运行。 即使您不在Debian上,您仍然可以使用来自Debian软件包的init脚本和配置布局:http://ftp.de.debian.org/debian/pool/main/u/uwsgi/uwsgi_1.2.3+dfsg.orig.tar.gz - freb

1

请确保将包含wsgi.py文件的目录添加到PythonPath中(您可以指定无限数量的PythonPath指令)


--pythonpath /home/ubuntu/django-projects/project/project/configs/staging/ <- 已检查,那里是我放置wsgi.py文件的位置。还是您指的其他地方?谢谢! - maumercado
应该是 /home/ubuntu/django-projects/project/project_apps/configs/staging 而不是 /home/ubuntu/django-projects/project/project/configs/staging 吗? - roberto
哦,我明白了... 是的,实际上就是这样,我放置在那里的设置中,“project_apps”文件夹实际上叫做“project”,我会进行编辑! - maumercado

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