apache默认首页由directoryindex指令控制,可编辑/etc/httpd/conf/httpd.conf(rhel系)或/etc/apache2/apache2.conf(debian系),修改directoryindex后重启httpd或apache2服务生效。
apache 默认首页文件由 directoryindex 指令控制,它定义用户访问目录(如 http://server/)时自动加载的文件名列表。修改主配置文件即可自定义该行为。
定位并编辑主配置文件
Apache 主配置文件路径为:
/etc/httpd/conf/httpd.conf(RHEL/CentOS/Rocky/AlmaLinux 系统)
或 /etc/apache2/apache2.conf(Debian/Ubuntu 系统)
使用文本编辑器打开(需 root 权限):
- CentOS/RHEL:
sudo vim /etc/httpd/conf/httpd.conf - Ubuntu/Debian:
sudo vim /etc/apache2/apache2.conf
找到并修改 DirectoryIndex 指令
在配置文件中搜索关键词 DirectoryIndex,通常位于“Main server configuration”段落中。默认行类似:
你可以按需扩展或替换为多个文件名,用空格分隔,Apache 会按顺序查找第一个存在的文件:
- 只支持 PHP 首页:
DirectoryIndex index.php - 兼容 HTML 和 PHP:
DirectoryIndex index.html index.htm index.php - 增加常见静态首页:
DirectoryIndex index.html index.htm index.php default.html home.html
注意:顺序很重要,前面的优先匹配;文件名区分大小写;路径是相对于 DocumentRoot 的。
保存配置并重启服务
修改后必须重启 Apache 才能生效:
- RHEL/CentOS 系:
sudo systemctl restart httpd - Debian/Ubuntu 系:
sudo systemctl restart apache2
可立即测试:访问 http://localhost/,若根目录下存在 index.php 但没有 index.html,且配置中 index.php 排在前面,则会自动加载 index.php。
验证是否生效
检查当前生效的 DirectoryIndex 值(含所有被包含的 conf 文件):
- 运行命令:
sudo httpd -t -D DUMP_RUN_CFG 2>/dev/null | grep DirectoryIndex(RHEL/CentOS) - 或查看完整解析后的配置:
sudo httpd -S(显示虚拟主机及关联的 DirectoryIndex)
若提示语法错误,请检查拼写、空格和引号——DirectoryIndex 后不加引号,多个文件名之间仅用空格,不可用逗号或换行。











