Apache/mod_wsgi/Django AJAX :500内部服务器错误:模块未找到错误:没有名为“corsheaders”的模块

c86crjj0  于 2022-11-16  发布在  Apache
关注(0)|答案(2)|浏览(112)

我们的问题源于一个Django项目发现了通过Apache/WSGI运行的corsheaders模块。代码在使用Django的本地runserver时运行良好,但在通过Apache(v.2.4.41)访问时抛出500内部服务器错误。如果我们在www.example.com中注解掉应用程序和中间件settings.py,则站点和其他代码运行良好(需要corsheaders的API功能除外)。
我们已经用尽了我们能找到的在线资源,所以提前感谢您的建议,卸载和重新安装django-cors-headers以各种方式使用pip.我们确实使用了其他几个模块,已经通过这个网关安装没有问题.我们可以告诉最好的,问题源于django wsgi没有看到模块.
我在下面包括了相关的日志和设置。我还注意到了corsheader模块的安装位置。

相关版本

  • django-cors-headers==3.13.0(安装于:/home/geekfest/.local/lib/python3.8/站点软件包)
  • ==1.5.0下载软件
  • python函数==3.8.10
  • 第4.1.2章:我的天!
  • Ubuntu 20.04.1版

如果python38.zip。

**settings.py**

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'corsheaders',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'stats',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'stats.middleware.EventData',
]

ROOT_URLCONF = 'geekstats.urls'

CORS_ORIGIN_ALLOW_ALL = True

错误日志

[Fri Oct 07 11:01:49.957188 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:49.957302 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:49.960516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] Traceback (most recent call last):
[Fri Oct 07 11:01:49.960613 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:49.960625 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     application = get_wsgi_application()
[Fri Oct 07 11:01:49.960633 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:49.960637 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     django.setup(set_prefix=False)
[Fri Oct 07 11:01:49.960645 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:49.960651 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:49.960674 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 91, in populate
[Fri Oct 07 11:01:49.960680 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     app_config = AppConfig.create(entry)
[Fri Oct 07 11:01:49.960690 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3/dist-packages/django/apps/config.py", line 90, in create
[Fri Oct 07 11:01:49.960695 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     module = import_module(entry)
[Fri Oct 07 11:01:49.960721 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Fri Oct 07 11:01:49.960726 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]     return _bootstrap._gcd_import(name[level:], package, level)
[Fri Oct 07 11:01:49.960735 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[Fri Oct 07 11:01:49.960742 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[Fri Oct 07 11:01:49.960748 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[Fri Oct 07 11:01:49.960770 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61279] ModuleNotFoundError: No module named 'corsheaders'
[Fri Oct 07 11:01:50.241210 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Failed to exec Python script file '/home/geekfest/geekstats/csgo-geekstats/gee>
[Fri Oct 07 11:01:50.241287 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] mod_wsgi (pid=11860): Exception occurred processing WSGI script '/home/geekfest/geekstats/csgo-geeks>
[Fri Oct 07 11:01:50.241442 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] Traceback (most recent call last):
[Fri Oct 07 11:01:50.241482 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/home/geekfest/geekstats/csgo-geekstats/geekstats/geekstats/wsgi.py", line 20, in <module>
[Fri Oct 07 11:01:50.241486 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     application = get_wsgi_application()
[Fri Oct 07 11:01:50.241494 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Fri Oct 07 11:01:50.241497 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     django.setup(set_prefix=False)
[Fri Oct 07 11:01:50.241504 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/__init__.py", line 24, in setup
[Fri Oct 07 11:01:50.241506 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     apps.populate(settings.INSTALLED_APPS)
[Fri Oct 07 11:01:50.241513 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]   File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 83, in populate
[Fri Oct 07 11:01:50.241516 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280]     raise RuntimeError("populate() isn't reentrant")
[Fri Oct 07 11:01:50.241532 2022] [wsgi:error] [pid 11860] [remote 108.41.247.85:61280] RuntimeError: populate() isn't reentrant

文件名:

import os,sys

sys.path.append('/home/geekfest/geekstats/.../geekstats')
sys.path.append('/usr/lib/python3/dist-packages')
sys.path.append('/home/geekfest/.local/lib/python3.8/site-packages')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'geekstats.settings')

application = get_wsgi_application()

Apache .conf虚拟主机定义:

<VirtualHost *:80>
    ServerName xxx.com
    ServerAlias stats.xxx.com
    DocumentRoot /home/geekfest/.../geekstats/stats
    ErrorLog ${APACHE_LOG_DIR}/gferror.log
    CustomLog ${APACHE_LOG_DIR}/gfaccess.log combined
    WSGIDaemonProcess xxx.com python-path=/usr/lib/python3.8:/home/geekfest/geekstats/...geekstats/geekstats:/usr/lib/python3/dist-packages:/home/geekfest/.local/lib/python3.8/site-packages
    WSGIProcessGroup xxx.com

# Tell Apache what it should call when the “xxx” url alias is called
    WSGIScriptAlias / /home/geekfest/.../geekstats/geekstats/wsgi.py

# Tell Apache where to find the “static” subdir for the django site
     Alias /static /home/geekfest/.../stats/static

# Give Apache the location and permissions to where the wsgi.py file resides in>
    <Directory /home/geekfest/.../geekstats/geekstats>
        <Files wsgi.py>
            Require all granted
            Options +Indexes +ExecCGI
        </Files>
    </Directory>

# Give Apache the location and permissions to the static directory (images) for>
    <Directory /home/geekfest/.../stats/static>
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>

</VirtualHost>
whlutmcx

whlutmcx1#

确保你已经安装了corheader。要安装它,你可以尝试pip install django-cors-headers。如果你正在为python包使用virtualenv,那么你必须在apache配置文件中指定virtualenv的位置。

wfypjpf4

wfypjpf42#

好了,我明白了。我知道这一定是路径挑战,因为模块在那里。如果你看上面,你会pip安装到/home。WSGI是从/user目录工作的。当我强迫Django告诉我其他工作库在哪里时,我看到了这个问题,并强制安装到/user。
指示pip强制重新安装另一个目录的命令是:

sudo pip install --upgrade --force-reinstall --target /usr/lib/python3/dist-packages/ django-cors-headers

相关问题