试图让django应用程序在CentOS 5上使用mod_wsgi

时间:2010-03-12 19:09:54

标签: python django mod-wsgi centos5

我正在运行CentOS 5,并且正在尝试使用mod_wsgi来使用django应用程序。我正在使用.wsgi设置我在Ubuntu上工作。我也使用python(/opt/python2.6/)的备用安装,因为我的django应用程序需要> 2.5并且操作系统使用2.3

这是错误:

[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251] SystemError: dynamic module not initialized properly
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251] mod_wsgi (pid=23630): Target WSGI script '/data/hosting/cubedev/apache/django.wsgi' cannot be loaded as Python module.
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251] mod_wsgi (pid=23630): Exception occurred processing WSGI script '/data/hosting/cubedev/apache/django.wsgi'.
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251] Traceback (most recent call last):
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]  File "/data/hosting/cubedev/apache/django.wsgi", line 8, in 
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]    import django.core.handlers.wsgi
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]  File "/opt/python2.6/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 1, in 
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]    from threading import Lock
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]  File "/opt/python2.6/lib/python2.6/threading.py", line 13, in 
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]    from functools import wraps
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]  File "/opt/python2.6/lib/python2.6/functools.py", line 10, in 
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251]    from _functools import partial, reduce
[Thu Mar 04 10:52:15 2010] [error] [client 10.1.0.251] SystemError: dynamic module not initialized properly

这是我的.wsgi文件

import os
import sys
os.environ['PYTHON_EGG_CACHE'] = '/tmp/django/' # This line was added for CentOS.
os.environ['DJANGO_SETTINGS_MODULE'] = 'cube.settings'

sys.path.append('/data/hosting/cubedev')

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

输出ldd /usr/lib/httpd/modules/mod_wsgi.so

linux-gate.so.1 =>  (0x00250000)
libpython2.6.so.1.0 => /opt/python2.6/lib/libpython2.6.so.1.0 (0x00be6000)
libpthread.so.0 => /lib/libpthread.so.0 (0x00110000)
libdl.so.2 => /lib/libdl.so.2 (0x00557000)
libutil.so.1 => /lib/libutil.so.1 (0x00128000)
libm.so.6 => /lib/libm.so.6 (0x0012c000)
libc.so.6 => /lib/libc.so.6 (0x00251000)
/lib/ld-linux.so.2 (0x0039a000)

vhost config

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerAlias cube-dev.example.com
    ServerName  cube-dev.example.com
    ErrorLog logs/cube-dev.example.com.error_log
    CustomLog logs/cube-dev.example.com.access_log common
    Alias /phpMyAdmin /var/www/phpMyAdmin/
   # DocumentRoot /data/hosting/cubedev

    WSGIScriptAlias / /data/hosting/cubedev/apache/django.wsgi

    WSGIProcessGroup cubedev.example.com
    WSGIDaemonProcess cubedev.example.com

    Alias /media/ /data/hosting/cubedev/media/
    Alias /adminmedia/  /opt/python2.6/lib/python2.6/site-packages/django/contrib/admin/media/
    Alias /media /data/hosting/cubedev/media

   <Directory "/data/hosting/cubedev/media">
     Order deny,allow
     Allow from all
    </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:7)

SystemError: dynamic module not initialized properly是无法正确初始化正在加载的dll(或.so)时引发的异常。在_PyImport_LoadDynamicModule的函数Python/importdl.c中,如果有人感兴趣的话。

现在,有问题的dll / so(Python中的动态模块)是_functools.so,它是Python标准库的一部分。我看到它是从/opt/python2.6加载的,所以我们知道这不是系统python。我的猜测是,这不是编译mod_wsgi的python。要检查是否是这种情况,请运行ldd mod_wsgi.so并查看返回的libpython

因此我的建议是通过在wsgi_mod源目录中运行来重新编译mod_wsgi来重新编译/opt/python2.6中的解释器

./configure --with-python=/opt/python2.6/bin/python2.6

或确保sys.prefix通过使用WSGIPythonHome目录设置其值来指向mod_wsgi所期望的python安装。

在ldd输出后更新

ldd输出中的第二行显示mod_wsgi在/usr/lib而不是/opt/python2.6中加载pythonlib。要指示mod_wsgi在/opt/python2.6中加载它,您应该将其添加到LD_LIBRARY_PATH环境变量中。

首先在命令行上尝试:

LD_LIBRARY_PATH=/opt/python2.6/lib:$LD_LIBRARY_PATH ldd mod_wsgi.so

然后确保在启动Apache的脚本中指定了正确的LD_LIBRARY_PATH。

又一次更新

您必须调试mod_wsgi配置。只需尝试使用以下.wsgi文件代替您的文件,并告诉我们您的内容:

def application(environ, start_response):
    status = '200 OK'
    start_response(status, [('Content-type', 'text/plain')])

    try:
        import sys
        return ['\n'.join([sys.prefix, sys.executable])]
    except:
        import traceback as tb
        return [tb.format_exc()]

如果你得到的不是`/opt/python2.6',请尝试选项

WSGIPythonHome /opt/python2.6

另见http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives