Django, mod_python and virtualenv

Just quick note for myself and for people still using mod_python.

To use virtualenv with mod_python create file myvirtualdjango.py in your virtualenv bin directory (change the path to reflect your setup):

#myvirtualdjango.py

activate_this = '/home/django/progopedia.ru/ve/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from django.core.handlers.modpython import handler

In the apache conf file add your virtualenv bin directory to the PythonPath and set PythonHandler to myvirtualdjango (copy-and-paste from real server httpd.conf):

<VirtualHost 127.0.0.1:81>
    ServerName progopedia.ru
    ServerAdmin admin@progopedia.ru

    <Location "/">
        SetHandler python-program
        PythonPath "['/home/django/progopedia.ru/ve/bin', '/home/django/progopedia.ru/src/progopedia_ru_project/'] + sys.path"
        PythonHandler myvirtualdjango
        SetEnv DJANGO_SETTINGS_MODULE settings
        SetEnv PYTHON_EGG_CACHE /var/tmp/egg
        PythonInterpreter polyprog_ru
    </Location>
</VirtualHost>

Deploying and tuning Django

I used shared hosting for my Django-based Encyclopedia of Programming Languages (Russian version), but realized, that I need dedicated server for more performance and stability. For now, I chose cheap server with 1,8 GHz CPU and 512 Mb RAM, with CentOS 5 installed.

In this article I’ll share my experience for installing on this server Django with Apache mod_python, with memcached caching and lighttpd for serving static files.