title: Multiple sites using Django and mod_wsgi on Apache
body: <p>I'm currently using two Django applications (say <code>A</code> &amp; <code>B</code>) hosted on the same domain (but being served on different ports) through Apache. I believe my setup is correct, but I'm randomly getting 500s on both the sites. The 500 on <code>A</code> (say for example) occurs mostly after a request has been served on <code>B</code> (and vice versa). </p>
  
  <p>Upon inspecting the error log (of say <code>A</code> for instance), I see that <code>A</code>'s wsgi module is trying to access <code>B</code>'s <code>settings.py</code> file (which obviously doesn't happen to be there, since the project paths are different) [and this does happen the other way too, B's wsgi raises an exception complaining of missing A's settings.py file]. I'm not sure why they'd look for the other settings file, the imports (for settings.py) on all views are specific to the respective projects.</p>
  
  <p>Here's my setup:</p>
  
  <p><code>A</code> is being served on port <code>8080</code>, <code>B</code> is being served on port <code>80</code>.</p>
  
  <p>VirtualHost:</p>
  
  <pre><code>&lt;VirtualHost *:8080&gt;
  
    ServerAdmin x@x.net
    ServerName  string1
  
    Alias /static/ /home/PATH_TO_PROJECT_A/static/
  
    &lt;Directory /home/PATH_TO_PROJECT_A/static&gt;
    Order deny,allow
    Allow from all
    &lt;/Directory&gt;
  
    WSGIScriptAlias / /home/PATH_TO_PROJECT_A/wsgi.py
  
    &lt;Directory /home/PATH_TO_PROJECT_A&gt;
    &lt;Files wsgi.py&gt;
    Order deny,allow
    Allow from all
    &lt;/Files&gt;
    &lt;/Directory&gt;
  
  
    LogLevel warn
    ErrorLog  /SOME_PATH/errorA.log
    CustomLog /SOME_PATH/accessA.log combined
  &lt;/VirtualHost&gt;
  
  
  &lt;VirtualHost *:80&gt;
    ServerName string1
    ServerAdmin x@x.net
  
    Alias /APP_B/static/ /home/PATH_TO_PROJECT_B/static/
  
    &lt;Directory /home/PATH_TO_PROJECT_B/static&gt;
    Order deny,allow
    Allow from all
    &lt;/Directory&gt;
  
    WSGIScriptAlias /APP_B /home/PATH_TO_PROJECT_B/wsgi.py/
  
    &lt;Directory /home/PATH_TO_PROJECT_B&gt;
    &lt;Files wsgi.py&gt;
    Order deny,allow
    Allow from all
    &lt;/Files&gt;
    &lt;/Directory&gt;
  
    ErrorLog /home/SOME_PATH/error2.log
    CustomLog /home/SOME_PATH/access2.log combined
  
    # All other files on B:80 (other than /APP_B are served normally
    DocumentRoot /home/foo/public_html/xyz/public
  
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>ports.conf:</p>
  
  <pre><code>NameVirtualHost *:8080
  Listen 8080
  Listen 80
  
  &lt;IfModule mod_ssl.c&gt;
   Listen 443
  &lt;/IfModule&gt;
  
  &lt;IfModule mod_gnutls.c&gt;
   Listen 443
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>wsgi.py on 'A':</p>
  
  <pre><code>import os, sys
  
  sys.path.append('home/PATH_TO_PROJECT_A') #1
  
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_A.settings") #2
  
  from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()
  </code></pre>
  
  <p>And exactly the same on B, with changes to line #1 and #2.</p>
  
  <p>Error I get from the <code>error.log</code> say for instance from <code>A</code>:</p>
  
  <pre><code> [Sun Aug 26 17:01:49 2012] [error] [client x] Traceback (most recent call last):
   [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
   [Sun Aug 26 17:01:49 2012] [error] [client x]     self.load_middleware()
   [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
   [Sun Aug 26 17:01:49 2012] [error] [client x]     for middleware_path in settings.MIDDLEWARE_CLASSES:
   [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
   [Sun Aug 26 17:01:49 2012] [error] [client x]     self._setup()
   [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
   [Sun Aug 26 17:01:49 2012] [error] [client x]     self._wrapped = Settings(settings_module)
   [Sun Aug 26 17:01:49 2012] [error] [client x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
   [Sun Aug 26 17:01:49 2012] [error] [client x]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
   [Sun Aug 26 17:01:49 2012] [error] [client x] ImportError: Could not import settings 'PROJECT_B.settings' (Is it on sys.path?): No module named PROJECT_B.settings
  </code></pre>
  
  <p>As you see <code>A</code>'s error log complains that <code>B</code>'s <code>settings.py</code> is missing. Please shed some light, I've no idea what's wrong. I don't see why one app would look for the other's settings.py file to import?</p>
  
  <p>Both the applications work and execute as expected, but they do break at random requests serving the 500 (which gets cleared if I refresh again).</p>
  
  <p>Thank you!</p>