Trying to get django application to work with mod_wsgi on CentOS 5

I am running CentOS 5 and trying to get a django application working with mod_wsgi. I am using .wsgi settings. I worked on Ubuntu. I also use an alternative python installation (/opt/python2.6/), since my django application requires> 2.5, and the OS uses 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

 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 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> 
+7
python django mod-wsgi centos5
source share
1 answer

SystemError: dynamic module not initialized properly is an exception that is thrown when the dll (or .so) that is loaded cannot be correctly initialized. In the _PyImport_LoadDynamicModule of Python/importdl.c in case someone is interested.

Now, the dll / so in question (dynamic module in Python parliance) is _functools.so , which is part of the Python standard library. I see that it is loading from / opt / python 2.6, so we know that this is not system python. I assume this is not the python mod_wsgi was compiled with. To check if this is running, run ldd mod_wsgi.so and see what libpython returns.

So my suggestion is to recompile the mod_wsgi againast interpreter in / opt / python 2.6 by running wsgi_mod in the source directory

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

or make sure sys.prefix points to the python installation that mod_wsgi expects by setting its value to the WSGIPythonHome directory.

UPDATE after ldd is released

The second line in the ldd output shows that mod_wsgi loads pythonlib in /usr/lib instead of /opt/python2.6 . To instruct mod_wsgi to load this value in /opt/python2.6 , you should probably add it to the LD_LIBRARY_PATH envirnoment variable.

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 the script that starts Apache has the correct LD_LIBRARY_PATH.

Another update

You will have to debug the mod_wsgi configuration. Just try with the following .wsgi file instead of yours and tell us what you get:

 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 you are not getting `/opt/python2.6 ', try with the option

 WSGIPythonHome /opt/python2.6 

See also http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives

+7
source share

All Articles