温馨提示×

Apache如何在CentOS启动

小樊
52
2026-08-18 21:10:49
栏目: 智能运维

CentOS 上启动 Apache(httpd),常见操作如下(适用于 CentOS 7/8/Stream,也兼容部分旧版本说明)。


一、安装 Apache(如未安装)

sudo yum install httpd -y

CentOS 8 / Stream 可使用:

sudo dnf install httpd -y

二、启动 Apache 服务

1️⃣ 启动 Apache

sudo systemctl start httpd

2️⃣ 设置开机自启

sudo systemctl enable httpd

三、查看 Apache 状态

sudo systemctl status httpd

正常状态应显示:

Active: active (running)

四、防火墙放行 HTTP/HTTPS(非常重要)

1️⃣ 放行端口

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

2️⃣ 验证

sudo firewall-cmd --list-services

五、测试 Apache 是否启动成功

浏览器访问:

http://服务器IP

看到 Apache Test Page 即成功。


六、常用管理命令

sudo systemctl stop httpd      # 停止
sudo systemctl restart httpd   # 重启
sudo systemctl reload httpd    # 重新加载配置

七、配置文件位置(常用)

项目 路径
主配置文件 /etc/httpd/conf/httpd.conf
虚拟主机 /etc/httpd/conf.d/
网站根目录 /var/www/html
日志 /var/log/httpd/

八、常见问题

❌ Apache 启动失败

查看错误日志:

sudo journalctl -xe
sudo tail -n 50 /var/log/httpd/error_log

❌ 端口 80 被占用

sudo netstat -tulnp | grep :80

如果你使用的是 CentOS 6 或需要 虚拟主机 / HTTPS / PHP 环境,可以告诉我,我可以给你更详细的配置示例。

0