温馨提示×

如何测试Apache2是否运行

小樊
54
2026-07-06 04:14:11
栏目: 编程语言

要测试 Apache2 是否正在运行,可以根据你使用的操作系统,选择以下几种常用方法(适用于大多数 Linux 发行版)。


一、使用 systemctl(推荐,适用于 systemd 系统)

systemctl status apache2

✅ 如果看到类似以下内容,说明 Apache2 正在运行:

Active: active (running) since ...

❌ 如果显示:

Active: inactive (dead)

说明 Apache2 没有运行。

启动 Apache2:

sudo systemctl start apache2

设置开机自启:

sudo systemctl enable apache2

二、使用 service 命令(较老系统)

sudo service apache2 status

或:

sudo /etc/init.d/apache2 status

三、检查 Apache 进程

ps aux | grep apache2

如果看到多个 apache2 进程,通常说明它正在运行。


四、检查端口监听(默认 80 或 443)

ss -tlnp | grep :80

或:

netstat -tlnp | grep :80

✅ 看到 LISTEN 状态说明 Apache 正在监听 HTTP 端口。


五、通过浏览器访问

在浏览器中访问:

http://localhost

或服务器 IP:

http://服务器IP

✅ 看到 Apache2 默认欢迎页面 说明运行正常。


六、使用 curl 测试(服务器本机)

curl http://localhost

或:

curl http://127.0.0.1

如果返回 HTML 内容,说明 Apache2 正常运行。


七、检查配置文件是否正确

sudo apache2ctl configtest

✅ 返回:

Syntax OK

说明配置文件没有语法错误。


常见问题排查

Apache 启动失败?

查看日志:

sudo journalctl -xe

或:

sudo tail -n 50 /var/log/apache2/error.log

总结(最常用)

推荐命令:

systemctl status apache2

如果你告诉我你的系统版本(如 Ubuntu 20.04 / Debian / CentOS),我可以给你更精确的命令。

0