温馨提示×

apache如何在centos自启动

小樊
47
2026-09-02 22:28:18
栏目: 智能运维

在 CentOS 上让 Apache(httpd) 开机自启动,取决于你使用的 CentOS 版本(CentOS 6 及以前用 service/chkconfig,CentOS 7 及以后用 systemd)。


一、CentOS 7 / 8 / Stream(推荐,systemd)

1. 启动 Apache

systemctl start httpd

2. 设置开机自启动

systemctl enable httpd

3. 验证是否开机自启

systemctl is-enabled httpd

输出应为:

enabled

4. 常用命令

systemctl status httpd     # 查看状态
systemctl restart httpd    # 重启
systemctl stop httpd       # 停止

二、CentOS 6 及更早版本(SysVinit)

1. 启动 Apache

service httpd start

2. 设置开机自启动

chkconfig httpd on

3. 验证

chkconfig --list httpd

应看到:

httpd           0:off   1:off   2:on   3:on   4:on   5:on   6:off

三、常见问题排查

1. Apache 未安装

yum install httpd -y

2. 防火墙未放行 80 端口

firewall-cmd --permanent --add-service=http
firewall-cmd --reload

3. SELinux 导致无法访问

可临时测试:

setenforce 0

(生产环境建议正确配置 SELinux 策略)


如果你能提供 CentOS 具体版本号,我可以给你更精确的操作步骤。

0