Apache 安装 #
安装前准备 #
系统要求 #
text
┌─────────────────────────────────────────────────────────────┐
│ Apache 系统要求 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 硬件要求: │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ CPU:1 核心以上(推荐 2 核心以上) │ │
│ │ 内存:512MB 以上(推荐 2GB 以上) │ │
│ │ 磁盘:100MB 以上(不含网站内容) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
│ 软件要求: │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 操作系统:Linux / Windows / macOS │ │
│ │ 依赖库:APR、APR-util、PCRE、OpenSSL(可选) │ │
│ │ 编译器:GCC 或 Clang(源码编译需要) │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
端口检查 #
安装前需要确保 80 端口(HTTP)和 443 端口(HTTPS)未被占用:
bash
# Linux/macOS
sudo netstat -tlnp | grep -E ':80|:443'
# 或
sudo lsof -i :80
sudo lsof -i :443
# 如果端口被占用,可以停止占用服务
# 例如停止 Nginx
sudo systemctl stop nginx
Linux 系统安装 #
Ubuntu/Debian #
bash
# 更新软件包索引
sudo apt update
# 安装 Apache
sudo apt install apache2 -y
# 查看安装版本
apache2 -v
# Server version: Apache/2.4.52 (Ubuntu)
# 启动 Apache
sudo systemctl start apache2
# 设置开机自启
sudo systemctl enable apache2
# 检查状态
sudo systemctl status apache2
CentOS/RHEL/Rocky Linux #
bash
# 更新软件包
sudo yum update -y
# 安装 Apache(CentOS 中叫 httpd)
sudo yum install httpd -y
# 查看安装版本
httpd -v
# Server version: Apache/2.4.53 (Rocky Linux)
# 启动 Apache
sudo systemctl start httpd
# 设置开机自启
sudo systemctl enable httpd
# 检查状态
sudo systemctl status httpd
# 配置防火墙(如果启用)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Fedora #
bash
# 安装 Apache
sudo dnf install httpd -y
# 启动服务
sudo systemctl start httpd
sudo systemctl enable httpd
openSUSE #
bash
# 安装 Apache
sudo zypper install apache2
# 启动服务
sudo systemctl start apache2
sudo systemctl enable apache2
Windows 系统安装 #
方法一:使用 XAMPP(推荐新手) #
text
┌─────────────────────────────────────────────────────────────┐
│ XAMPP 安装 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 下载 XAMPP │
│ 访问 https://www.apachefriends.org/ │
│ 下载 Windows 版本 │
│ │
│ 2. 运行安装程序 │
│ 双击下载的 .exe 文件 │
│ 选择安装目录(默认 C:\xampp) │
│ │
│ 3. 选择组件 │
│ ☑ Apache(必选) │
│ ☑ MySQL(可选) │
│ ☑ PHP(可选) │
│ ☑ phpMyAdmin(可选) │
│ │
│ 4. 完成安装 │
│ 启动 XAMPP Control Panel │
│ 点击 Apache 的 Start 按钮 │
│ │
└─────────────────────────────────────────────────────────────┘
方法二:官方二进制安装 #
text
┌─────────────────────────────────────────────────────────────┐
│ Apache Lounge 安装 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 下载 Apache │
│ 访问 https://www.apachelounge.com/download/ │
│ 下载 Windows 版本(VS17 版本) │
│ │
│ 2. 解压文件 │
│ 解压到 C:\Apache24 │
│ (目录结构:C:\Apache24\bin\httpd.exe) │
│ │
│ 3. 安装为 Windows 服务 │
│ 以管理员身份打开 CMD │
│ cd C:\Apache24\bin │
│ httpd.exe -k install │
│ │
│ 4. 启动服务 │
│ httpd.exe -k start │
│ 或通过服务管理器启动 │
│ │
│ 5. 测试 │
│ 浏览器访问 http://localhost │
│ │
└─────────────────────────────────────────────────────────────┘
Windows 常用命令 #
cmd
# 安装服务
httpd.exe -k install
# 卸载服务
httpd.exe -k uninstall
# 启动服务
httpd.exe -k start
# 停止服务
httpd.exe -k stop
# 重启服务
httpd.exe -k restart
# 测试配置文件
httpd.exe -t
macOS 系统安装 #
方法一:使用 Homebrew(推荐) #
bash
# 安装 Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 Apache
brew install httpd
# 启动服务
brew services start httpd
# 查看状态
brew services list
# 停止服务
brew services stop httpd
# 重启服务
brew services restart httpd
方法二:使用系统自带 Apache #
macOS 自带 Apache,但版本可能较旧:
bash
# 启动 Apache
sudo apachectl start
# 停止 Apache
sudo apachectl stop
# 重启 Apache
sudo apachectl restart
# 查看版本
httpd -v
# 配置文件位置
# /etc/apache2/httpd.conf
macOS 配置文件位置 #
text
┌─────────────────────────────────────────────────────────────┐
│ macOS Apache 文件位置 │
├─────────────────────────────────────────────────────────────┤
│ │
│ Homebrew 安装: │
│ 配置文件:/opt/homebrew/etc/httpd/httpd.conf │
│ 文档根目录:/opt/homebrew/var/www │
│ 日志目录:/opt/homebrew/var/log/httpd/ │
│ │
│ 系统自带: │
│ 配置文件:/etc/apache2/httpd.conf │
│ 文档根目录:/Library/WebServer/Documents │
│ 日志目录:/var/log/apache2/ │
│ │
└─────────────────────────────────────────────────────────────┘
Docker 安装 #
快速启动 #
bash
# 拉取官方镜像
docker pull httpd:latest
# 运行容器
docker run -d --name my-apache \
-p 80:80 \
-v /path/to/website:/usr/local/apache2/htdocs/ \
httpd:latest
# 访问
# http://localhost
Docker Compose 方式 #
yaml
# docker-compose.yml
version: '3.8'
services:
apache:
image: httpd:2.4
container_name: apache-server
ports:
- "80:80"
- "443:443"
volumes:
- ./htdocs:/usr/local/apache2/htdocs
- ./conf/httpd.conf:/usr/local/apache2/conf/httpd.conf
- ./logs:/usr/local/apache2/logs
restart: always
bash
# 启动
docker-compose up -d
# 停止
docker-compose down
# 查看日志
docker-compose logs -f
源码编译安装 #
下载源码 #
bash
# 下载最新版本
cd /usr/local/src
wget https://downloads.apache.org/httpd/httpd-2.4.58.tar.gz
wget https://downloads.apache.org/apr/apr-1.7.2.tar.gz
wget https://downloads.apache.org/apr/apr-util-1.6.3.tar.gz
# 解压
tar -xzf httpd-2.4.58.tar.gz
tar -xzf apr-1.7.2.tar.gz
tar -xzf apr-util-1.6.3.tar.gz
安装依赖 #
bash
# Ubuntu/Debian
sudo apt install -y build-essential libpcre3-dev libssl-dev libxml2-dev
# CentOS/RHEL
sudo yum groupinstall -y "Development Tools"
sudo yum install -y pcre-devel openssl-devel libxml2-devel expat-devel
编译安装 APR #
bash
cd /usr/local/src/apr-1.7.2
./configure --prefix=/usr/local/apr
make
sudo make install
编译安装 APR-util #
bash
cd /usr/local/src/apr-util-1.6.3
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make
sudo make install
编译安装 Apache #
bash
cd /usr/local/src/httpd-2.4.58
./configure \
--prefix=/usr/local/apache2 \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-so \
--enable-ssl \
--enable-rewrite \
--enable-mods-shared=most
make
sudo make install
配置 systemd 服务 #
bash
# 创建服务文件
sudo tee /etc/systemd/system/httpd.service << EOF
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecStop=/usr/local/apache2/bin/apachectl stop
ExecReload=/usr/local/apache2/bin/apachectl graceful
PIDFile=/usr/local/apache2/logs/httpd.pid
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start httpd
# 设置开机自启
sudo systemctl enable httpd
验证安装 #
检查版本 #
bash
# Linux/macOS
apache2 -v # Ubuntu/Debian
httpd -v # CentOS/RHEL/macOS
# Windows
httpd.exe -v
检查已加载模块 #
bash
# 列出已编译模块
apache2 -l # Ubuntu/Debian
httpd -l # CentOS/RHEL
# 列出已加载模块(包括动态模块)
apache2 -M # Ubuntu/Debian
httpd -M # CentOS/RHEL
测试配置文件 #
bash
# 检查配置语法
apache2ctl configtest # Ubuntu/Debian
apachectl configtest # CentOS/RHEL/macOS
httpd -t # 通用
# 输出 Syntax OK 表示配置正确
访问测试页面 #
text
┌─────────────────────────────────────────────────────────────┐
│ 验证安装成功 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 打开浏览器 │
│ │
│ 2. 访问以下地址: │
│ http://localhost (本机访问) │
│ http://127.0.0.1 (本机访问) │
│ http://服务器IP地址 (远程访问) │
│ │
│ 3. 看到以下页面表示成功: │
│ │
│ Ubuntu: "Apache2 Ubuntu Default Page" │
│ CentOS: "HTTP Server Test Page" │
│ 通用: "It works!" │
│ │
└─────────────────────────────────────────────────────────────┘
命令行测试 #
bash
# 使用 curl 测试
curl -I http://localhost
# 预期输出
HTTP/1.1 200 OK
Date: Sat, 29 Mar 2026 12:00:00 GMT
Server: Apache/2.4.52 (Ubuntu)
Content-Type: text/html;charset=UTF-8
安装后配置 #
修改文档根目录 #
bash
# Ubuntu/Debian 默认目录
/var/www/html
# CentOS/RHEL 默认目录
/var/www/html
# macOS 默认目录
/Library/WebServer/Documents
# 修改配置
sudo nano /etc/apache2/sites-available/000-default.conf
# 修改 DocumentRoot 指向新目录
设置目录权限 #
bash
# 设置所有者
sudo chown -R www-data:www-data /var/www/html # Ubuntu/Debian
sudo chown -R apache:apache /var/www/html # CentOS/RHEL
# 设置权限
sudo chmod -R 755 /var/www/html
配置防火墙 #
bash
# Ubuntu (UFW)
sudo ufw allow 'Apache'
sudo ufw allow 'Apache Secure'
# CentOS (firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
常见安装问题 #
端口被占用 #
bash
# 查找占用 80 端口的进程
sudo lsof -i :80
# 停止占用服务
sudo systemctl stop nginx
# 或修改 Apache 端口
# 编辑配置文件,将 Listen 80 改为其他端口
权限问题 #
bash
# 错误:Permission denied
# 解决:确保 Apache 用户对目录有读取权限
# 查看 Apache 运行用户
ps aux | grep httpd
# 设置正确权限
sudo chown -R apache:apache /var/www/html
sudo chmod -R 755 /var/www/html
SELinux 问题(CentOS/RHEL) #
bash
# 查看 SELinux 状态
getenforce
# 临时关闭(测试用)
sudo setenforce 0
# 永久关闭(修改配置文件)
sudo nano /etc/selinux/config
# 将 SELINUX=enforcing 改为 SELINUX=disabled
# 或设置正确的 SELinux 上下文
sudo chcon -R -t httpd_sys_content_t /var/www/html
目录结构 #
Ubuntu/Debian 目录结构 #
text
/etc/apache2/
├── apache2.conf # 主配置文件
├── conf-available/ # 可用配置
├── conf-enabled/ # 已启用配置(符号链接)
├── envvars # 环境变量
├── magic # MIME 类型定义
├── mods-available/ # 可用模块
├── mods-enabled/ # 已启用模块(符号链接)
├── ports.conf # 端口配置
├── sites-available/ # 可用站点
└── sites-enabled/ # 已启用站点(符号链接)
/var/www/html/ # 默认文档根目录
/var/log/apache2/ # 日志目录
CentOS/RHEL 目录结构 #
text
/etc/httpd/
├── conf/
│ ├── httpd.conf # 主配置文件
│ └── magic # MIME 类型定义
├── conf.d/ # 额外配置
├── conf.modules.d/ # 模块配置
└── logs -> ../../var/log/httpd # 日志目录
/var/www/html/ # 默认文档根目录
/var/log/httpd/ # 日志目录
下一步 #
安装完成后,继续学习 配置基础,了解 Apache 配置文件的结构和语法!
最后更新:2026-03-29