安装与卸载
在开始之前,您需要先安装 Apache,不同 Linux 发行版的命令不同。

对于 Debian / Ubuntu 系统:
# 更新软件包列表 sudo apt update # 安装 apache2 sudo apt install apache2 # 卸载 apache2 sudo apt remove apache2
对于 CentOS / RHEL / Rocky Linux / AlmaLinux 系统:
# 安装 httpd (CentOS 7/8 使用 yum/dnf) sudo yum install httpd # CentOS 7 sudo dnf install httpd # CentOS 8 / RHEL 8 / Rocky Linux # 启动并设置开机自启 sudo systemctl start httpd sudo systemctl enable httpd # 卸载 httpd sudo yum remove httpd # CentOS 7 sudo dnf remove httpd # CentOS 8 / RHEL 8
核心服务管理命令 (Systemd)
在现代 Linux 系统中,我们使用 systemctl 命令来管理服务,这是最常用和最重要的命令集。
启动 Apache 服务
sudo systemctl start httpd
停止 Apache 服务
sudo systemctl stop httpd
重启 Apache 服务
这会先停止再启动,常用于应用配置文件更改。
sudo systemctl restart httpd
重新加载 Apache 服务
这会优雅地重新加载配置文件,不会中断现有的连接,推荐在修改了配置文件(如 httpd.conf)后使用。
sudo systemctl reload httpd
查看 Apache 服务状态
sudo systemctl status httpd
输出会显示服务是否正在运行(active (running)),以及最近的日志。

设置开机自启
让 Apache 在系统启动时自动运行。
sudo systemctl enable httpd
禁止开机自启
sudo systemctl disable httpd
检查 httpd 配置文件语法
在重启或重新加载服务之前,这是一个非常好的习惯,可以避免因配置错误导致服务无法启动。
sudo apachectl configtest # 或者 sudo httpd -t
- 输出
Syntax OK表示语法正确。 - 输出
AH00526或其他错误信息,表示配置文件有语法错误,会提示具体是哪一行。
测试与验证
检查监听端口
Apache 默认监听 80 端口,您可以使用 ss 或 netstat 命令来确认。
# 使用 ss 命令 (推荐) sudo ss -tulnp | grep :80 # 使用 netstat 命令 sudo netstat -tulnp | grep :80
如果看到类似 LISTEN 0 511 *:80 的输出,说明 Apache 正在监听 80 端口。

测试网页访问
在浏览器中访问服务器的 IP 地址或域名,如果您在本地服务器上操作,可以访问 http://localhost 或 http://127.0.0.1。
如果您看到了 "It works!" 或默认的 Apache 欢迎页面,说明安装和基本配置成功了。
日志管理
Apache 的日志是排查问题的关键,主要的日志文件位于 /var/log/ 目录下。
访问日志
记录所有对服务器的请求。
-
位置:
/var/log/httpd/access_log(RHEL/CentOS) 或/var/log/apache2/access.log(Debian/Ubuntu) -
查看命令:
# 查看最新的日志 tail -f /var/log/httpd/access_log # 搜索特定IP的访问记录 grep "192.168.1.100" /var/log/httpd/access_log # 统计访问量最多的IP awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr
错误日志
记录服务器运行中的错误、警告和信息。
-
位置:
/var/log/httpd/error_log(RHEL/CentOS) 或/var/log/apache2/error.log(Debian/Ubuntu) -
查看命令:
# 实时查看最新的错误信息 tail -f /var/log/httpd/error_log # 搜索包含 "File does not exist" 的错误 grep "File does not exist" /var/log/httpd/error_log
配置文件管理
Apache 的核心配置文件是 httpd.conf。
主配置文件位置
- RHEL/CentOS:
/etc/httpd/conf/httpd.conf - Debian/Ubuntu:
/etc/apache2/apache2.conf或/etc/apache2/sites-enabled/000-default.conf
常用配置指令示例
编辑配置文件通常需要 sudo 权限。
sudo nano /etc/httpd/conf/httpd.conf
常见配置项:
ServerName: 设置服务器的域名或 IP 地址。ServerName www.example.com:80。DocumentRoot: 网站文件存放的根目录。DocumentRoot "/var/www/html"。<VirtualHost>: 配置虚拟主机,用于在一台服务器上托管多个网站。<Directory>: 设置特定目录的权限,如是否允许覆盖、索引等。
模块管理
Apache 的功能通过模块来扩展,如 mod_rewrite (URL重写)、mod_ssl (HTTPS) 等。
启用模块
# 对于 RHEL/CentOS sudo yum install mod_ssl # 安装 SSL 模块 sudo systemctl restart httpd # 重启服务使模块生效 # 对于 Debian/Ubuntu sudo a2enmod ssl # 启用 SSL 模块 sudo systemctl restart apache2 # 重启服务
a2enmod是 Debian/Ubuntu 系统特有的启用模块命令。- 在 RHEL/CentOS 中,模块通常以
.so文件形式存在于/etc/httpd/modules/目录,并在主配置文件httpd.conf中通过LoadModule指令加载。
禁用模块
# 对于 Debian/Ubuntu sudo a2dismod ssl sudo systemctl restart apache2 # 对于 RHEL/CentOS,需要注释掉 httpd.conf 中的 LoadModule 行,然后重启
命令行工具摘要
| 命令 | 功能 | 示例 |
|---|---|---|
apachectl 或 httpd |
Apache 控制脚本,用于启动、停止、检查配置等 | sudo apachectl start |
apachectl configtest 或 httpd -t |
检查配置文件语法 | sudo apachectl configtest |
systemctl |
系统服务管理器,用于控制 httpd 服务 | sudo systemctl restart httpd |
ss 或 netstat |
检查端口监听情况 | sudo ss -tulnp \| grep :80 |
tail |
查看日志文件的末尾内容,常用于实时监控 | tail -f /var/log/httpd/error_log |
grep |
在日志文件中搜索特定内容 | grep "error" /var/log/httpd/error_log |
a2enmod / a2dismod |
(仅 Debian/Ubuntu) 启用/禁用 Apache 模块 | sudo a2enmod rewrite |
yum / dnf / apt |
包管理器,用于安装/卸载 Apache | sudo dnf install httpd |
希望这份详细的命令指南能帮助您更好地管理 Apache HTTP Server!
