使用WSGI为两个Django站点提供服务只会导致其中一个站点出现内部服务器错误

时间:2016-10-11 22:36:40

标签: python django mod-wsgi django-cms

我有一个Django站点和一个Django CMS站点,我使用虚拟主机从运行MySQL 5.6,Apache2 2.4.7和Django 1.8的相同ubuntu 14.04服务器通过mod_wsgi版本4.5.7提供服务。在本地(在我的Linux PC上)我已经设法完成了这两个网站完美工作,因此决定迁移到服务器。

迁移之后,注意一切都有相同的版本,情况是Django网站正常工作,而Django CMS网站给我和内部服务器错误500.这些是我的虚拟主机' .conf文件,以及损坏站点的wsgi.py文件。

# 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
<VirtualHost *:80>
    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

    <Directory /home/bsg/cms>
        Require all granted
    </Directory>

    LogLevel warn
    ErrorLog /var/log/apache2/cms-error.log
    CustomLog /var/log/apache2/cms-access.log combined
</VirtualHost>

# Django_site.conf
<VirtualHost *:80>
    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

    <Directory /home/bsg/admin>
        Require all granted
    </Directory>

    LogLevel warn
    ErrorLog /var/log/apache2/admin-error.log
    CustomLog /var/log/apache2/admin-access.log combined
</VirtualHost>

可以注意到,我对两个站点使用相同的虚拟环境。相关日志会出现以下错误。

[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 <module>
[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

根据Dirk Eschler在the thread中的建议修改wsgi.py以获取真实错误后,/ var / log / apache2 / cms-error.log中的错误更改为

[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 <module>
[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'.

请注意,我已经设法通过python manage.py runserver运行,访问和使用cms站点,它可以正常运行。在最新的错误之后,没有任何结果。非常感谢任何帮助或提示。

2 个答案:

答案 0 :(得分:1)

这是由于您的数据库或Django初始化期间所需的其他资源不可用,或者Django初始化在第一次以其他方式失败。回到过去,Django的初始化是可重入的,如果第一次失败,可以再次调用它。现在不再是这种情况,所以如果第一次初始化失败,您将被迫重新启动该过程。

当您使用最新版本的mod_wsgi时,您应该可以向WSGIDaemonProcess添加其他选项来处理此问题。

startup-timeout=15

如果mod_wsgi在第一次尝试的15秒内无法成功加载WSGI脚本文件,那么mod_wsgi守护进程将自动重启。如果由于初始化期间所需的某些服务不可用而一直失败,它将继续这样做。

顺便说一句,错误日志中应该有一个早于错误的消息,而不是初始化失败的真正原因。您引用的那条消息来自第一个之后的后续失败。我不相信你提供的其他消息是正确的,所以返回并在Apache重启后查找第一条消息。

最后,最佳做法是不使用:

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

但:

WSGIDaemonProcess bsgadmin python-home=home/hicklin/venv-bsg python-path=/home/bsg/admin

也就是说,使用python-home选项指定Python虚拟环境的位置,不要将site-packages添加到python-path。参见:

答案 1 :(得分:0)

在虚拟主机配置文件中将startup-timeout=15添加到WSGIDaemonProcess并没有解决我的问题。

对于错误日志中的&#34;之前的错误消息&#34;使用修改后的wsgi.py来获取真正的错误,/var/log/apache2/cms-error.log中出现的唯一错误似乎与&#34;目标WSGI脚本不包含WSGI应用程序&#39;应用&#39;&#34 ;.如问题所示。在重新启动期间检查apache2错误日志以及随后加载站点的尝试显示了典型输出。

[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'

最后,有效的方法是格式化服务器并重新开始。 mod_WSGI现在可以与两个站点完美配合。我还升级到MySQL 5.7和ubuntu 16.04。

我想指出,关于如何设置python-pathpython-home的最终建议不适用于旧系统,但是,它在新系统上没有任何问题。这让我觉得我以某种方式安装了一个修改过的mod-WSGI,或者它在安装过程中被破坏了。我不确定这一点。