0

该错误仅出现在 openSuse Server 12.3 x64(最小安装)中。具有相同 apache 设置的相同代码在 Ubuntu 发行版中就像魅力一样。运行 django 项目时出现几个错误。

错误:访问页面 url 时“500 内部服务器错误”

Traceback (most recent call last):
mod_wsgi (pid=4601): Target WSGI script '/srv/www/compare/htdocs/config/apache/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=4601): Exception occurred processing WSGI script '/srv/www/compare/htdocs/config/apache/wsgi.py'.
Traceback (most recent call last):
     File "/var/www/thehomeboard/wwwhome/wsgi.py", line 37, in <module>
       application = get_wsgi_application()
     File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
       django.setup()
     File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
       apps.populate(settings.INSTALLED_APPS)
     File "/usr/lib/python2.7/dist-packages/django/apps/registry.py", line 78, in populate
       raise RuntimeError("populate() isn't reentrant")
    File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
        raise RuntimeError("populate() isn't reentrant")
RuntimeError: populate() isn't reentrant

错误:运行 python wsgi.py 时。(运行python manage.py时出现同样的错误)

Traceback (most recent call last):
  File "/srv/www/compare/htdocs/config/apache/wsgi.py", line 28, in <module>
    application = get_wsgi_application()
  File "/usr/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/usr/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/lib/python2.7/site-packages/django/apps/config.py", line 112, in create
    mod = import_module(mod_path)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/lib/python2.7/site-packages/debug_toolbar/apps.py", line 6, in <module>
    from debug_toolbar import settings as dt_settings
  File "/usr/lib/python2.7/site-packages/debug_toolbar/settings.py", line 8, in <module>
    from debug_toolbar.compat import import_module
  File "/usr/lib/python2.7/site-packages/debug_toolbar/compat.py", line 39, in <module>
    from django.test import (  # NOQA
  File "/usr/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module>
    from django.test.client import Client, RequestFactory
  File "/usr/lib/python2.7/site-packages/django/test/client.py", line 22, in <module>
    from django.test import signals
ImportError: cannot import name signals

项目的配置如下所示:

虚拟主机配置

<VirtualHost *:80>
  ServerName compare.django

  DocumentRoot /srv/www/compare/htdocs
  ErrorLog /srv/www/compare/log/error.log
  CustomLog /srv/www/compare/log/custom.log combined

  WSGIDaemonProcess compare python-path=/srv/www/compare/htdocs:/usr/lib/python2.7/site-packages processes=2 threads=5

  WSGIScriptAlias / /srv/www/compare/htdocs/config/apache/wsgi.py
  <Directory /srv/www/compare/htdocs>
      <Files wsgi.py>
        #Require all granted
        Order deny,allow
        Allow from all
      </Files>
  </Directory>

  <Directory /srv/www/compare/htdocs/config/apache>
      WSGIProcessGroup compare
      #Require all granted
      Order deny,allow
      Allow from all
  </Directory>

  Alias /static/ /srv/www/compare/htdocs/static/
  <Directory /srv/www/compare/htdocs/static>
      #Require all granted
      Order deny,allow
      Allow from all
  </Directory>

  Alias /storage/ /srv/www/compare/htdocs/storage/
  <Directory /srv/www/compare/htdocs/storage>
      #Require all granted
      Order deny,allow
      Allow from all
  </Directory>

</VirtualHost>

wsgi.py

import os, sys
from django.core.wsgi import get_wsgi_application

# Calculate the path based on the location of the WSGI script.
apache_configuration = os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)

sys.path.append(workspace)
sys.path.append(project)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
application = get_wsgi_application()

Python环境信息:

mod_wsgi 设置

$ldd /usr/lib64/apache2/mod_wsgi.so 
linux-vdso.so.1 (0x00007fff67947000)
libpython2.7.so.1.0 => /usr/lib64/libpython2.7.so.1.0 (0x00007f698c085000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f698be69000)
libc.so.6 => /lib64/libc.so.6 (0x00007f698babb000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f698b8b7000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f698b6b4000)
libm.so.6 => /lib64/libm.so.6 (0x00007f698b3b5000)
/lib64/ld-linux-x86-64.so.2 (0x00007f698c651000)

python import sys, django
print sys.prefix # '/usr'
print sys.version # '2.7.3 (default, Apr 14 2012, 08:58:41) [GCC]'
print sys.version_info # 'sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)'
print django.__file__ # '/usr/lib/python2.7/site-packages/django/__init__.pyc'
4

1 回答 1

0

我的问题的解决方案原来与 Python 在 openSuse Server 12.3 中的安装(预安装)方式以及它如何符合mod_wsgi安装有关。

我如何找到解决方案

我知道的事实。该项目在 Ubuntu 环境下工作,在 openSuse 上不工作。因此,必须有与源代码相关的外部因素阻止它工作。唯一的外部因素是环境。我用谷歌搜索了与“目标 WSGI 脚本未作为 Python 模块加载”、“RuntimeError(“populate() 不可重入”) 相关的错误,我发现了一系列与 mod_wsgi 相关的帖子。最后,最合理的答案是由 Graham Dumpleton 发布答案

更深入地,我发现了 modwsgi wiki 和安装问题,我在Lack Of Python Shared Library段落中找到了更多信息。安装问题中显示的ldd命令输出与我的mod_wsgi.so文件输出不同。这让我觉得 openSuse 中预装的 Python 没有使用共享库。从那里我决定安装 Python 表单源。

解决方案实施

  1. 首先,我删除了在 openSuse 中创建的所有 python 安装痕迹

    zypper rm python 
    zypper rm python-base
    zypper rm python-distribute
    zypper rm libpython
    
  2. 从源代码安装 Python。按照此说明,并记住添加--enable-shared标志,正如 Graham 在安装问题中提到的那样

  3. 我安装了与项目相关的 requirements.txt 并重新启动了系统。重新启动 apache 并不能解决问题。

这些是导致解决我的问题的步骤。我希望它可以为您节省一些白发。

一直以来,我在从源代码安装 Python 时遇到了一些小问题,但这些问题很容易在谷歌上搜索并在途中修复。

于 2015-09-14T09:38:03.860 回答