内部服务器访问django项目时出错

时间:2013-11-15 09:48:54

标签: python django django-wsgi

我正在尝试在我的centos 6.3服务器中设置python 2.7 + Django + virtualenv + mod_wsgi环境,以通过apache运行我的python应用程序。 但是在尝试通过apache访问应用程序时出现内部错误。错误日志显示以下内容。

============

[Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] mod_wsgi (pid=20361): Target WSGI script '/var/www/html/djangosites/spark.wsgi' cannot be loaded as Python module.

[Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] mod_wsgi (pid=20361): Exception occurred processing WSGI script '/var/www/html/djangosites/spark.wsgi'.

Fri Nov 15 04:20:30 2013] [error] [client 117.201.194.54] ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

============

作为测试服务器运行时工作正常。

==

(virtualenv)[root@lampserver spark]# python manage.py runserver 0.0.0.0:8080
Validating models...

0 errors found
November 15, 2013 - 09:15:19
Django version 1.6, using settings 'spark.settings'
Starting development server at 0.0.0.0:8080
Quit the server with CONTROL-C.

====

spark.wsgi文件如下所示。

===========

import sys
import site
import os

vepath = '/var/www/html/virtualenv/lib/python2.7/site-packages'

prev_sys_path = list(sys.path)

site.addsitedir(vepath)

sys.path.append('/var/www/html/djangosites')

new_sys_path = [p for p in sys.path if p not in prev_sys_path]

for item in new_sys_path:

    sys.path.remove(item)

sys.path[:0] = new_sys_path

from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'spark.settings'

application = WSGIHandler()

==============

apache conf中wsgi的条目如下所示

============

[root@lampserver djangosites]# cat /etc/httpd/conf.d/django.conf 


LoadModule wsgi_module modules/mod_wsgi.so
AddHandler wsgi-script .wsgi

WSGIScriptAlias / /var/www/html/djangosites/spark.wsgi

WSGIDaemonProcess spark processes=5 threads=15 display-name=%{GROUP}

WSGIProcessGroup spark

WSGIApplicationGroup %{GLOBAL}

WSGISocketPrefix /var/run/wsgi

=================

Apache以“apache”用户身份运行。

我的项目文件和权限如下所示。

===========

root@lampserver djangosites]# pwd

var/www/html/djangosites

[root@lampserver djangosites]# ls

spark  spark.wsgi

[root@lampserver djangosites]# ll
total 8

drwxr-xr-x 3 apache apache 4096 Nov 15 02:38 spark

-rwxrwxrwx 1 apache apache  535 Nov 15 03:16 spark.wsgi

[root@lampserver djangosites]# cd spark/

manage.py  spark/     

[root@lampserver spark]# ll
total 8

-rwxr-xr-x 1 apache apache  248 Nov 15 02:38 manage.py

drwxr-xr-x 2 apache apache 4096 Nov 15 03:05 spark

[root@lampserver spark]# cd spark/

[root@lampserver spark]# ll
total 28
-rw-r--r-- 1 apache apache    0 Nov 15 02:38 __init__.py

-rw-r--r-- 1 apache apache  136 Nov 15 02:40 __init__.pyc

-rw-r--r-- 1 apache apache 1969 Nov 15 02:38 settings.py

-rw-r--r-- 1 apache apache 2142 Nov 15 02:40 settings.pyc

-rw-r--r-- 1 apache apache  296 Nov 15 02:38 urls.py

-rw-r--r-- 1 apache apache  416 Nov 15 02:40 urls.pyc

-rwxr-xr-x 1 apache apache  385 Nov 15 02:38 wsgi.py

-rw-r--r-- 1 apache apache  589 Nov 15 02:40 wsgi.pyc

==================

有人可以通过我的设置确定问题吗?

此致

2 个答案:

答案 0 :(得分:0)

你能开始尝试用更简单的东西来改变你的wsgi:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "spark.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这可能有助于更轻松地发现问题

当然,也可以使用默认的wsgi文件(spark/wsgi.py)而不是spark.wsgi,方法是更改​​apache配置文件中的WSGIScriptAlias指令。下面是一个更简单的apache配置文件的示例,其中包含您的站点设置,测试一下并让我知道:

WSGIPythonPath /var/www/html/djangosites:/var/www/html/virtualenv/lib/python2.7/site-packages
WSGIPassAuthorization On
WSGIScriptAlias / /var/www/html/djangosites/spark/wsgi.py

<Directory /var/www/html/djangosites/spark>
    <Files wsgi.py>
        Order allow,deny
        Allow from all
     </Files>
</Directory>

答案 1 :(得分:0)

将此行添加到spark.wsgi文件中:

sys.path.append('/var/www/html/djangosites/spark')

由于您的django站点位于该目录中