在 Linux 中启动、停止与重启 Apache

Apache 是开源的跨平台 HTTP 服务器,广泛用于托管网站与 Web 应用。启动、停止、重启和重新加载 Apache 服务,是开发者和系统管理员最日常的任务之一。

本指南讲解如何在 Ubuntu、Debian、Fedora、RHEL 及其衍生发行版上使用 systemctl 管理 Apache 服务。

速查表

如需可打印的速查表,请参阅 systemctl 备忘单。

在 Ubuntu 和 Debian 上,Apache 服务名为 apache2;在 Fedora、RHEL 以及 AlmaLinux、Rocky Linux 等衍生版上,服务名为 httpd。请将命令中的服务名替换为你系统对应的名称。

操作Ubuntu / DebianFedora、RHEL 及其衍生版
启动sudo systemctl start apache2sudo systemctl start httpd
停止sudo systemctl stop apache2sudo systemctl stop httpd
重启sudo systemctl restart apache2sudo systemctl restart httpd
重新加载sudo systemctl reload apache2sudo systemctl reload httpd
查看状态sudo systemctl status apache2sudo systemctl status httpd
开机自启sudo systemctl enable apache2sudo systemctl enable httpd
禁用开机自启sudo systemctl disable apache2sudo systemctl disable httpd
测试配置sudo apachectl configtestsudo apachectl configtest

准备工作

本指南中的命令需要 root 或 sudo 权限。

使用 systemctl 管理 Apache 服务时,可接受的参数如下:

  • start(启动)— 启动 Apache 服务。
  • stop(停止)— 终止 Apache 服务。
  • restart(重启)— 先停止再启动 Apache 服务。这会造成短暂中断,并可能关闭活动连接,具体取决于服务单元停止 Apache 的方式。
  • reload(重新加载)— 重新加载配置而不中断活动连接。Apache 主进程会通知工作进程完成当前请求,再以新配置重启它们。
  • status(状态)— 显示服务当前状态,包括是否正在运行以及最近日志输出。

在 Ubuntu 与 Debian 上启动、停止、重启和重新加载 Apache

在 Ubuntu(26.04、22.04、20.04、18.04)和 Debian 上,Apache 服务名为 apache2,由 systemctl 管理。

要启动 Apache 服务,运行:

sudo systemctl start apache2

要停止 Apache 服务,运行:

sudo systemctl stop apache2

要重启 Apache 服务,运行:

sudo systemctl restart apache2

要在修改配置后重新加载 Apache 而不中断活动连接,运行:

sudo systemctl reload apache2

在带有活动客户端的正式服务器上,如果只需应用配置变更,应使用 reload 而非 restart。

要查看 Apache 服务当前状态,运行:

sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
     Active: active (running) since Sun 2026-03-01 14:00:00 UTC; 2h ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 12346 (apache2)
      Tasks: 55 (limit: 4915)
     Memory: 12.4M
     CGroup: /system.slice/apache2.service
             ├─12346 /usr/sbin/apache2 -k start
             ├─12347 /usr/sbin/apache2 -k start
             └─12348 /usr/sbin/apache2 -k start

Active 一行确认 Apache 正在运行;Loaded 一行中的 enabled 表示服务会在开机时自动启动;Main PID 是父进程,CGroup 下方的条目则是处理请求的各 Apache 工作进程。

开机时启用或禁用 Apache

要让 Apache 在系统启动时自动运行,运行:

sudo systemctl enable apache2

要阻止 Apache 在开机时自动启动,运行:

sudo systemctl disable apache2

在 Fedora、RHEL 及其衍生版上启动、停止、重启和重新加载 Apache

在 Fedora、RHEL 以及 AlmaLinux、Rocky Linux 等衍生版上,Apache 服务名为 httpd。

要启动 Apache 服务,运行:

sudo systemctl start httpd

要停止 Apache 服务,运行:

sudo systemctl stop httpd

要重启 Apache 服务,运行:

sudo systemctl restart httpd

要在修改配置后重新加载 Apache 而不中断活动连接,运行:

sudo systemctl reload httpd

要查看 Apache 服务当前状态,运行:

sudo systemctl status httpd

要让 Apache 在开机时自动启动,运行:

sudo systemctl enable httpd

要禁用开机自动启动,运行:

sudo systemctl disable httpd

测试 Apache 配置

在重启或重新加载 Apache 之前,先用 apachectl 测试配置是否存在语法错误:

sudo apachectl configtest

如果配置有效,输出会显示:

Syntax OK

如果存在错误,输出会描述问题,并给出文件路径与行号。重新加载或重启 Apache 之前,请修复所有报错,避免因为配置错误导致服务器宕机。

使用 service 命令启动、停止和重启 Apache

较早的运维手册与部署脚本通常通过 service 命令而非 systemctl 来调用 Apache。这些命令在当前 Debian 和 Ubuntu 版本上仍然可用,因为 service 是一个小型封装脚本,它会检测当前运行的 init 系统,并代为把请求转发给 systemctl。

要以这种方式重启 Apache,运行:

sudo service apache2 restart

它接受的参数与上述一致:

sudo service apache2 start

sudo service apache2 stop

sudo service apache2 reload

sudo service apache2 status

在 Fedora、RHEL 及其衍生版上,等价命令是 sudo service httpd restart,但该封装脚本位于单独的软件包中,最小化安装时可能并未包含。在这些系统上请使用 systemctl。

在已经运行 systemd 的机器上,切换到 service 没有任何好处,现有脚本中使用 service 也无需修改。

故障排查

配置变更后 Apache 无法启动:启动前先运行 sudo apachectl configtest 检查语法错误,并查看错误输出定位出问题的文件与行号。

端口 80 或 443 已被占用:可能有其它进程正在监听该端口。用 sudo ss -tlnp 'sport = :80' 检查 80 端口(排查 HTTPS 时把 80 换成 443)。停止冲突进程,或在虚拟主机配置中把 Apache 改为使用其它端口。

找不到 apache2.service 或 httpd.service 单元:Apache 可能尚未安装,或服务名与你的发行版不符。用 systemctl list-unit-files | grep -E '^(apache2|httpd)\.service' 确认已安装了哪个服务单元。

重新加载失败:配置中存在错误,导致 reload 无法完成。重新加载前先运行 sudo apachectl configtest 定位并修复问题。

Apache 正在运行但不响应请求:检查防火墙规则。在 Ubuntu/Debian 上运行 sudo ufw status;在 RHEL 系系统上运行 sudo firewall-cmd --list-all。确保端口 80(HTTP)和 443(HTTPS)已开放。

配置修改未生效:编辑任何配置文件后,运行 sudo systemctl reload apache2(或 httpd)以在不重启的情况下应用变更。

常见问题

restart 与 reload 有何区别?restart 会先停止再启动 Apache 服务,会产生短暂中断,并可能关闭活动连接;reload 则通知 Apache 主进程重新读取配置文件,并优雅地重启工作进程,不中断活动连接。在正式服务器上做常规配置变更时,请使用 reload。

如何确认 Apache 是否正在运行?在 Ubuntu/Debian 上运行 sudo systemctl status apache2,在 RHEL 系系统上运行 sudo systemctl status httpd。也可以运行 curl -I http://localhost 来验证 Apache 是否在响应 HTTP 请求。

如何让 Apache 在重启后自动启动?在 Ubuntu/Debian 上运行 sudo systemctl enable apache2,在 RHEL 系系统上运行 sudo systemctl enable httpd。要确认,可运行 sudo systemctl is-enabled apache2,输出应为 enabled。

Apache 的日志文件在哪里?在 Ubuntu/Debian 上,日志位于 /var/log/apache2/(access.log 与 error.log);在 RHEL 系系统上,日志位于 /var/log/httpd/。可用 journalctl 查看 systemd 服务日志:sudo journalctl -u apache2。

结语

现在你已经掌握了在 Ubuntu、Debian、Fedora 及 RHEL 系系统上启动、停止、重启、重新加载和管理 Apache Web 服务器的方法。在正式服务器上应用配置变更时,请使用 reload 而非 restart,以避免中断活动连接;并且始终先运行 apachectl configtest,在错误拖垮服务器之前发现问题。

如需查看完整的 Apache 命令列表,请参阅《你应当了解的 Apache 命令》指南。