简体   繁体   English

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

[英]Trying to get django app to work with mod_wsgi on CentOS 5

I'm running CentOS 5, and am trying to get a django application working with mod_wsgi. 我正在运行CentOS 5,我正在尝试使用mod_wsgi来使用django应用程序。 I'm using .wsgi settings I got working on Ubuntu. 我正在使用.wsgi设置我在Ubuntu上工作。 I'm also using an alternate installation of python (/opt/python2.6/) since my django application needs >2.5 and the OS uses 2.3 我也使用python(/opt/python2.6/)的备用安装,因为我的django应用程序需要> 2.5,操作系统使用2.3

Here is the error: 这是错误:

[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

And here is my .wsgi file 这是我的.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()

output of ldd /usr/lib/httpd/modules/mod_wsgi.so 输出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 vhost配置

<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>

SystemError: dynamic module not initialized properly is the exception that is thrown when a dll (or .so) that is being loaded cannot be properly initialized. SystemError: dynamic module not initialized properly是无法正确初始化正在加载的dll(或.so)时引发的异常。 In function _PyImport_LoadDynamicModule of Python/importdl.c in case anyone is interested. Python/importdl.c函数_PyImport_LoadDynamicModule中,以防任何人感兴趣。

Now, the dll/so in question (the dynamic module in Python parliance) is _functools.so which is part of Python standard library. 现在,有问题的dll / so(Python parliance中的动态模块 )是_functools.so ,它是Python标准库的一部分。 I see that it is being loaded from /opt/python2.6 so we know that this is not the system python. 我看到它是从/opt/python2.6加载的,所以我们知道这不是系统python。 My guess is that this is not the python against which mod_wsgi was compiled. 我的猜测是,这不是编译mod_wsgi的python。 To check whether this is the case run ldd mod_wsgi.so and look at what libpython is returned. 要检查是否是这种情况,请运行ldd mod_wsgi.so并查看返回的libpython

Therefore my suggestion is either to recompile mod_wsgi againast the interpreter in /opt/python2.6 by running in the wsgi_mod source directory 因此我的建议是通过在wsgi_mod源目录中运行来重新编译mod_wsgi来重新编译/opt/python2.6中的解释器

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

or make sure that sys.prefix points to the python installation that mod_wsgi expects by setting its value with the WSGIPythonHome directory. 或者确保sys.prefix通过使用WSGIPythonHome目录设置其值来指向mod_wsgi所期望的python安装。

UPDATE after ldd output 在ldd输出后更新

The second line in the ldd output shows that mod_wsgi loads the pythonlib in /usr/lib instead of /opt/python2.6 . ldd输出中的第二行显示mod_wsgi在/usr/lib而不是/opt/python2.6中加载/opt/python2.6 To instruct mod_wsgi to load that in /opt/python2.6 you should probably prepend it to the LD_LIBRARY_PATH envirnoment variable. 要指示mod_wsgi在/opt/python2.6加载它,您应该将它添加到LD_LIBRARY_PATH环境变量中。

Try it first on the command line: 首先在命令行上尝试:

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

and then make sure that the correct LD_LIBRARY_PATH is specified in the script that starts Apache. 然后确保在启动Apache的脚本中指定了正确的LD_LIBRARY_PATH。

Yet another update 又一次更新

You'll have to debug your mod_wsgi configuration. 您必须调试mod_wsgi配置。 Just try with the following .wsgi file in place of yours and tell us what you get: 只需尝试使用以下.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()]

If what you get is not `/opt/python2.6', try with the option 如果你得到的不是`/opt/python2.6',请尝试使用该选项

WSGIPythonHome /opt/python2.6

See also http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives 另请参阅http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM