Apache默认根目录可修改,但必须同步更新DocumentRoot和对应<Directory>路径、设置权限及SELinux上下文(RHEL系),否则易报403或404;RHEL/CentOS默认为/var/www/html,Ubuntu/Debian同路径但配置文件在/etc/apache2/sites-enabled/000-default.conf,Windows多为安装目录下htdocs。
apache 默认网站根目录路径可以修改,但不能只改一个地方——documentroot 和对应的 <directory> 配置必须同步更新,否则大概率出现 403 禁止访问或 404 找不到页面。
确认当前默认路径
不同系统默认路径略有差异:
- RHEL/CentOS/Rocky/AlmaLinux(用
httpd包):默认是/var/www/html - Ubuntu/Debian(用
apache2包):默认是/var/www/html(主配置在/etc/apache2/sites-enabled/000-default.conf) - Windows(如 WAMP/XAMPP):通常是安装目录下的
htdocs文件夹,例如C:/wamp/bin/apache/Apache2.4/htdocs
验证方法:运行 grep "^DocumentRoot" /etc/httpd/conf/httpd.conf(RHEL系)或 grep -r "DocumentRoot" /etc/apache2/(Debian系),看实际配置值。
修改 DocumentRoot 和 Directory 配置
以 RHEL 系统为例(路径为 /etc/httpd/conf/httpd.conf):
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
- 用编辑器打开配置文件:
sudo vim /etc/httpd/conf/httpd.conf - 找到并修改
DocumentRoot行,例如改为:DocumentRoot "/home/www" - 紧接着找到对应
<Directory "...>块(通常就在 DocumentRoot 下方几行),把引号内路径也改成一致:<Directory "/home/www"> - 确保该
<Directory>块内包含权限放行语句(Apache 2.4+):Require all granted
设置目录权限与 SELinux 上下文(RHEL 系重点)
新目录不仅要存在,还要让 Apache 进程能读取:
- 创建目录并赋权:
sudo mkdir -p /home/www && sudo chmod -R 755 /home/www - 修改属主(RHEL 默认运行用户是
apache):sudo chown -R apache:apache /home/www - 若启用 SELinux(默认开启),必须修复安全上下文:
sudo semanage fcontext -a -t httpd_sys_content_t "/home/www(/.*)?"sudo restorecon -Rv /home/www
重启服务并验证
完成修改后务必检查语法并重启:
- 检查配置是否合法:
sudo httpd -t(RHEL)或sudo apache2ctl configtest(Debian)——无报错才能继续 - 重启服务:
sudo systemctl restart httpd(RHEL)或sudo systemctl restart apache2(Debian) - 浏览器访问服务器 IP 或
localhost,确认页面正常加载

















