温馨提示×

centos怎么弄apache配

小樊
45
2026-10-11 20:00:09
栏目: 智能运维

下面给你一份 CentOS 上配置 Apache(httpd) 的实用步骤,适用于 CentOS 7 / 8 / Stream(差别我会标出来)。


一、安装 Apache

CentOS 7

yum install -y httpd

CentOS 8 / Stream

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

如果没开防火墙可忽略。


四、测试访问

浏览器访问:

http://服务器IP

看到 Apache Test Page 即成功。


五、网站目录结构

默认网站根目录:

/var/www/html

放一个测试页:

echo "Hello Apache" > /var/www/html/index.html

六、配置虚拟主机(推荐)

1. 新建站点目录

mkdir -p /var/www/example.com
echo "example.com" > /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

八、常用命令

# 检查配置语法
httpd -t

# 重启
systemctl restart httpd

# 查看错误日志
tail -f /var/log/httpd/error_log

如果你是要:

  • 配 HTTPS(SSL)
  • 配 多域名
  • 配 WordPress
  • 用 CentOS 6(老系统)

可以直接告诉我,我按你的场景写。

0 踩