精读笔记 · 第17章 Web 服务器(Apache)
大约 3 分钟
精读笔记 · 第17章 Web 服务器(Apache)
精读整理自 RHCE9 随堂讲义(原文字版 19 页)|同名视频约 11 节(第02套) 关联知识文件:
分章笔记/01-Linux/02-06-服务器部署(RHCE9第16-24章)
一、Web 基础概念
- B/S 架构取代 C/S 成为主流;HTML=超文本标记语言;网页(HTML/PHP/Java 写的文件)→ 主页(首个页面)→ 网站(多个网页)→ URL 统一资源定位符(
http://www.baidu.com:80/1.html)。 - 静态站点:Apache 建议 2.4+;LAMP = Linux + Apache + MySQL(MariaDB) + PHP(系统+服务+数据库+中间件)。
二、Apache(httpd)要点与部署
- 软件包
httpd;端口 80/tcp(http)、443/tcp(https);官网 www.apache.org。 - 关键路径:主配置
/etc/httpd/conf/httpd.conf、子配置/etc/httpd/conf.d/*.conf、网站主目录默认/var/www/html。 - 部署五连:
yum -y install httpd→systemctl start httpd→status→enable(开机自启)→ 关防火墙/setenforce 0;验证httpd -v。
三、虚拟主机(一台服务器跑多个网站)
- 类型:基于主机名(www.a.org / www.b.org),本课实验均用此方式。
- 配置套路(每站 3 步):
- 建网站目录并放 index.html:
mkdir /var/www/html/a.org; - 新建站点配置
/etc/httpd/conf.d/a.org.conf:
- 建网站目录并放 index.html:
<VirtualHost *:80>
ServerName www.a.org # 域名
DocumentRoot /var/www/html/a.org # 网站根目录
</VirtualHost>注意:网站目录不在默认 /var/www/html 下时(如 /b.org),需加目录授权:
<Directory "/b.org">
Require all granted
</Directory>- 语法检查 + 重启:
httpd -t(无报错)→systemctl restart httpd。
- 客户端测试:Linux 改
/etc/hosts(IP www.a.org)→yum install -y elinks→elinks http://www.b.org;Windows 改C:\Windows\System32\drivers\etc\hosts后用浏览器访问。
四、LAMP 部署论坛 Discuz!(动态站点)
- 基础环境:SELinux 永久 disabled(
sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config)+setenforce 0;停+禁用 firewalld。 - 安装 LAMP:
yum -y install httpd mariadb-server mariadb php php-mysqlnd gd php-gd→systemctl start/enable httpd mariadb(PHP8 配 Discuz3.5)。 - 导入源码:wget 下载 Discuz_X3.5_SC_UTF8.zip →
yum -y install unzip→ 解压 → 只把 upload/ 目录内容拷到/var/www/html(readme/utility 不要传)→chown -R apache.apache /var/www/html。 - 建库:
mysql→create database discuz;→set password=password('密码');→\q。 - 客户端加 hosts 解析 → 浏览器访问 http://IP 完成前台安装。
- 同法可部署 wordpress(博客,目录 /webroot/wordpress +
chmod -R 777)、ecshop(商城)、edusoho(网校,配 php-fpm + 虚拟主机)——思路都是“LAMP + 源码 + 建库 + 浏览器安装”。
五、命令速查表
| 场景 | 命令/路径 |
|---|---|
| 安装/启停 | yum -y install httpd、systemctl start/status/enable httpd |
| 版本/语法 | httpd -v、httpd -t |
| 配置位置 | /etc/httpd/conf/httpd.conf、/etc/httpd/conf.d/*.conf |
| 站点根目录 | /var/www/html |
| 虚拟主机 | <VirtualHost *:80> + ServerName + DocumentRoot(目录授权 <Directory>) |
| 客户端解析 | Linux /etc/hosts、Windows C:\Windows\System32\drivers\etc\hosts |
| 文本浏览器 | elinks http://域名、curl http://域名 |
| 数据库准备 | mysql → create database 名; → set password=password(...) |
六、易错点
httpd -t只在语法层面把关:目录不存在、权限不足、SELinux 拦截仍会 403/404。- 网站目录不在默认 html 下必须写
<Directory>授权Require all granted,否则 Forbidden。 - 访问不到先按顺序查:hosts 解析 / 服务状态 / 防火墙 / SELinux / httpd -t。
- Discuz 只需 upload 目录内容,别把 readme 等一起传;传完改属主 apache。
- 虚拟主机基于主机名:客户端不解析域名(hosts)就会落到默认站点或访问失败。
七、实操清单
单站 httpd 验证 80 端口 → 双虚拟主机 a.org/b.org(含目录授权)双域名 elinks 验证 → LAMP 装 Discuz(源码/建库/浏览器安装)→ 任选 wordpress 或 ecshop 复练。
八、本章自测
- Apache 主配置文件、子配置、默认站点目录?2. 虚拟主机两个核心指令?3. 目录不在默认 html 下要加什么?4. LAMP 各字母含义?5. Discuz 解压后应拷贝哪个目录、属主改成什么?6. 客户端 Linux/Windows 各改哪个 hosts?
