在 CentOS 里,Apache 的软件包名不是 apache2(那是 Ubuntu/Debian 的叫法),而是 httpd。下面是在 CentOS 上启动 Apache 的正确方法。
rpm -q httpd
如果没有安装,先安装:
sudo yum install httpd -y
# CentOS 8 及以上
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo systemctl status httpd
看到 active (running) 说明启动成功。
如果无法访问网页,通常是防火墙没开:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
(如需 HTTPS)
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache Test Page 即成功。
| 功能 | 命令 |
|---|---|
| 启动 | systemctl start httpd |
| 停止 | systemctl stop httpd |
| 重启 | systemctl restart httpd |
| 状态 | systemctl status httpd |
| 开机自启 | systemctl enable httpd |
如果你是从 Ubuntu 迁移到 CentOS,或者想用 apache2 命令,我也可以帮你对比说明。