温馨提示×

apache centos咋配置

小樊
47
2026-10-03 00:07:19
栏目: 智能运维

你问的“apache centos 咋配置”范围比较广,我按最常见场景给你一个CentOS 7/8 + Apache(httpd)基础配置教程,从安装到简单站点配置,一步一步来。


一、安装 Apache(httpd)

CentOS 7

yum install -y httpd

CentOS 8

dnf install -y httpd

二、启动并设置开机自启

systemctl start httpd
systemctl enable httpd

查看状态:

systemctl status httpd

三、防火墙放行 80 / 443 端口

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

如果是云服务器,还要在安全组放行 80/443。


四、测试 Apache 是否成功

浏览器访问:

http://服务器IP

看到 Apache Test Page 就成功了。


五、基本目录结构说明

路径 作用
/var/www/html 默认网站根目录
/etc/httpd/conf/httpd.conf 主配置文件
/etc/httpd/conf.d/ 额外配置文件(推荐放虚拟主机)

六、配置一个简单网站(虚拟主机)

1️⃣ 创建网站目录

mkdir -p /var/www/example.com
echo "Hello Apache" > /var/www/example.com/index.html

2️⃣ 创建配置文件

vi /etc/httpd/conf.d/example.conf

内容示例:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com

    <Directory /var/www/example.com>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

3️⃣ 重启 Apache

systemctl restart httpd

七、开启 PHP(可选)

yum install -y php php-cli
systemctl restart httpd

测试:

echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

访问:

http://IP/phpinfo.php

八、常见排错

✅ 403 错误

  • 目录权限不对
chown -R apache:apache /var/www/example.com
chmod -R 755 /var/www/example.com

✅ 无法访问

  • 防火墙 / 安全组
  • Apache 没启动

九、你还可以配置什么?

  • HTTPS(Let’s Encrypt)
  • 反向代理
  • 多域名
  • 伪静态(Rewrite)
  • 访问控制(IP 限制)

如果你能告诉我: ✅ CentOS 版本
✅ 想干嘛(建站 / 反向代理 / HTTPS / PHP)

我可以给你更精确的一套配置。

0 踩