精读笔记(RHCA 英文教材)· RH358 Chapter 8 Configuring Web Servers
精读笔记(RHCA 英文教材)· RH358 Chapter 8 Configuring Web Servers
教材原文:RHCA 官方英文教材(教材第 19~20 页)(OCR 整书版已从本站移除,本页为章节精读) 说明:本手册文字层共 965 行 / 44 页,第 7/8/9 章均含同款 Structure 页,可继续逐章精读;本笔记按“讲义要点 + 必要补充”双语整理(httpd/Nginx/TLS 细节参考 RHEL8 官方文档)。 关联知识:RHCE9 精读服务类章节(httpd 配置思路)、
02-08-Ansible自动化(openssl 系列模块与部署自动化)、RH358 Chapter 1(systemd);本手册章节方法为 Lecture + Demo 与 Practice Exercise(区别于前几章 Guided Exercise)。
Chapter 8 | Configuring Web Servers(配置 Web 服务器)
- 一句话目标:In this chapter you will guide your students through the configuration of Apache HTTPD and Nginx, virtual hosts, TLS, and automation of the web service configurations.(带学生完成 Apache httpd 与 Nginx、虚拟主机、TLS 的配置,以及 Web 服务配置的自动化)
Chapter Objectives(本章目标,双语)
By the end of this chapter, students should be able to:
- Configure a basic web server using Apache HTTPD.(用 Apache HTTPD 配置基础 Web 服务器)
- Configure Apache HTTPD to provide IP-based and name-based virtual hosts.(配置 Apache httpd 提供基于 IP 与基于名字的虚拟主机)
- Configure Apache HTTPD to provide virtual hosts that use TLS to support the HTTPS protocol.(配置 Apache httpd 提供使用 TLS 的虚拟主机以支持 HTTPS)
- Configure a web server that provides HTTPS access to multiple virtual hosts using Nginx.(用 Nginx 提供多虚拟主机的 HTTPS 访问)
- Automate configuration of Apache HTTPD and Nginx web servers using Ansible.(用 Ansible 自动化 Apache httpd 与 Nginx 的配置)
Chapter Schedule(课时表,共 260 分钟)
| Section | 主题 | 方法(P:Lecture + Demo / A:Practice Exercise) |
|---|---|---|
| 1 | Configuring a Basic Web Server with Apache HTTPD | P 25 分钟 + A 30 分钟 |
| 2 | Configuring and Troubleshooting Virtual Hosts with Apache HTTPD | P 25 分钟 + A 20 分钟 |
| 3 | Configuring HTTPS with Apache HTTPD | P 40 分钟 + A 40 分钟 |
| 4 | Configuring a Web Server with Nginx | P 15 分钟 + A 30 分钟 |
| 5 | Automating Web Server Configuration | P 5 分钟 + A 15 分钟 |
| - | Lab(Performance Checklist) | 约 15 分钟 * |
- 注 *:表中 Lab 行原文排版损坏(“15 65 / Checklist 5”);按 Total 260 分钟减去前五节 245 分钟推断 Lab 为 15 分钟。
Key Takeaways(本章要点 5 条)
Apache HTTP Server and Nginx both provide support for running a web server on Red Hat Enterprise Linux.(Apache HTTP Server 与 Nginx 都能在 RHEL 上提供 Web 服务) Both web servers allow you to support multiple web sites on the same server.(两者都支持在同一台服务器上跑多个网站) The HTTPS protocol uses TLS private keys and server certificates to protect communication between web browsers and web servers.(HTTPS 用 TLS 私钥与服务器证书保护浏览器↔服务器通信) Each virtual host provided by the web server can have its own TLS certificate and private key, and the TLS certificate can support multiple names for the same site.(每个虚拟主机可拥有自己的 TLS 证书与私钥,且一张证书可包含同一站点的多个名字——SAN) Ansible provides tools that you can use to manage TLS private keys, certificate signing requests, and certificates, as well as automating your web server deployment and configuration.(Ansible 提供管理 TLS 私钥/CSR/证书的工具,并能自动化 Web 服务器部署与配置) 中文归纳考点:① httpd 与 Nginx 二选一都能跑 Web;② 一台服务器多站点=虚拟主机;③ HTTPS=TLS 私钥+服务器证书;④ 每虚拟主机可独立证书 + SAN 多域名;⑤ Ansible 管 TLS 私钥/CSR/证书 + 自动化部署。
补充精讲 A:Apache httpd 基础(Section 1 对应知识)
- 安装与启停:
dnf install -y httpd systemctl enable --now httpd systemctl status httpd - 关键文件/目录: | 路径 | 作用 | | --- | --- | |
/etc/httpd/conf/httpd.conf| 主配置文件 | |/etc/httpd/conf.d/*.conf| 配置片段(IncludeOptional,虚拟主机/SSL 多放这里) | |/var/www/html/| 默认 DocumentRoot | |/var/log/httpd/access_log、error_log| 访问/错误日志 | |httpd(用户/组) | 工作进程身份 | - 基本排障三连:
httpd -t(语法检查 Syntax OK)→systemctl reload httpd(改配置热加载)→curl http://localhost/验证。防火墙放行firewall-cmd --permanent --add-service=http && firewall-cmd --reload。 - SELinux:默认目录内容标为
httpd_sys_content_t即可读;Web 应用要写文件用httpd_sys_rw_content_t(如 /var/www/html 下的上传目录),必要时restorecon -Rv。 - 页面发布:往 DocumentRoot 放 index.html 后 curl 验证;默认页测试服务器“活着”。
补充精讲 B:虚拟主机(Section 2 对应知识)
- 概念(Key Takeaway 2):一台服务器、多个站点=虚拟主机。两种类型:name-based(按 Host 头区分,主流)与 IP-based(按不同 IP/端口区分,需多地址)。httpd 与 Nginx 都支持。
- httpd 配置示例(放
/etc/httpd/conf.d/):<VirtualHost *:80> ServerName www.example.com DocumentRoot /var/www/vhosts/www.example.com <Directory /var/www/vhosts/www.example.com> Require all granted </Directory> </VirtualHost> <VirtualHost *:80> ServerName intranet.example.com DocumentRoot /var/www/vhosts/intranet.example.com </VirtualHost> - 要点:
ServerName是 name-based 区分依据;首个/默认 vhost 充当未匹配请求的兜底。- 每个站点独立 DocumentRoot 与日志;目录要配
<Directory>访问控制(Require all granted)。 - 改完
httpd -t→systemctl reload httpd。
- 排障(Section 2 主题词 Troubleshooting):
httpd -S(列出所有 vhost 的 ServerName/别名,排查冲突);curl -H "Host: www.example.com" http://localhost/(模拟按名字访问);看 error_log。 - SELinux:各 vhost 的 DocumentRoot 也要
httpd_sys_content_t上下文(restorecon)。
补充精讲 C:用 Apache 配置 HTTPS(Section 3 对应知识)
- 原理(Key Takeaways 3、4):HTTPS 通信由 TLS 私钥 + 服务器证书保护;每个虚拟主机可有自己的证书与私钥;一张证书可含多个站点名(SAN:Subject Alternative Name),省证书、防“证书名不匹配”。
- 安装与最小配置:
dnf install -y mod_ssl # 提供 SSLEngine 等指令与默认 443 vhost<VirtualHost *:443> ServerName secure.example.com DocumentRoot /var/www/vhosts/secure.example.com SSLEngine on SSLCertificateFile /etc/pki/tls/certs/secure.example.com.crt SSLCertificateKeyFile /etc/pki/tls/private/secure.example.com.key </VirtualHost> - 自签证书生成(练习/内网用,SAN 示例):
openssl req -x509 -newkey rsa:2048 -nodes \ -keyout /etc/pki/tls/private/secure.example.com.key \ -out /etc/pki/tls/certs/secure.example.com.crt \ -days 365 -subj "/CN=secure.example.com" \ -addext "subjectAltName=DNS:secure.example.com,DNS:www.example.com" - 验证:
httpd -t+ reload;curl -k https://secure.example.com/(-k跳过自签校验);生产证书由受信 CA 签发,浏览器不报错。 - 防火墙需放行 https:
firewall-cmd --permanent --add-service=https && firewall-cmd --reload。
补充精讲 D:用 Nginx 配置 Web 服务(Section 4 对应知识)
- 安装与启停:
dnf install -y nginx;systemctl enable --now nginx;nginx -t语法检查;systemctl reload nginx。 - 文件布局:主配置
/etc/nginx/nginx.conf(含include /etc/nginx/conf.d/*.conf);站点片段放/etc/nginx/conf.d/;默认 root/usr/share/nginx/html;日志/var/log/nginx/。 - 虚拟主机=
server { }块(对应 httpd 的<VirtualHost>):server { listen 80; server_name www.example.com; root /usr/share/nginx/www.example.com; } server { listen 443 ssl; server_name secure.example.com; root /usr/share/nginx/secure.example.com; ssl_certificate /etc/pki/tls/certs/secure.example.com.crt; ssl_certificate_key /etc/pki/tls/private/secure.example.com.key; } - 要点:
server_name区分 name-based 站点;443 块带ssl参数即可做 HTTPS(每 vhost 独立证书/私钥,同 httpd 考点);nginx -t后 reload。 - 放行:
firewall-cmd --permanent --add-service={http,https};SELinux 对应httpd_sys_content_t同样适用 Nginx 静态内容(nginx 读取内容时走 httpd 类型策略,按需 restorecon)。 - 选型速记:httpd 配置风格(Directory/htaccess)成熟、生态大;Nginx 轻量高并发、配置简洁;RH358 要求两者都会配。
补充精讲 E:用 Ansible 自动化 Web 服务器配置(Section 5 对应知识)
- 教学口径:Section 5 只有 5 分钟 Lecture + 15 分钟 Practice Exercise——重点是把 A~D 的“安装→配置→放行”变成 playbook,并演示 Ansible 的 TLS 管理能力(Key Takeaway 5)。
- TLS 材料管理(考点:openssl 三件套):Ansible 用
openssl_privatekey、openssl_csr、openssl_certificate模块生成/管理私钥、证书签名请求与证书(自签可用selfsignedprovider,受信签发可对接 CA provider),证书内容与私钥可经模板落到目标机再由 httpd/nginx 引用。 - 部署自动化推荐模块组合(无红帽官方专属“web 角色”时的通用做法,与 RH358 Ch7 自动化同思路):handlers:
# 思路示例(非讲义原文) - name: Install web server dnf: name={{ item }} state=present loop: [httpd, mod_ssl] # nginx 场景换成 nginx - name: Write vhost config template: src=vhost.conf.j2 dest=/etc/httpd/conf.d/{{ item }}.conf notify: reload httpd - name: Open firewall firewalld: service={{ item }} permanent=yes state=enabled loop: [http, https]systemctl reload httpd/nginx;配置片段用 template 生成、改配置即 reload——幂等且可审计。 - 章节练习口径:Practice Exercise 让学生把“多虚拟主机 + HTTPS + 各站点独立证书”的整套配置用 playbook 复现并验证(对应 Objective 5)。
命令速查表
| 命令/文件 | 用途 |
|---|---|
dnf install httpd / nginx / mod_ssl | 安装 Web 服务与 SSL 模块 |
systemctl enable --now httpd / nginx | 启动并开机自启 |
httpd -t / nginx -t | 语法检查 |
systemctl reload httpd / nginx | 配置热加载 |
/etc/httpd/conf/httpd.conf、/etc/httpd/conf.d/ | httpd 主配置与片段 |
/etc/nginx/nginx.conf、/etc/nginx/conf.d/ | nginx 主配置与片段 |
/var/www/html/、/usr/share/nginx/html | 默认站点根目录 |
curl -H "Host: www.example.com" http://localhost/ | 按名字测 name-based vhost |
httpd -S | 列出全部 vhost(排查 ServerName 冲突) |
openssl req -x509 ... -addext subjectAltName=... | 生成自签证书(含 SAN) |
SSLEngine on + SSLCertificateFile/KeyFile | httpd 443 vhost 启用 TLS |
listen 443 ssl; + ssl_certificate(_key) | nginx server 块启用 TLS |
firewall-cmd --add-service={http,https} | 放行 80/443 |
restorecon -Rv <目录> | 修正 SELinux 文件上下文 |
| openssl_privatekey / openssl_csr / openssl_certificate | Ansible 管理 TLS 私钥/CSR/证书 |
| dnf+template+service+firewalld(+handler reload) | Web 部署自动化模块组合 |
核心词汇表
| 英文 | 中文速记 |
|---|---|
| Apache HTTPD (httpd) | Apache Web 服务器(RHEL 包名 httpd) |
| Nginx | 高并发轻量 Web 服务器(RHEL 包名 nginx) |
| virtual host | 虚拟主机(一台服务器多个网站) |
| name-based / IP-based vhost | 按 Host 头 / 按 IP 区分的虚拟主机 |
| ServerName / server_name | httpd / nginx 的站点名指令 |
| DocumentRoot / root | httpd / nginx 站点根目录 |
| TLS | 传输层安全协议(HTTPS 底层) |
| HTTPS | 443/TCP,TLS 保护的 HTTP |
| server certificate / private key | 服务器证书 / 私钥 |
| SSLEngine / SSLCertificateFile / SSLCertificateKeyFile | httpd 的 TLS 开关/证书/私钥指令 |
| ssl_certificate / ssl_certificate_key | nginx 的证书/私钥指令 |
| SAN(Subject Alternative Name) | 证书主题备用名(一张证书多域名) |
| self-signed certificate | 自签证书(openssl req -x509 生成) |
| mod_ssl | httpd 的 SSL/TLS 模块 |
| conf.d | 配置片段目录(httpd 与 nginx 通用约定) |
| httpd_sys_content_t / httpd_sys_rw_content_t | Web 内容只读 / 可写 SELinux 上下文 |
| openssl_privatekey / openssl_csr / openssl_certificate | Ansible 的私钥 / CSR / 证书模块 |
| httpd -S | 打印 vhost 汇总(排障) |
| Practice Exercise | 本章练习形式(动手复现手动/自动化配置) |
本章自测
- RHEL8 上“Apache 还是 Nginx”怎么选?两者在“多站点”“HTTPS”上能力是否一致(对照 Key Takeaways 1、2)?
- name-based 与 IP-based 虚拟主机区别?httpd 用什么指令给虚拟主机取名字、放在哪个目录?
- 配置完 httpd 虚拟主机,验证与排障的命令序列(语法→重载→按名字 curl→vhost 汇总)?
- HTTPS 为什么需要“私钥 + 证书”各一份?能不能每个虚拟主机各用各的证书?一张证书怎么支持多个站点名(SAN)?
- 写出 httpd 443 虚拟主机的四个关键指令;自签证书用什么 openssl 命令生成(含 SAN)?
- nginx 里“虚拟主机”叫什么?80 与 443 的 server 块各怎么写?
nginx -t与 reload 何时用? - 部署 Web 服务器自动化,安装/配置/放行/重载分别对应哪些模块?为什么配置变更用 handler 触发 reload?
- Ansible 管理 TLS 的“openssl 三件套”是哪三个模块?分别生成什么?
- 防火墙与 SELinux 各要做什么才能让外部客户端正常访问 80/443?(提示:firewalld 服务名 + 内容目录上下文)
- 把 Objectives 五条与 Key Takeaways 五条对应起来;再用“虚拟主机 + TLS”一句话解释“为什么一台服务器能安全地跑多个 HTTPS 网站”。
