下面按常见使用场景,给你一份 CentOS 下 Apache(httpd)配置指南,从安装到常见配置,适合新手和生产环境参考。
yum install httpd -y
systemctl start httpd
systemctl enable httpd
dnf install httpd -y
systemctl start httpd
systemctl enable httpd
验证:
systemctl status httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
| 路径 | 作用 |
|---|---|
/etc/httpd/ |
主配置目录 |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
子配置文件(推荐放站点配置) |
/var/www/html/ |
默认网站根目录 |
vi /etc/httpd/conf/httpd.conf
常见参数:
ServerName www.example.com
DocumentRoot "/var/www/html"
echo "Hello Apache" > /var/www/html/index.html
重启:
systemctl restart httpd
vi /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
ServerName www.a.com
DocumentRoot /var/www/a
<Directory /var/www/a>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.b.com
DocumentRoot /var/www/b
<Directory /var/www/b>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
创建目录:
mkdir -p /var/www/a /var/www/b
echo "A Site" > /var/www/a/index.html
echo "B Site" > /var/www/b/index.html
重启:
systemctl restart httpd
vi /etc/httpd/conf.modules.d/00-base.conf
确保:
LoadModule rewrite_module modules/mod_rewrite.so
.htaccess 示例:
RewriteEngine On
RewriteRule ^test$ index.php [L]
yum install mod_ssl openssl -y
vi /etc/httpd/conf.d/ssl.conf
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /path/cert.pem
SSLCertificateKeyFile /path/privkey.pem
SSLCertificateChainFile /path/chain.pem
</VirtualHost>
重启:
systemctl restart httpd
# 查看错误日志
tail -f /var/log/httpd/error_log
# 检查配置语法
httpd -t
# 查看端口
ss -lntp | grep httpd
chown -R apache:apache /var/www
chmod -R 755 /var/www
如果是 SELinux 开启:
setsebool -P httpd_can_network_connect 1
restorecon -Rv /var/www
✅ 安装
✅ 防火墙
✅ conf.d 配置虚拟主机
✅ 日志排查
✅ HTTPS
如果你告诉我:
我可以直接给你一套可用配置文件。