温馨提示×

centos apache配置啥步骤

小樊
38
2026-10-03 00:05:16
栏目: 智能运维

下面给你一份**CentOS 上配置 Apache(httpd)**的常见步骤,适用于 CentOS 7 / 8 / Stream,从安装到基本站点配置。


一、安装 Apache

CentOS 7

yum install httpd -y

CentOS 8 / Stream

dnf install httpd -y

二、启动并设置开机自启

systemctl start httpd
systemctl enable httpd

查看状态:

systemctl status httpd

三、防火墙放行(重要)

开放 80 端口(HTTP)

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

如需 HTTPS

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

四、测试 Apache 是否正常工作

浏览器访问:

http://服务器IP

看到 Apache 测试页 说明成功。


五、网站目录结构说明

默认网站根目录:

/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>

    ErrorLog /var/log/httpd/example-error.log
    CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>

3️⃣ 检查配置并重启

httpd -t
systemctl restart httpd

七、常见补充配置

开启 .htaccess(伪静态)

AllowOverride All

绑定多域名

每个域名一个 conf 文件即可。

配置 HTTPS(可选)

  • 安装 mod_ssl
yum install mod_ssl -y
  • 使用 Let’s Encrypt(certbot)更方便

八、排错常用命令

journalctl -xe
tail -f /var/log/httpd/error_log
httpd -t

如果你有具体需求(比如 PHP、HTTPS、多站点、反向代理),可以直接说,我给你针对性配置。

0 踩