强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Apache HTTP Server 完全指南 / 安装与初始配置

安装与初始配置

本章介绍如何在不同操作系统上安装 Apache HTTP Server,包括包管理器安装和源码编译安装。

1. 包管理器安装

1.1 Debian/Ubuntu

# 更新包索引
sudo apt update

# 安装 Apache
sudo apt install apache2

# 启动服务
sudo systemctl start apache2

# 设置开机自启
sudo systemctl enable apache2

# 检查状态
sudo systemctl status apache2

# 检查版本
apache2 -v
# Server version: Apache/2.4.52 (Ubuntu)
# Server built:   2024-01-10T09:30:00

1.2 CentOS/RHEL/Rocky Linux

# 安装 Apache
sudo yum install httpd
# 或者在 CentOS 8+/Rocky Linux
sudo dnf install httpd

# 启动服务
sudo systemctl start httpd

# 设置开机自启
sudo systemctl enable httpd

# 检查状态
sudo systemctl status httpd

# 检查版本
httpd -v

1.3 Fedora

sudo dnf install httpd
sudo systemctl start httpd
sudo systemctl enable httpd

1.4 Arch Linux

sudo pacman -S apache
sudo systemctl start httpd
sudo systemctl enable httpd

1.5 macOS

# macOS 自带 Apache
# 启动
sudo apachectl start

# 停止
sudo apachectl stop

# 重启
sudo apachectl restart

# 或者使用 Homebrew 安装最新版
brew install httpd
brew services start httpd

1.6 Windows

  1. 下载 Apache Windows 版本:Apache HausApache Lounge
  2. 解压到目录,如 C:\Apache24
  3. 以管理员身份运行命令提示符:
cd C:\Apache24\bin
httpd.exe -k install
httpd.exe -k start

2. 源码编译安装

2.1 安装依赖

Debian/Ubuntu:

sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev libapr1-dev libaprutil1-dev libexpat1-dev

CentOS/RHEL:

sudo yum groupinstall "Development Tools"
sudo yum install pcre-devel openssl-devel zlib-devel apr-devel apr-util-devel expat-devel

2.2 下载源码

# 创建工作目录
mkdir -p ~/src/apache
cd ~/src/apache

# 下载 Apache 源码
wget https://archive.apache.org/dist/httpd/httpd-2.4.58.tar.gz

# 下载依赖库
wget https://archive.apache.org/dist/apr/apr-1.7.4.tar.gz
wget https://archive.apache.org/dist/apr-util/apr-util-1.6.3.tar.gz
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.42.tar.gz

# 解压
tar -xzf httpd-2.4.58.tar.gz
tar -xzf apr-1.7.4.tar.gz
tar -xzf apr-util-1.6.3.tar.gz
tar -xzf pcre2-10.42.tar.gz

2.3 编译依赖库

# 编译 APR
cd apr-1.7.4
./configure --prefix=/usr/local/apr
make
sudo make install

# 编译 APR-util
cd ../apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
sudo make install

# 编译 PCRE2
cd ../pcre2-10.42
./configure --prefix=/usr/local/pcre2
make
sudo make install

2.4 编译 Apache

cd ../httpd-2.4.58

# 配置编译选项
./configure \
    --prefix=/usr/local/apache2 \
    --with-apr=/usr/local/apr \
    --with-apr-util=/usr/local/apr-util \
    --with-pcre=/usr/local/pcre2 \
    --enable-so \
    --enable-ssl \
    --enable-rewrite \
    --enable-proxy \
    --enable-proxy-http \
    --enable-proxy-balancer \
    --enable-deflate \
    --enable-expires \
    --enable-headers \
    --enable-cache \
    --enable-file-cache \
    --enable-disk-cache \
    --enable-mem-cache \
    --enable-remoteip \
    --enable-unique-id \
    --enable-info \
    --enable-status \
    --with-mpm=event \
    --enable-mpms-shared=all

# 编译
make -j$(nproc)

# 安装
sudo make install

2.5 验证安装

# 检查版本
/usr/local/apache2/bin/apachectl -v

# 检查编译参数
/usr/local/apache2/bin/apachectl -V

# 检查加载的模块
/usr/local/apache2/bin/apachectl -M

# 测试配置
/usr/local/apache2/bin/apachectl configtest

# 启动服务
sudo /usr/local/apache2/bin/apachectl start

3. 模块编译

3.1 编译第三方模块

# 使用 apxs 编译模块
cd /path/to/module-source

# 方法 1:使用系统 apxs
apxs -cia mod_example.c

# 方法 2:使用自定义 apxs
/usr/local/apache2/bin/apxs -cia mod_example.c

apxs 参数说明:

参数说明
-c编译
-i安装
-a激活(添加 LoadModule)
-A激活但注释掉
-I包含路径
-L库路径
-l链接库

3.2 编译 mod_security 示例

# 下载 mod_security
git clone https://github.com/SpiderLabs/ModSecurity.git
cd ModSecurity

# 安装依赖
sudo apt install libyajl-dev libgeoip-dev liblua5.2-dev libcurl4-openssl-dev

# 编译
./autogen.sh
./configure --with-apxs=/usr/local/apache2/bin/apxs
make
sudo make install

# 编译 Apache 模块
cd apache2
/usr/local/apache2/bin/apxs -cia mod_security2.la

3.3 管理已编译模块

# 列出已安装模块
ls /usr/local/apache2/modules/

# 查看模块信息
/usr/local/apache2/bin/apachectl -l

# 动态加载模块
# 编辑 httpd.conf,添加:
# LoadModule example_module modules/mod_example.so

4. 初始配置

4.1 配置文件位置

安装方式配置文件路径
Debian/Ubuntu 包管理/etc/apache2/apache2.conf
CentOS/RHEL 包管理/etc/httpd/conf/httpd.conf
源码编译/usr/local/apache2/conf/httpd.conf

4.2 基本配置

# /etc/apache2/apache2.conf (Debian/Ubuntu) 或 /etc/httpd/conf/httpd.conf (CentOS)

# 服务器根目录
ServerRoot "/etc/apache2"

# 监听端口
Listen 80
Listen 443

# 服务器管理员邮箱
ServerAdmin admin@example.com

# 服务器名称
ServerName www.example.com:80

# 运行用户和组 (Linux)
User www-data
Group www-data

# 文档根目录
DocumentRoot "/var/www/html"

# 目录配置
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

# 默认文档
DirectoryIndex index.html index.php

# MIME 类型
TypesConfig /etc/mime.types
AddType application/x-httpd-php .php

# 日志配置
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

# 包含其他配置文件
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
IncludeOptional sites-enabled/*.conf

4.3 Debian/Ubuntu 特有配置

Debian/Ubuntu 使用分离的配置文件结构:

# 查看配置结构
ls -la /etc/apache2/
# apache2.conf      - 主配置文件
# ports.conf        - 端口配置
# envvars           - 环境变量
# magic             - MIME 类型魔术规则
# conf-available/   - 可用配置
# conf-enabled/     - 启用配置
# mods-available/   - 可用模块
# mods-enabled/     - 启用模块
# sites-available/  - 可用站点
# sites-enabled/    - 启用站点

# 启用模块
sudo a2enmod rewrite
sudo a2enmod ssl

# 禁用模块
sudo a2dismod status

# 启用站点
sudo a2ensite mysite.conf

# 禁用站点
sudo a2dissite mysite.conf

4.4 CentOS/RHEL 特有配置

# 查看配置结构
ls -la /etc/httpd/
# conf/httpd.conf        - 主配置文件
# conf.d/                - 额外配置
# conf.modules.d/        - 模块配置
# modules/               - 模块链接

# 配置防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

# SELinux 配置
sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P httpd_can_network_connect_db 1

4.5 验证配置

# 测试配置语法
sudo apachectl configtest
# 或
sudo apache2ctl configtest

# 输出应为:
# Syntax OK

# 查看解析后的配置
sudo apachectl -S
sudo apachectl -t -D DUMP_RUN_CFG
sudo apachectl -t -D DUMP_MODULES

# 启动/重启服务
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl reload apache2

# 检查端口监听
sudo ss -tlnp | grep -E ':80|:443'
# 或
sudo netstat -tlnp | grep -E ':80|:443'

4.6 测试页面

# 创建测试页面
echo "<h1>Apache is working!</h1>" | sudo tee /var/www/html/index.html

# 测试访问
curl -I http://localhost/
# HTTP/1.1 200 OK
# Date: Sat, 10 May 2026 12:00:00 GMT
# Server: Apache/2.4.58 (Ubuntu)
# Content-Type: text/html; charset=UTF-8

5. 业务场景

5.1 开发环境快速搭建

# 使用 apt 快速安装
sudo apt install apache2 php libapache2-mod-php mysql-server php-mysql
sudo systemctl start apache2 mysql

# 创建 PHP 测试页面
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

5.2 生产环境编译安装

# 使用自定义编译路径和模块
./configure --prefix=/opt/apache \
    --enable-ssl \
    --enable-http2 \
    --with-mpm=event \
    --enable-mods-shared=most

5.3 容器化部署

# 使用官方 Docker 镜像
docker run -d --name apache \
    -p 80:80 \
    -v /path/to/website:/usr/local/apache2/htdocs \
    httpd:2.4

6. 注意事项

  1. 权限问题:确保 Apache 用户对网站目录有读取权限
  2. 防火墙:开放 80 和 443 端口
  3. SELinux:CentOS/RHEL 需要配置正确的 SELinux 上下文
  4. 版本选择:生产环境建议使用 LTS 版本或发行版提供的稳定版本
  5. 安全更新:定期检查并应用安全更新

7. 扩展阅读

8. 总结

Apache 的安装方式多种多样,应根据实际需求选择:

  • 快速部署:使用包管理器
  • 定制需求:源码编译
  • 特殊模块:编译第三方模块
  • 容器环境:使用 Docker 镜像

安装完成后,务必进行配置验证和安全加固。