码上问学问代码之理,学编程之道
教材同步›项目 11
教材同步 · 叶廷东《Linux 操作系统管理(第 2 版)》航空工业出版社 · “十四五”国家规划教材

项目11 Web 服务器(Apache)

Web 服务器把网页送给浏览器。装 Apache httpd、改 DocumentRoot、apachectl configtest 校验、放行 http。

任务 11.1

服务概述

Apache / httpd

Web 服务器把网页送给浏览器。RHEL 系默认 Apache(软件包 httpd)。教材项目 11。

任务 11.2

安装与配置

从装包到写出配置文件
11.2.1安装软件包

dnf install -y httpd;守护进程 httpd,管理命令 apachectl。

11.2.2配置文件 /etc/httpd/conf/httpd.conf

关键指令:Listen 80、ServerName、DocumentRoot "/var/www/html"(网页根)、DirectoryIndex index.html。网页放 DocumentRoot 即可访问。

conf
关键指令:<code>Listen 80</code>、<code>ServerName</code>、<code>DocumentRoot "/var/www/html"</code>(网页根)、<code>DirectoryIndex index.html</code>。网页放 DocumentRoot 即可访问。
任务 11.3

启动、放行与测试

让服务跑起来并验证
11.3.1启动与防火墙

启动服务并设置开机自启;用 firewall-cmd --add-service=http --permanent 放行,再 --reload。生产服务器只开必要端口。

bash
systemctl enable --now http
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
Created symlink ... success (规则已重载)
11.3.2测试验证

在 /var/www/html 放一个 index.html,浏览器访问 http://服务器IP 应看到页面;curl -I localhost 返回 200 即正常。

11.3.3关键命令参数卡
随身参数卡 · Apache(httpd) 命令
apachectl
  • apachectl configtest 校验配置语法
  • apachectl start/stop/restart
  • apachectl -S 看虚拟主机与端口
  • httpd -t 等价 configtest
放行 / 测
  • firewall-cmd --add-service=http --permanent
  • firewall-cmd --add-service=https --permanent
  • systemctl enable --now httpd
  • curl -I localhost 看响应头验证
完整五步实战在这里:网络服务配置实战 · Web(Apache) 按「场景 → 配置 → 启动 → 测试 → 排障」详细展开,含多服务集成与排障法,本项目只给命令与参数卡。

拓展训练

教材每项目末尾 · 上机可重做取最高分
1
在虚拟机里安装并启动 Apache / httpd,用 systemctl status 确认 Active(running)。
2
用 firewall-cmd --add-service=http --permanent 放行后 --reload,从另一台机器测试能连通。
3
故意写错一处配置,用本项目参数卡里的『校验命令』找出错误并修复。
4
把该服务设为开机自启(systemctl enable),重启虚拟机验证自动起来。

服务项目重在『排障』:连不上时按 配置→端口→防火墙→日志 逐层查,journalctl -u 服务名 是第一现场。