Serving two Django sites with WSGI results in internal server error for only one of the sites
<p>I have a Django site and a Django CMS site which I am serving from the same ubuntu 14.04 server running MySQL 5.6, Apache2 2.4.7 and Django 1.8 via mod_wsgi version 4.5.7 using virtual hosts. Locally (on my Linux PC) I have managed to accomplish this with both sites working perfectly and hence decided to migrate to the server.</p>

<p>After the migration, taking care that everything has the same version, the situation is such that the Django site is working properly while the Django CMS site is giving me and internal server error 500. These are my virtual hosts' .conf files, and the wsgi.py file for the broken site.</p>

<pre><code># wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bsg.settings")
application = get_wsgi_application()

# Django_cms_site.conf
&lt;VirtualHost *:80&gt;
    ServerName site.com
    ServerAdmin webmaster@localhost.com

    Alias /static/ /home/bsg/cms/static/
    Alias /media/ /home/bsg/cms/media/

    WSGIScriptAlias / /home/bsg/cms/bsg/wsgi.py
    WSGIDaemonProcess bsgcms python-path=/home/hicklin/bsg/cms:/home/venv-bsg/lib/python2.7/site-packages
    WSGIProcessGroup bsgcms

    &lt;Directory /home/bsg/cms&gt;
        Require all granted
    &lt;/Directory&gt;

    LogLevel warn
    ErrorLog /var/log/apache2/cms-error.log
    CustomLog /var/log/apache2/cms-access.log combined
&lt;/VirtualHost&gt;

# Django_site.conf
&lt;VirtualHost *:80&gt;
    ServerName django.site.com
    ServerAdmin webmaster@localhost.com

    Alias /static/ /home/bsg/admin/site/static/

    WSGIScriptAlias / /home/bsg/admin/site/wsgi.py
    WSGIDaemonProcess bsgadmin python-path=/home/bsg/admin:/home/hicklin/venv-bsg/lib/python2.7/site-packages
    WSGIProcessGroup bsgadmin

    &lt;Directory /home/bsg/admin&gt;
        Require all granted
    &lt;/Directory&gt;

    LogLevel warn
    ErrorLog /var/log/apache2/admin-error.log
    CustomLog /var/log/apache2/admin-access.log combined
&lt;/VirtualHost&gt;
</code></pre>

<p>As can be noted, I am using the same virtual environment for both sites. The relevant logs give the following error.</p>

<pre><code>[Tue Oct 11 22:39:43.416901 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984] mod_wsgi (pid=19799): Target WSGI script '/home/bsg/cms/bsg/wsgi.py' cannot be loaded as Python module.
[Tue Oct 11 22:39:43.416942 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984] mod_wsgi (pid=19799): Exception occurred processing WSGI script '/home/bsg/cms/bsg/wsgi.py'.
[Tue Oct 11 22:39:43.416977 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984] Traceback (most recent call last):
[Tue Oct 11 22:39:43.417014 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]   File "/home/bsg/cms/bsg/wsgi.py", line 16, in &lt;module&gt;
[Tue Oct 11 22:39:43.417067 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]     application = get_wsgi_application()
[Tue Oct 11 22:39:43.417093 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]   File "/home/venv-bsg/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Tue Oct 11 22:39:43.417134 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]     django.setup()
[Tue Oct 11 22:39:43.417158 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]   File "/home/venv-bsg/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Tue Oct 11 22:39:43.417194 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]     apps.populate(settings.INSTALLED_APPS)
[Tue Oct 11 22:39:43.417217 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]   File "/home/venv-bsg/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
[Tue Oct 11 22:39:43.417255 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984]     raise RuntimeError("populate() isn't reentrant")
[Tue Oct 11 22:39:43.417289 2016] [wsgi:error] [pid 19799] [remote 77.71.226.73:64984] RuntimeError: populate() isn't reentrant
</code></pre>

<p>After modifying the wsgi.py to get the <em>real error</em> as suggested by Dirk Eschler in <a href="http://stackoverflow.com/questions/30954398/django-populate-isnt-reentrant">the thread</a>, the error in /var/log/apache2/cms-error.log changes to</p>

<pre><code>[Tue Oct 11 21:36:06.087723 2016] [wsgi:error] [pid 21584] handling WSGI exception
[Tue Oct 11 21:36:06.087811 2016] [wsgi:error] [pid 21584] Traceback (most recent call last):
[Tue Oct 11 21:36:06.087854 2016] [wsgi:error] [pid 21584]   File "/home/bsg/cms/bsg/wsgi.py", line 9, in &lt;module&gt;
[Tue Oct 11 21:36:06.087975 2016] [wsgi:error] [pid 21584]     application = get_wsgi_application()
[Tue Oct 11 21:36:06.087996 2016] [wsgi:error] [pid 21584]   File "/home/venv-bsg/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Tue Oct 11 21:36:06.088067 2016] [wsgi:error] [pid 21584]     django.setup()
[Tue Oct 11 21:36:06.088093 2016] [wsgi:error] [pid 21584]   File "/home/venv-bsg/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
[Tue Oct 11 21:36:06.088164 2016] [wsgi:error] [pid 21584]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Oct 11 21:36:06.088190 2016] [wsgi:error] [pid 21584]   File "/home/venv-bsg/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
[Tue Oct 11 21:36:06.088366 2016] [wsgi:error] [pid 21584]     self._setup(name)
[Tue Oct 11 21:36:06.088388 2016] [wsgi:error] [pid 21584]   File "/home/venv-bsg/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Tue Oct 11 21:36:06.088417 2016] [wsgi:error] [pid 21584]     % (desc, ENVIRONMENT_VARIABLE))
[Tue Oct 11 21:36:06.088442 2016] [wsgi:error] [pid 21584] ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
[Tue Oct 11 21:36:08.591950 2016] [wsgi:error] [pid 21584] [remote 77.71.226.73:3851] mod_wsgi (pid=21584): Target WSGI script '/home/bsg/cms/bsg/wsgi.py' does not contain WSGI application 'application'.
</code></pre>

<p>Please note that I have managed to run, access and work the cms site via python manage.py runserver and it works without a hitch. Following the latest error did not result in anything fruitful yet. Any help or hints are greatly appreciated.</p>

<p>This is caused by your database or some other resource needed during initialisation of Django not being available, or Django initialisation otherwise failing in some other way the first time. Back in time the initialisation of Django was reentrant and could be called a second time if the first time it failed. This is no longer the case so if initialisation fails the first time, you are forced to restart the process.</p>

<p>As you are using a recent version of mod_wsgi, you should be able to add an additional option to <code>WSGIDaemonProcess</code> to deal with this issue.</p>

<pre><code>startup-timeout=15
</code></pre>

<p>What will happen is that if mod_wsgi can't successfully load the WSGI script file within 15 seconds of the first attempt to do so, then the mod_wsgi daemon process will be automatically restarted. It will keep doing this if it keeps failing because of some service required during initialisation not being available.</p>

<p>BTW, there should have been an earlier error message in the error log than that which gave the real reason why initialisation failed. That message you quote is from the subsequent failures after the first. I don't trust that the other message you give is correct, so go back and look for the first message after Apache restart.</p>

<p>Finally, best practice is not to use:</p>

<pre><code>WSGIDaemonProcess bsgadmin python-path=/home/bsg/admin:/home/hicklin/venv-bsg/lib/python2.7/site-packages
</code></pre>

<p>but:</p>

<pre><code>WSGIDaemonProcess bsgadmin python-home=home/hicklin/venv-bsg python-path=/home/bsg/admin
</code></pre>

<p>That is, use <code>python-home</code> option to specify location of Python virtual environment, do not add <code>site-packages</code> to <code>python-path</code>. See:</p>

<ul>
<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 mod-wsgi django-cms