Apache配置静态网站的核心是修改httpd.conf中的DocumentRoot和对应<Directory>块,同步设置目录权限、SELinux上下文及防火墙规则;安装命令为yum install httpd -y,启动后需确保路径一致、Require all granted启用且restorecon修复SELinux上下文。

Apache 配置基础静态网站服务器,核心是修改主配置文件、指定网站根目录、设置目录权限,并确保服务正常运行。整个过程不复杂,但几个关键点容易出错,比如路径一致性、SELinux 限制和目录访问控制。
安装并启用 httpd 服务
Apache 在 CentOS/RHEL 系统中对应的软件包名为 httpd,不是 apache:
- 安装命令:
yum install httpd -y(CentOS 7)或dnf install httpd -y(CentOS 8+/RHEL 9) - 启动服务:
systemctl start httpd - 设为开机自启:
systemctl enable httpd - 检查状态:
systemctl status httpd,确认 Active: active (running)
修改网站根目录 DocumentRoot
默认网站文件放在 /var/www/html,如需自定义(例如改到 /myTest/apache1):
Apache Superset 是一个广泛采用的开源 BI 平台,用于 SQL 探索、图表构建和仪表板交付。当代理需要查询仓库数据、组装仪表板或使用成熟的分析界面解释指标而不是临时笔记本代码时,此技能非常有用。
- 创建目标目录并放入首页文件:
mkdir -p /myTest/apache1 && echo "Hello Apache" > /myTest/apache1/index.html - 编辑主配置文件:
vim /etc/httpd/conf/httpd.conf - 找到
DocumentRoot行(通常在第119行左右),改为:DocumentRoot "/myTest/apache1" - 注意:整个配置中只能有一个
DocumentRoot,后出现的会覆盖前面的
配置 Directory 权限控制
仅改 DocumentRoot 不够,必须同步更新对应 <Directory> 块,否则返回 403 Forbidden:
- 找到类似
<Directory "/var/www/html">的区块(通常在第124行附近) - 复制或修改为:
<Directory "/myTest/apache1"><br> AllowOverride None<br> Require all granted<br></Directory>
-
Require all granted是 Apache 2.4+ 的新语法,替代旧版的Order allow,deny+Allow from all
处理 SELinux 和防火墙
常见 403 或无法访问,往往不是配置问题,而是安全策略拦截:
- 检查 SELinux 状态:
getenforce,若为Enforcing,需调整上下文:semanage fcontext -a -t httpd_sys_content_t "/myTest/apache1(/.*)?"restorecon -Rv /myTest/apache1 - 开放防火墙端口(如使用 firewalld):
firewall-cmd --permanent --add-service=httpfirewall-cmd --reload - 临时禁用 SELinux 测试(仅调试用):
setenforce 0

















