Django与Apache 2.4部署报错:500错误及ModuleNotFoundError求助
Django部署到Apache 2.4出现ModuleNotFoundError的解决方案
环境信息
- Windows 11
- Python 3.11.5
- Apache 2.4
- Django项目名:ranking
错误日志
[Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] mod_wsgi (pid=16020): Failed to exec Python script file 'C:/Apache24/htdocs/django_file/ranking/ranking/wsgi.py'. [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] mod_wsgi (pid=16020): Exception occurred processing WSGI script 'C:/Apache24/htdocs/django_file/ranking/ranking/wsgi.py'. [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] Traceback (most recent call last): [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:/Apache24/htdocs/django_file/ranking/ranking/wsgi.py", line 19, in <module> [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] application = get_wsgi_application() [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ^^^^^^^^^^^^^^^^^^^^^^ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\wsgi.py", line 12, in get_wsgi_application [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] django.setup(set_prefix=False) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\__init__.py", line 19, in setup [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ^^^^^^^^^^^^^^^^^^^^^^^ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 102, in __getattr__ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] self._setup(name) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 89, in _setup [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] self._wrapped = Settings(settings_module) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ^^^^^^^^^^^^^^^^^^^^^^^^^ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 217, in __init__ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] mod = importlib.import_module(self.SETTINGS_MODULE) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "C:\Users\dkdle\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] return _bootstrap._gcd_import(name[level:], package, level) [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "<frozen importlib._bootstrap>", line 1204, in _gcd_import [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "<frozen importlib._bootstrap>", line 1176, in _find_and_load [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] File "<frozen importlib._bootstrap>", line 1135, in _find_and_load_unlocked [Mon Nov 13 20:44:10.540478 2023] [wsgi:error] [pid 16020:tid 1112] [client ::1:51548] ModuleNotFoundError: No module named 'ranking.settings'; 'ranking' is not a package
原虚拟主机配置
<VirtualHost *:80> ServerName my server domain WSGIScriptAlias / "C:/Apache24/htdocs/django_file/ranking/ranking/wsgi.py" WSGIPassAuthorization On <Directory "C:/Apache24/htdocs/django_file/ranking/ranking"> Options +ExecCGI AddHandler wsgi-script .py Require all granted </Directory> </VirtualHost>
原wsgi.py代码
import os import sys sys.path.append('/path/to/ranking/module') from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ranking.settings') application = get_wsgi_application()
修复步骤
1. 修正wsgi.py的Python路径配置
原wsgi.py中的sys.path.append('/path/to/ranking/module')是占位符,需要替换为包含manage.py的项目根目录,并使用sys.path.insert(0, ...)确保路径优先级最高:
import os import sys # 添加项目根目录(包含manage.py的目录)到Python路径 sys.path.insert(0, 'C:/Apache24/htdocs/django_file/ranking') from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ranking.settings') application = get_wsgi_application()
2. 调整Apache虚拟主机配置
添加WSGIDaemonProcess指定Python解释器环境,避免系统Python与项目依赖不一致;同时简化Directory配置:
<VirtualHost *:80> ServerName your-domain.com # 指定Python环境,替换为你的Python安装路径和项目根目录 WSGIDaemonProcess ranking python-path=C:/Apache24/htdocs/django_file/ranking python-home=C:/Users/dkdle/AppData/Local/Programs/Python/Python311 WSGIProcessGroup ranking WSGIScriptAlias / "C:/Apache24/htdocs/django_file/ranking/ranking/wsgi.py" WSGIPassAuthorization On <Directory "C:/Apache24/htdocs/django_file/ranking/ranking"> Require all granted </Directory> # 可选:配置静态文件访问 Alias /static/ "C:/Apache24/htdocs/django_file/ranking/static/" <Directory "C:/Apache24/htdocs/django_file/ranking/static"> Require all granted </Directory> </VirtualHost>
3. 验证项目结构
确保内层ranking目录下存在__init__.py文件(空文件即可),否则Python无法识别为包:
django_file/ └── ranking/ ├── manage.py └── ranking/ ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py
4. 重启Apache服务
修改配置后,重启Apache服务使更改生效。
内容的提问来源于stack exchange,提问作者rornfldla
相关产品推荐
相关产品推荐

