在 CentOS 上配置 Apache(httpd)一般分为 安装、启动、放行防火墙、配置虚拟主机 等几个步骤。下面以 CentOS 7 / 8 / Stream 为例说明(systemd 系统)。
sudo yum update -y
sudo yum install httpd -y
CentOS 中 Apache 的包名是 httpd
sudo systemctl start httpd
sudo systemctl enable httpd
检查状态:
sudo systemctl status httpd
看到 active (running) 表示成功。
sudo firewall-cmd --state
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache HTTP Server Test Page 即成功。
| 目录 | 说明 |
|---|---|
/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/example
<Directory /data/www/example>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example_error.log
CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
sudo mkdir -p /data/www/example
sudo chown -R apache:apache /data/www/example
sudo systemctl restart httpd
getenforce
临时关闭测试:
sudo setenforce 0
sudo tail -f /var/log/httpd/error_log
如果使用 CentOS 8 / Stream,推荐使用:
sudo dnf install httpd -y
其余步骤相同。
如果你需要:
✅ 配置 HTTPS(SSL)
✅ 部署 PHP / Python / Java 项目
✅ 配置多站点
✅ 配合 Nginx 使用
可以告诉我你的具体需求,我可以一步步帮你配置。