You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS7环境下使用Apache2虚拟主机部署Django应用故障咨询

Django+Apache公网部署报错解决方案

前置依赖检查

确认已安装和项目Python版本匹配的mod_wsgi模块:

yum install -y python3-mod_wsgi

1. 统一域名配置

当前配置存在多组域名不匹配问题,将以下4处配置修改为你实际用于访问的公网域名即可:

  • Apache虚拟主机的ServerName字段
  • WSGIDaemonProcess后接的进程名
  • WSGIProcessGroup后接的组名
  • settings.py中ALLOWED_HOSTS列表内的域名

2. 修正Apache虚拟主机配置

2.1 修正虚拟环境路径

WSGIDaemonProcess的python-home参数需指向虚拟环境根目录,而非Python库目录,同时添加python-path指定项目根目录,完整配置参考:

WSGIDaemonProcess 你的实际公网域名 processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/django/hello_world/mysite/venv python-path=/var/www/django/hello_world/mysite

2.2 修正标签大小写

Apache配置标签大小写敏感,将原配置的小写<directory>修改为首字母大写的<Directory>。

3. 配置权限与SELinux规则(CentOS7特有)

  • 给项目目录赋予Apache用户访问权限:
chown -R apache:apache /var/www/django/hello_world/mysite
chmod -R 755 /var/www/django/hello_world/mysite
  • 临时关闭SELinux验证问题:
setenforce 0

如果关闭后访问正常,执行以下命令永久放行Apache对项目目录的访问权限:

semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/django(/.*)?"
restorecon -Rv /var/www/django

4. 防火墙与服务重启

  • 放通HTTPS 443端口:
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload
  • 重启Apache服务生效配置:
systemctl restart httpd

配置完成后访问公网域名即可正常打开Django默认初始化页面。

内容的提问来源于stack exchange,提问作者GTS Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 23:36:02