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

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s