# 17.1 Apache Web 服务器

Web 服务器是现代互联网基础设施的核心组件，负责接收并响应客户端的 HTTP 请求，构成了万维网的基础通信架构。

Apache HTTP Server 作为历史最悠久、应用最广泛的开源 Web 服务器之一，在企业级应用和学术研究中都占有重要地位，其稳定的性能表现和丰富的功能集使其成为 Web 服务部署的首选方案之一。

本章将系统介绍地在 FreeBSD 操作系统上部署 Apache Web 服务器的方法与实践。

## 安装 Apache

Apache HTTP Server（Apache）是一款遵循 Apache License 2.0 协议的开源 Web 服务器软件，以其模块化架构、跨平台兼容性和高度可配置性著称，其设计遵循高度可扩展性原则，允许用户通过加载不同的模块来扩展功能。在 FreeBSD 上可以通过 Ports 或 pkg 两种方式进行安装。

使用 Ports 方式安装 Apache，这种方式提供了最大的配置灵活性，允许用户在编译前进行定制化配置，需要先进入对应的 Ports 目录，然后执行编译安装命令。

```sh
# cd /usr/ports/www/apache24/
# make install clean
```

或者使用 pkg 包管理器进行安装，这种方式更加快捷方便，能够自动处理依赖关系，适合快速部署场景。

```sh
# pkg install apache24
```

## 查看安装后说明

安装完成后，建议查看安装包的说明文档，了解必要的配置和注意事项，这些信息对于正确配置和运行 Apache 至关重要。

```sh
# pkg info -D apache24
apache24-2.4.63:
On install:
To run apache www server from startup, add apache24_enable="yes"
in your /etc/rc.conf. Extra options can be found in startup script.
# 要使 Apache WWW 服务器在启动时自动运行，请在 /etc/rc.conf 文件中添加 apache24_enable="yes"。
# 额外的选项可以在启动脚本中找到。

Your hostname must be resolvable using at least 1 mechanism in
/etc/nsswitch.conf typically DNS or /etc/hosts or apache might
have issues starting depending on the modules you are using.

# 你的主机名必须能够通过 /etc/nsswitch.conf 中至少一种机制进行解析，通常是 DNS 或 /etc/hosts，
# 否则，Apache 可能在启动时会遇到问题，具体取决于你使用的模块。

- apache24 default build changed from static MPM to modular MPM
- more modules are now enabled per default in the port
- icons and error pages moved from WWWDIR to DATADIR
# - apache24 默认构建从静态 MPM（多处理模块）更改为模块化 MPM。
# - 现在 Ports 中默认启用了更多模块。
# - 图标和错误页面从 WWWDIR 移动到了 DATADIR。

   If built with modular MPM and no MPM is activated in
   httpd.conf, then mpm_prefork will be activated as default
   MPM in etc/apache24/modules.d to keep compatibility with
   existing php/perl/python modules!
# 如果使用模块化 MPM 构建并且在 httpd.conf 中没有激活 MPM，那么 mpm_prefork 将作为默认的 MPM 在 etc/apache24/modules.d 中激活，
# 以保持与现有的 PHP/Perl/Python 模块的兼容性！

Please compare the existing httpd.conf with httpd.conf.sample
and merge missing modules/instructions into httpd.conf!
# 请将现有的 httpd.conf 文件与 httpd.conf.sample 文件进行比较，并将缺失的模块/指令合并到 httpd.conf 文件中！

On upgrade:
The default database format was changed from BerkeleyDB 5
(deprecated) from ports to BerkeleyDB 1 from base libc.
# 升级时：
# 默认的数据库格式已从 BerkeleyDB 5（已弃用，来自 Ports）切换到 base libc 中的 BerkeleyDB 1。

You must rebuild all your (RewriteMap) databases once after
upgrading from 2.4.62 to 2.4.62_1 or later.
# 升级后，你必须重新构建所有的 (RewriteMap) 数据库，特别是在从 2.4.62 升级到 2.4.62_1 或更高版本之后。
```

## 守护进程

为了让 Apache 在系统启动时自动运行，确保服务的持久性和可用性，需要将其配置为开机自启服务，这是生产环境部署的标准做法。

添加服务开机自启：

```sh
# service apache24 enable   # 设置 Apache HTTP Server 2.4 在系统启动时自动启动
```

## 启动 Apache

完成开机自启配置后，可以手动启动 Apache 服务并检查其运行状态，验证服务是否正常工作。

启动服务（可能需要一分钟，具体时间取决于系统配置和硬件性能）：

```sh
# service apache24 start   # 启动 Apache HTTP Server 2.4 服务
Performing sanity check on apache24 configuration:
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 198.18.0.103. Set the 'ServerName' directive globally to suppress this message
Syntax OK
Starting apache24.
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 198.18.0.103. Set the 'ServerName' directive globally to suppress this message
```

查看 Apache HTTP Server 2.4 服务状态：

```sh
# service apache24 status
apache24 is running as pid 2068.
```

Apache 服务启动后，可以通过访问 `localhost` 或对应的 IP 地址进行验证，确认 Web 服务正常响应请求：

![Apache FreeBSD](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-bfd439dee19407e4bdfa1f2c53f33e8ae15af879%2FApache1.png?alt=media)

## 附录：Apache 相关文件路径

以下是 Apache 在 FreeBSD 系统中的主要文件和目录路径，供配置和维护时参考。这些路径遵循 FreeBSD 的文件系统层次标准（FHS），将第三方软件安装在 `/usr/local` 目录下，与基本系统中用户空间的软件分离，这种设计有助于系统的维护和升级管理。

目录结构：

```sh
/usr/local/
├── etc/
│   └── apache24/
│       ├── httpd.conf          # Apache 主配置文件（实际生效的配置）
│       └── httpd.conf.sample   # 示例配置文件（参考模板，不直接使用）
└── www/
    └── apache24/
        └── data/               # 网站根目录（存放网页内容，如 index.html）
```

说明：

* Apache 配置文件位于 `/usr/local/etc/apache24/httpd.conf`
* Apache 示例文件位于 `/usr/local/etc/apache24/httpd.conf.sample`
* Apache 网站根目录位于 `/usr/local/www/apache24/data/`

## 课后习题

1. 为 FreeBSD Port www/apache24 新增目前缺失的 make 选项。
2. 在 FreeBSD 中，基于 Apache24 配置 HTTPS 支持，实现 SSL 自动签名。
3. 在 FreeBSD 中，基于 Apache24 部署一个 WordPress 或 Typecho。
