Apache无法加载Django应用问题求助
Django + Apache 无限加载问题解决步骤
核心问题定位
mod_wsgi进程无权限访问虚拟环境目录/home/jordanstarrk_production/heimat/venv,导致Python初始化失败,触发ModuleNotFoundError: No module named 'encodings'错误,最终引发Apache无限加载。
1. 修复虚拟环境目录权限
- 先检查当前目录权限:
(替换ls -ld /home/jordanstarrk_production/heimat/venv ls -l /home/jordanstarrk_production/heimat/venv/lib/pythonX.X/site-packagespythonX.X为你的Python版本,比如python3.10) - 给Apache运行用户(默认是
www-data)开放目录访问权限:# 给父目录添加执行权限(允许进入目录) chmod +x /home/jordanstarrk_production chmod +x /home/jordanstarrk_production/heimat # 将虚拟环境目录的组权限设为www-data,开放读取和执行权限 chown -R jordanstarrk:www-data /home/jordanstarrk_production/heimat/venv chmod -R g+rX /home/jordanstarrk_production/heimat/venv
2. 核对mod_wsgi配置
打开Apache的Django站点配置文件,确认以下参数正确:
# 指定虚拟环境根目录 WSGIDaemonProcess heimat python-home=/home/jordanstarrk_production/heimat/venv WSGIProcessGroup heimat # 绑定wsgi.py脚本,关联进程组 WSGIScriptAlias / /home/jordanstarrk_production/heimat/heimat/wsgi.py process-group=heimat application-group=%{GLOBAL}
注意:python-home必须指向虚拟环境的根目录,不是venv/bin。
3. 验证Apache用户访问权限
切换到www-data用户测试能否正常访问虚拟环境:
sudo su - www-data -s /bin/bash cd /home/jordanstarrk_production/heimat/venv python -c "import sys; print(sys.path)"
如果能正常输出Python路径,说明权限问题已解决;若仍报错,继续排查SELinux或目录权限。
4. 处理SELinux限制(若服务器启用)
如果服务器开启了SELinux,会阻止Apache访问用户家目录:
- 查看当前SEBool设置:
getsebool -a | grep httpd - 允许Apache读取用户目录内容:
setsebool -P httpd_read_user_content 1 setsebool -P httpd_enable_homedirs 1 - 或者给项目目录添加正确的SELinux上下文:
semanage fcontext -a -t httpd_sys_content_t "/home/jordanstarrk_production/heimat(/.*)?" restorecon -Rv /home/jordanstarrk_production/heimat
5. 重启Apache生效
完成所有配置后,重启Apache服务:
sudo systemctl restart apache2
内容的提问来源于stack exchange,提问作者ubuntuuber
相关产品推荐
相关产品推荐

