在 CentOS 里,Apache 的软件包名通常不是 apache2(那是 Debian/Ubuntu 的叫法),而是 httpd。下面分情况说明。
# 启动 Apache
service httpd start
# 设置开机自启
chkconfig httpd on
# 查看状态
service httpd status
# 启动 Apache
systemctl start httpd
# 设置开机自启
systemctl enable httpd
# 查看运行状态
systemctl status httpd
# 重启 Apache
systemctl restart httpd
# CentOS 7/8
yum install httpd -y
# 或
dnf install httpd -y
安装后再执行上面的 systemctl 命令。
如果开了 firewalld:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
如果是 iptables(老系统):
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
service iptables save
浏览器访问服务器 IP:
http://服务器IP
看到 Apache 测试页 就说明成功了。
如果你用的是 CentOS 特定版本 或 面板(如宝塔),可以告诉我,我给你更精确的命令。