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>Adding <code>startup-timeout=15</code> to <code>WSGIDaemonProcess</code> in the virtual hosts config file did not solve my issue.</p>

<p>As for the "earlier error message in the error log" with the modified <code>wsgi.py</code> to get the real error, the only errors present in the <code>/var/log/apache2/cms-error.log</code> seem to be related to the fact that the "Target WSGI script does not contain WSGI application 'application'". As indicated in the question. Inspection of the apache2 error log during restart and subsequent attempts of loading the site showed the typical output.</p>

<pre><code>[Wed Oct 12 16:16:01.372050 2016] [mpm_prefork:notice] [pid 23564] AH00169: caught SIGTERM, shutting down
[Wed Oct 12 16:16:02.292367 2016] [mpm_prefork:notice] [pid 1169] AH00163: Apache/2.4.7 (Ubuntu) mod_wsgi/4.5.7 Python/2.7 PHP/5.5.9-1ubuntu4.20 configured -- resuming normal operations
[Wed Oct 12 16:16:02.292455 2016] [core:notice] [pid 1169] AH00094: Command line: '/usr/sbin/apache2'
</code></pre>

<p>Finally, what worked was to format the server and start all over again. The mod_WSGI is now working perfectly with both sites. I have also upgraded to MySQL 5.7 and ubuntu 16.04.</p>

<p>I would like to point out that the final suggestion about how to set the <code>python-path</code> and <code>python-home</code> did not work with the old system, however, it worked without any issues on the new system. This leads me to think that I somehow installed a modified mod-WSGI or it was corrupted during installation. I am not sure of this.</p>

python django mod-wsgi django-cms