mod_wsgi python2.5 ubuntu 11.04 problem
Question: I have such cfg on my amd64 platform with ubuntu 11.04:

  1. build python2.5 from source to /usr/local/python2.5
  2. virtualenv at /home/se7en/.virtualenvs/e-py25

alsp i recompile mod_wsgi.so to custom python:

    
    
    se7en@se7en-System-Product-Name:~$ ldd /usr/lib/apache2/modules/mod_wsgi.so
        linux-vdso.so.1 =>  (0x00007fff5af6c000)
        libpython2.5.so.1.0 => /usr/lib/libpython2.5.so.1.0 (0x00007f7bed14b000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7becf2d000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7becd28000)
        libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f7becb25000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7bec8a0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7bec50b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f7bed717000)
    

django.wsgi

    
    
    #/usr/local/python2.5/bin/python
    # -*- coding: utf-8 -*-
    import os, sys
    sys.stdout = sys.stderr
    
    
    dn = os.path.dirname
    PROJECT_ROOT = os.path.abspath( (dn(__file__)) )
    
    #DJANGO_PROJECT_ROOT = os.path.join(PROJECT_ROOT, 'apps')
    DJANGO_PROJECT_ROOT = PROJECT_ROOT
    
    sys.path.insert(0, '/usr/local/python2.5/lib/python2.5/site-packages' )
    sys.path.insert(0,'/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages')
    sys.path.insert(0, DJANGO_PROJECT_ROOT )
    
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
    
    print sys.version #return 2.5.5 (r255:77872, Apr 15 2011, 22:12:51
    print sys.path
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

apache site config:

    
    
    <VirtualHost 192.168.1.3>
    
        ServerAdmin admin@wsgi.debianworld.ru
        ServerName wsgi.debianworld.ru
    
    
        ErrorLog    /home/se7en/workspace/lxchg/logs/error_log
        CustomLog   /home/se7en/workspace/lxchg/logs/access_log common
    
    
    
        WSGIScriptAlias / /home/se7en/workspace/lxchg/django.wsgi
    
    
    
    
    
    
        Alias "/media/" "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/contrib/admin/media/"
        <Location "/media/">
            SetHandler None
        </Location>
    
        Alias "/main_media/" "/home/se7en/workspace/lxchg/main_media/"
        <Location "/main_media/">
            SetHandler None
        </Location>
    </VirtualHost>
    WSGIPythonHome /home/se7en/.virtualenvs/e-py25
    #WSGIPythonExecutable /usr/local/python2.5/bin/python
    

I got an error in my error log:

    
    
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3] mod_wsgi (pid=2955): Exception occurred processing WSGI script '/home/se7en/workspace/lxchg/django.wsgi'.
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3] Traceback (most recent call last):
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 248, in __call__
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     response = self.get_response(request)
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/core/handlers/base.py", line 141, in get_response
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     return self.handle_uncaught_exception(request, resolver, sys.exc_info())
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/core/handlers/base.py", line 176, in handle_uncaught_exception
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     if resolver.urlconf_module is None:
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/core/urlresolvers.py", line 239, in _get_urlconf_module
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     self._urlconf_module = import_module(self.urlconf_name)
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/utils/importlib.py", line 35, in import_module
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     __import__(name)
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "C:\\workspace\\sdl\\lxchg\\urls.py", line 3, in <module>
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/views/generic/create_update.py", line 1, in <module>
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     from django.forms.models import ModelFormMetaclass, ModelForm
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/forms/__init__.py", line 17, in <module>
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     from models import *
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/forms/models.py", line 6, in <module>
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     from django.db import connections
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/db/__init__.py", line 77, in <module>
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     connection = connections[DEFAULT_DB_ALIAS]
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/db/utils.py", line 92, in __getitem__
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     backend = load_backend(db['ENGINE'])
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]   File "/home/se7en/.virtualenvs/e-py25/lib/python2.5/site-packages/django/db/utils.py", line 50, in load_backend
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     raise ImproperlyConfigured(error_msg)
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3] ImproperlyConfigured: 'django.db.backends.mysql' isn't an available database backend. 
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3] Try using django.db.backends.XXX, where XXX is one of:
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3]     'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
    [Wed Jun 22 13:45:54 2011] [error] [client 192.168.1.3] Error was: /home/se7en/.virtualenvs/e-py25/lib/python2.5/lib-dynload/array.so: undefined symbol: PyUnicodeUCS2_FromUnicode
    

Then i start project via manage.py runserver it works, but via mod_wsgi do
not. please help)


Answer: The operating system supplied Python versions on Linux boxes are compiled for
UCS4 Unicode character width. If you build from source code, if you don't
explicitly say otherwise it will default to UCS2. The result is that your
system Python 2.5 and local version under /usr/local are expecting different
widths for Unicode characters and since Unicode functions have the UCS type as
part of the name, when you are using objects compiled for one Python with the
other, you get undefined errors for the Unicode functions.

Why this is occuring as hinted at by other answer is that mod_wsgi.so is
actually picking up the system wide UCS4 libpython2.5.so file instead of that
for UCS2 Python under /usr/local/lib. Although you could set LD_LIBRARY_PATH
to make a process to look in /usr/local/lib first, that is a pain to do under
Apache. Instead what you should do is rebuild mod_wsgi from source code again
and do:

    
    
    make distclean
    ./configure --with-python=/usr/local/bin/python2.5
    LD_RUN_PATH=/usr/local/lib make
    sudo make install
    

By setting LD_RUN_PATH as environment variable on the command when invoking
'make', the linker will embed /usr/local/lib into library search path direct
into mod_wsgi.so. That way it will find correct libpython2.5.so at run time
without needing to set LD_LIBRARY_PATH. You can confirm it worked by running
'ldd' on the resultant mod_wsgi.so and it should then pick up correct library
from /usr/local/lib.

