[Django]-Weird, inconsistent behavior using Apache and multiple Django sites

3๐Ÿ‘

โœ…

Go read:

This describes all the common reasons why you can get mixing of responses for sites when running Django under Apache/mod_wsgi.

In addition to that, you can also see issues if not careful where you have sites sharing a common parent domain. For this you may have to adjust cookie domains or names if each site was setup and is effectively sharing the same cookie.

Also be aware of sharing common backend services for all sites such as memcache as there can be contention as far as distinct sites use the same keys for data. For that setup things to use a distinct memcache key prefix to keep data separate.

0๐Ÿ‘

First of all, combine identical sites like this:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName www.siteA.tk
    ServerAlias siteA.tk siteA.com www.siteA.com
    WSGIScriptAlias / /home/me/siteA/siteA/wsgi.py
    Alias /static/ /var/www/siteA/static/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.siteB.tk
    ServerAlias siteB.tk siteB.com www.siteB.com

    WSGIScriptAlias / /home/me/siteB/siteB/wsgi.py
</VirtualHost>

Also, instead of using WSGIPythonPath in Apache configuration, add this line in the beginning of your wsgi.py file (this example is for siteA, change it properly for siteB):

import sys
sys.path.append('/home/me/siteA')
๐Ÿ‘คSadjad

0๐Ÿ‘

Self realization.

The solution was to change the wsgi.py files generated by django. In fact it specifically says in the comments:

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "siteA.settings"

I used the immediately simple option and replaced the given line below the comment with

os.environ["DJANGO_SETTINGS_MODULE"] = "yoursite.settings"

Restart apache

$ apachectl -k graceful

Problem solved.

๐Ÿ‘คYujin Wu

Leave a comment