下面按从零开始的思路,给你一份在 CentOS(以 7/8/Stream 为主) 上配置 Apache(httpd) 的实用指南,涵盖安装、启动、目录结构、虚拟主机、防火墙和常见配置。
sudo yum install httpd -y
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 --reload
如需 HTTPS:
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
浏览器访问:
http://服务器IP
看到 Apache Test Page 即成功。
| 路径 | 说明 |
|---|---|
/etc/httpd/ |
主配置目录 |
/etc/httpd/conf/httpd.conf |
主配置文件 |
/etc/httpd/conf.d/ |
额外配置文件 |
/var/www/html/ |
默认网站根目录 |
/var/log/httpd/ |
日志目录 |
编辑主配置:
sudo vi /etc/httpd/conf/httpd.conf
修改:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
重启:
sudo systemctl restart httpd
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R apache:apache /var/www/example.com
sudo vi /etc/httpd/conf.d/example.com.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
sudo systemctl restart httpd
在目录配置中确保:
AllowOverride All
如果网页无法访问:
sudo setsebool -P httpd_can_network_connect 1
sudo chcon -R -t httpd_sys_content_t /var/www
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
httpd -t # 检查配置语法
如果你需要:
可以直接告诉我你的具体需求。