温馨提示×

怎么安装CentOS Apache

小樊
45
2026-08-03 22:15:37
栏目: 智能运维

CentOS 上安装 Apache(httpd) 非常简单,下面按常见版本给你说明。


一、CentOS 7 安装 Apache

1. 更新系统(可选但推荐)

sudo yum update -y

2. 安装 Apache

sudo yum install httpd -y

3. 启动 Apache

sudo systemctl start httpd

4. 设置开机自启

sudo systemctl enable httpd

5. 检查运行状态

sudo systemctl status httpd

6. 防火墙放行 HTTP/HTTPS

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

7. 测试

浏览器访问:

http://服务器IP

看到 Apache Test Page 即成功。


二、CentOS 8 / Rocky / AlmaLinux

CentOS 8 以后使用 dnf

sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd

防火墙设置同上。


三、Apache 常见目录说明

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

四、常见问题

1. 无法访问?

  • 检查防火墙:firewall-cmd --list-all
  • 云服务器需放行 安全组 80/443
  • 检查 Apache 是否运行:systemctl status httpd

2. 修改端口(如 8080)

编辑:

vim /etc/httpd/conf/httpd.conf

修改:

Listen 8080

然后重启:

sudo systemctl restart httpd

如果你需要:

  • ✅ 配置 虚拟主机
  • ✅ 安装 PHP + Apache
  • ✅ 配置 HTTPS(SSL)
  • ✅ 多站点部署

可以直接告诉我你的 CentOS 版本和使用场景,我一步步帮你配置。

0