在 CentOS 上配置 Apache(httpd),下面给你一套从安装到常见配置的完整流程(适用于 CentOS 7 / 8 / Stream)。
sudo yum update -y
CentOS 8 / Stream 可用
dnf update -y
sudo yum install httpd -y
或(CentOS 8+)
sudo dnf install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
查看状态:
systemctl status httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache 测试页 说明成功 ✅
| 路径 | 作用 |
|---|---|
/etc/httpd/ |
主配置目录 |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
自定义配置(推荐) |
/var/www/html/ |
默认网站根目录 |
/var/log/httpd/ |
日志目录 |
sudo mkdir -p /data/www
sudo chown -R apache:apache /data/www
编辑:
sudo vi /etc/httpd/conf/httpd.conf
找到:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
改为:
DocumentRoot "/data/www"
<Directory "/data/www">
AllowOverride All
Require all granted
</Directory>
sudo systemctl restart httpd
sudo vi /etc/httpd/conf.d/example.conf
示例:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /data/www
<Directory /data/www>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example-error.log
CustomLog /var/log/httpd/example-access.log combined
</VirtualHost>
sudo systemctl restart httpd
sudo vi /etc/httpd/conf.modules.d/00-base.conf
确保有:
LoadModule rewrite_module modules/mod_rewrite.so
或在 .htaccess 中:
RewriteEngine On
RewriteRule ^test$ index.php
✅ 检查:
sudo setenforce 0 # 临时关闭(测试用)
永久关闭(不推荐生产):
sudo vi /etc/selinux/config
SELINUX=disabled
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl status httpd
httpd -t # 检查配置语法
如果你需要:
告诉我你的 CentOS 版本 + 使用场景,我可以直接给你定制配置。