Subdomain Django Settings Conflict
<p>I have a site with a set of sub-domains. When I visit one of those sub-domains including the actual domain, it <strong>sometimes</strong> shows an Internal Server Error on the browser and when I check the apache error.log it tells that it throws an <strong>ImportError</strong> :</p>

<pre><code>    [Tue Oct 11 06:47:10.999837 2016] [:error] [pid 13114] [client 0.0.0.0:58735] mod_wsgi (pid=13114): Target WSGI script '/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py' cannot be loaded as Python module.
    [Tue Oct 11 06:47:11.000377 2016] [:error] [pid 13114] [client 0.0.0.0:58735] mod_wsgi (pid=13114): Exception occurred processing WSGI script '/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py'.
    [Tue Oct 11 06:47:11.000478 2016] [:error] [pid 13114] [client 0.0.0.0:58735] Traceback (most recent call last):
    [Tue Oct 11 06:47:11.000552 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/var/www/html/api.ai-labs.co/ai_labs_apps/wsgi.py", line 27, in &lt;module&gt;
    [Tue Oct 11 06:47:11.000675 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     application = get_wsgi_application()
    [Tue Oct 11 06:47:11.000737 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    [Tue Oct 11 06:47:11.000853 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     django.setup()
    [Tue Oct 11 06:47:11.000913 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
    [Tue Oct 11 06:47:11.001017 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    [Tue Oct 11 06:47:11.001076 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 55, in __getattr__
    [Tue Oct 11 06:47:11.001269 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     self._setup(name)
    [Tue Oct 11 06:47:11.001330 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 43, in _setup
    [Tue Oct 11 06:47:11.001395 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     self._wrapped = Settings(settings_module)
    [Tue Oct 11 06:47:11.001450 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 99, in __init__
    [Tue Oct 11 06:47:11.001526 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     mod = importlib.import_module(self.SETTINGS_MODULE)
    [Tue Oct 11 06:47:11.001581 2016] [:error] [pid 13114] [client 0.0.0.0:58735]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    [Tue Oct 11 06:47:11.001689 2016] [:error] [pid 13114] [client 0.0.0.0:58735]     __import__(name)
    [Tue Oct 11 06:47:11.001765 2016] [:error] [pid 13114] [client 0.0.0.0:58735] ImportError: No module named project.settings
</code></pre>

<p>I'll just post the files of the two out of five applications because I really can't figure out the error and I'm very bad at deploying to the actual server</p>

<p><strong>1st application: accounts</strong></p>

<p><strong>This is the 1st application's apache2/sites-available/accounts.ai-labs.conf</strong></p>

<pre><code>    &lt;VirtualHost *:80&gt;
            # The ServerName directive sets the request scheme, hostname and port that
            # the server uses to identify itself. This is used when creating
            # redirection URLs. In the context of virtual hosts, the ServerName
            # specifies what hostname must appear in the request's Host: header to
            # match this virtual host. For the default virtual host (this file) this
            # value is not decisive as it is used as a last resort host regardless.
            # However, you must set it for any further virtual host explicitly.
            ServerName accounts.ai-labs.co

            # ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html/accounts.ai-labs.co/project

            # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
            # error, crit, alert, emerg.
            # It is also possible to configure the loglevel for particular
            # modules, e.g.
            #LogLevel info ssl:warn

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined

            # For most configuration files from conf-available/, which are
            # enabled or disabled at a global level, it is possible to
            # include a line for only one particular virtual host. For example the
            # following line enables the CGI configuration for this host only
            # after it has been globally disabled with "a2disconf".
            #Include conf-available/serve-cgi-bin.conf

            &lt;Directory /var/www/html/accounts.ai-labs.co/project&gt;
                    Options Indexes FollowSymLinks
                    AllowOverride All
                    Require all granted
            &lt;/Directory&gt;

            Alias /static /var/www/html/accounts.ai-labs.co/project/static
            &lt;Directory /var/www/html/accounts.ai-labs.co/project/static&gt;
                    Require all granted
            &lt;/Directory&gt;

            Alias /static /var/www/html/accounts.ai-labs.co/project/media
            &lt;Directory /var/www/html/accounts.ai-labs.co/project/media&gt;
                    Require all granted
            &lt;/Directory&gt;

            &lt;Directory /var/www/html/accounts.ai-labs.co/project/project&gt;
                    &lt;Files wsgi.py&gt;
                            Require all granted
                    &lt;/Files&gt;
            &lt;/Directory&gt;

            WSGIScriptAlias / /var/www/html/accounts.ai-labs.co/project/project/wsgi.py

    &lt;/VirtualHost&gt;
</code></pre>

<p><strong>This is the 1st application's wsgi.py</strong></p>

<pre><code>"""
WSGI config for project project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/
"""

import os
import sys
import site

site.addsitedir('/var/www/html/accounts.ai-labs.co/lib/python2.7/site-packages')

sys.path.append('/var/www/html/accounts.ai-labs.co/project')
sys.path.append('/var/www/html/accounts.ai-labs.co/project/project')

from django.core.wsgi import get_wsgi_application
from django.conf import settings

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

activate_env=os.path.expanduser("/var/www/html/accounts.ai-labs.co/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

application = get_wsgi_application()
</code></pre>

<p><strong>2nd application: blogs</strong></p>

<p><strong>This is the 2nd application's apache2/sites-available/blogs.ai-labs.conf</strong></p>

<pre><code>&lt;VirtualHost *:80&gt;
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName blogs.ai-labs.co

        # ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/blogs.ai-labs.co

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

        &lt;Directory /var/www/html/blogs.ai-labs.co&gt;
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        &lt;/Directory&gt;

        Alias /static /var/www/html/blogs.ai-labs.co/static
        &lt;Directory /var/www/html/blogs.ai-labs.co/static&gt;
                Require all granted
        &lt;/Directory&gt;

        Alias /static /var/www/html/blogs.ai-labs.co/media
        &lt;Directory /var/www/html/blogs.ai-labs.co/media&gt;
                Require all granted
        &lt;/Directory&gt;

        &lt;Directory /var/www/html/blogs.ai-labs.co/ai_labs_blogs&gt;
                &lt;Files wsgi.py&gt;
                        Require all granted
                &lt;/Files&gt;
        &lt;/Directory&gt;

        WSGIScriptAlias / /var/www/html/blogs.ai-labs.co/ai_labs_blogs/wsgi.py

&lt;/VirtualHost&gt;
</code></pre>

<p><strong>This is the 2nd application's wsgi.py</strong></p>

<pre><code>"""
WSGI config for ai_labs_blogs project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os
import sys
import site

site.addsitedir('/var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/local/lib/python2.7/site-packages')

sys.path.append('/var/www/html/blogs.ai-labs.co')
sys.path.append('/var/www/html/blogs.ai-labs.co/ai_labs_blogs')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ai_labs_blogs.settings")

activate_env=os.path.expanduser("/var/www/html/.virtualenvs_copy/ai-labs-website-pure-django/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

application = get_wsgi_application()
</code></pre>

<p>How do I resolve this?</p>

<p>Don't use:</p>

<pre><code>os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
</code></pre>

<p>use:</p>

<pre><code>os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
</code></pre>

<p>Similarly for the other <code>wsgi.py</code> file.</p>

<p>This is mentioned in the Django documentation for mod_wsgi as a requirement when not using daemon mode. You really should use daemon mode so each application is in separate processes. You can also clean up how virtual environments are used.</p>

<ul>
<li><a href="https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/" rel="nofollow">https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/</a></li>
<li><a href="http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html" rel="nofollow">http://blog.dscpl.com.au/2012/10/why-are-you-using-embedded-mode-of.html</a></li>
<li><a href="http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html" rel="nofollow">http://blog.dscpl.com.au/2014/09/using-python-virtual-environments-with.html</a></li>
</ul>

python django apache digital-ocean ubuntu-16.04