Apache/mod_wsgi部署Django时corsheaders模块缺失的500错误求助
Django部署Apache/mod_wsgi时出现
corsheaders模块找不到的500错误 本地用runserver运行Django项目完全正常,但部署到Apache(v2.4.41)+mod_wsgi时触发500内部服务器错误,错误提示ModuleNotFoundError: No module named 'corsheaders'。注释掉settings.py里corsheaders相关的应用和中间件后,站点能正常运行,但依赖该模块的API功能失效。已经尝试过多种pip卸载重装django-cors-headers的操作,都没解决问题,推测是WSGI无法识别这个模块。
相关版本信息
- django-cors-headers==3.13.0(安装路径:
/home/geekfest/.local/lib/python3.8/site-packages) - django-cors-middleware==1.5.0
- python==3.8.10
- Django==4.1.2
- Ubuntu==20.04.1
Python路径:['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/home/geekfest/.local/lib/python3.8/site-packages', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
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
Apache错误日志(error.log)
[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
wsgi.py配置
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虚拟主机配置
<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>
内容的提问来源于stack exchange,提问作者Mailboxhead
相关产品推荐
相关产品推荐

