mod_wsgi 64-bit

When building mod_wsgi there are two things to remember if you are building on a 64-bit Linux system. My examples came from Red Hat Enterprise Linux 5 WS.

Here is the link to these and other problems:

http://code.google.com/p/modwsgi/wiki/InstallationIssues

First, When running configure make sure the –enable-shared switch is used. This will create a shared object library similar to this /usr/local/lib:


-r-xr-xr-x  1 root root  5624411 Feb  7 10:11 libpython2.6.so.1.0

You will need to add a link, so this library can be found by Apache or your web server:


lrwxrwxrwx  1 root root       19 Feb  7 10:11 libpython2.6.so -> libpython2.6.so
 .1.0

And finally, don’t forget to compile the library’s location into make (mod_wsgi’s build directory).

 LD_RUN_PATH=/usr/local/lib make 

before running make install.

Building the shared library information is important, so you do not get an error like this:

Syntax error lin line 200 of /etc/httpd/conf/httpd.con Cannot load /etc/httpd/modules/mod_wsgi.so into server: libpython2.6.so.1.0: cannot open shared object file: No such file or directory

I had mistakenly thought you Apache would use environment variables to locate the shared library.


Choosing Django: mod_wsgi Configuration

The focus of using mod_python is to load and configure in Apache’s configuration file.

Here is a typical configuration extract for mod_python:


SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE settings
SetEnv PYTHON_EGG_CACHE /tmp/.python_eggs
PythonOption django.root /home/amr/django/amr
PythonPath "['/home/amr/django/amr/bin', '/home/amr/django', '/home/amr/django/amr', '/home/amr/django/amr/media', '/home/amr/django/amr/media/admin'] + sys.path"
PythonDebug On

On the other  hand, mod_wsgi is mentioned in the Apache configuration, but a lot more of the configuration is contained within the mod_wsgi handler, which you write:


WSGIScriptAlias / /usr/local/www/wsgi-scripts/wsgi_handler.py
<Directory /usr/local/www/wsgi-scripts>
 Order allow,deny
 Allow from all
</Directory>

Here is a working mod_wsgi handler:

import os
import sys

PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))
sys.path.append(PROJECT_ROOT)
sys.path.append(os.path.join('/home/amr/django'))
sys.path.append(os.path.join('/home/amr/django/amr'))
sys.path.append(os.path.join('/home/amr/bin'))
sys.path.append(os.path.join('/usr/local/www/documents/media'))
sys.path.append(os.path.join('/usr/local/www/documents/media/media'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'amr.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/python_egg_cache'

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

And finally, here is the full Apache configuration, which will allow loading css and other static documents along with the Django admin site.


Listen 8002
<VirtualHost _default_:8002>

AliasMatch ^/([^/]*\.css) /usr/local/www/documents/media/$1

Alias /media/ /usr/local/www/documents/media/media/

Alias /static/ /usr/local/www/documents/media/
<Directory /usr/local/www/documents/media/>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /usr/local/www/wsgi-scripts/wsgi_handler.py
<Directory /usr/local/www/wsgi-scripts>
 Order allow,deny
 Allow from all
</Directory>

ServerAdmin someone@someone.com
</VirtualHost>

Finally, the pertinent environment variables from settings.py

# Main URL for the project
BASE_URL = 'http://myserver:8002'

# Absolute path to the directory that holds media
MEDIA_ROOT = '/usr/local/www/documents/media/'

# URL that handles the media served from MEDIA_ROOT
MEDIA_URL = 'http://myserver:8002/'

# URL prefix for admin media -- CSS, JavaScript and images.
ADMIN_MEDIA_PREFIX = BASE_URL + '/media/'


Choosing Django: The Why Of It All

I chose Django for two reasons. First it was a framework, and second its back end was Python instead of another language.  Also, the Django “sales pitch” appealed to me. My Django projects started with mod_python, the Apache module that allows Python code to be executed in Apache’s back end.

Almost all references to mod_python were accompanied by the advice that mod_wsgi should be used, unless this was a legacy web site that could not easily be re-written. Eventually, I came to the same conclusion as almost everyone else, and moving to mod_wsgi was not due to lack of basic Django functionality. Instead my problems were almost completely due to not understanding how to load media.

Moving from mod_python to mod_wsgi became a small project in itself.


Configuring Django: Not As Easy As You Think

I have been struggling a while with a Django content loading problem. Given my web site is both in production and under development, I thought it might be a good thing to take my workstation and start the conversion from mod_python to mod_wsgi. It is always a good decision not to make major changes on your production system, but I’m beginning to think it was not a good idea on my workstation, either.

I am running apache on Ubuntu 10.04 LTS, and while it’s a desktop configuration, the server configuration was installed, so I could do development work. The whole process, including getting wsgi installed as been, as I’m fond of saying, one long root canal without Novocaine.

I am determined to succeed, because I believe in the basic premise of frame-based web development.

Tonight, I’m going into a Django meetup to hear about installation — a night that’s going to be snowing and cold — only because installation and configuration is the headliner topic. I will have to see what develops.