How to fix Django, Apache VirtualHost, mod_wsgi and DJANGO_SETTINGS_MODULE

I’ve spent countless hours trying to fix Django installations running on legacy Apache servers, these usually recurring every few months, a time span long enough to forget how the last fix was done. And for some reason, the docs are not mentioning this crucial features AT ALL! In the official Django + mod_wsgi documentation page, they don’t mention something so irrelevant such as THE MAIN DJANGO SETTINGS FILE?

For future memory, here's a solution that makes me happy, and hopefully next time will only be a few minutes headache (although I already know it won't be the case...)

- copy the default wsgi.py to a new file called apache_wsgi.py

- modify the new apache_wsgi.py so that it reads:

import os
import mod_wsgi
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.%s" % mod_wsgi.process_group)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

- now open your virtualhost configuration file and check it contains the following

WSGIScriptAlias / /path/to/your/apache_wsgi.py
WSGIDaemonProcess pick_settings_file_name processes=2 threads=15 python-path=/path/to/virtualenv/lib/python2.7/site-packages:/path/to/django/project
WSGIProcessGroup pick_settings_file_name

- almost there! create a /settings folder sibling of settings.py, put an empty __init__.py in it, then move settings.py inside that folder, rename it to common.py and create a new pick_settings_file_name.py with a single liner in it

from .common import *

VOILAAAAAAAAAAAAAAA!!!!

just restart your apache and everything will work. in your pick_settings_file_name.py you will have configurations specific for your environment (i.e. multiple copies of the file for dev, staging: project_dev.py, project_staging.py)