温馨提示×

apache centos怎配置

小樊
38
2026-10-11 19:59:09
栏目: 智能运维

你这个问题比较宽泛,我分几种常见场景给你讲,都是 CentOS + Apache(httpd) 的实用配置。


一、CentOS 安装 Apache

CentOS 7

yum install httpd -y
systemctl start httpd
systemctl enable httpd

CentOS 8 / Stream

dnf install httpd -y
systemctl start httpd
systemctl enable httpd

防火墙放行:

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

二、基本目录结构

  • 网站根目录:/var/www/html
  • 主配置文件:/etc/httpd/conf/httpd.conf
  • 虚拟主机配置目录:/etc/httpd/conf.d/

三、配置虚拟主机(多网站)

例如配置 www.example.com

mkdir -p /var/www/example
echo "hello example" > /var/www/example/index.html

新建配置文件:

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

内容:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/example
    <Directory /var/www/example>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

重启 Apache:

systemctl restart httpd

四、配置 HTTPS(SSL)

1. 安装模块

yum install mod_ssl -y

2. 使用证书(假设已有 crt 和 key)

vi /etc/httpd/conf.d/ssl.conf

关键配置:

SSLCertificateFile /path/to/cert.crt
SSLCertificateKeyFile /path/to/private.key

或虚拟主机方式:

<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/example
    SSLEngine on
    SSLCertificateFile /path/to/cert.crt
    SSLCertificateKeyFile /path/to/private.key
</VirtualHost>

五、常见优化配置

开启 rewrite

AllowOverride All

禁止目录浏览

Options -Indexes

设置默认首页

DirectoryIndex index.php index.html

六、排错命令

systemctl status httpd
journalctl -xe
httpd -t      # 检查配置语法

如果你是指:

  • ✅ CentOS 几?
  • ✅ Apache 做什么用(PHP / 反向代理 / 多域名)?
  • ✅ 是否用宝塔 / 手动配置?

告诉我,我可以给你更精准的一步到位配置。

0 踩