在 CentOS 上通常是 httpd 而不是 apache2(apache2 是 Debian/Ubuntu 的叫法)。如果你在 CentOS 上执行 systemctl start apache2 或 service apache2 start,大概率会直接失败,因为服务名不存在。
下面分情况说明:
systemctl start apache2
CentOS 会提示:
Failed to start apache2.service: Unit apache2.service not found.
systemctl start httpd
或者旧版本:
service httpd start
检查是否安装:
rpm -qa | grep httpd
没装就装:
yum install httpd -y # CentOS 7
dnf install httpd -y # CentOS 8/Stream
查看:
ss -lntp | grep :80
常见冲突:nginx、tomcat、其他 httpd
解决:
/etc/httpd/conf/httpd.conf 的 Listen 80测试配置:
httpd -t
如果有错误,会直接提示哪一行。
查看日志:
tail /var/log/audit/audit.log
临时关闭测试:
setenforce 0
长期解决要写 SELinux 策略,而不是一直关。
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
检查:
ls -ld /var/www/html
systemctl status httpd
journalctl -xe
CentOS 没有 apache2,只有 httpd;启动不了先看服务名、端口、配置和 SELinux。
如果你是 具体报错信息(比如 Job for httpd.service failed),把报错贴出来,我可以精准帮你定位。