下面给你一个CentOS 下配置 Apache(httpd)的常用完整流程,从安装到常见配置,基本照着做就能跑起来。
CentOS 7 / 8 通用:
# CentOS 7
yum install -y httpd
# CentOS 8+/Stream
dnf install -y httpd
查看版本:
httpd -v
systemctl start httpd
systemctl enable httpd
查看状态:
systemctl status httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
查看状态:
getenforce
临时关闭(测试用):
setenforce 0
永久关闭(不推荐):
vi /etc/selinux/config
SELINUX=disabled
或者放行 httpd:
setsebool -P httpd_can_network_connect 1
网站根目录:/var/www/html
配置文件:/etc/httpd/conf/httpd.conf
测试页面:
echo "Hello Apache" > /var/www/html/index.html
浏览器访问服务器 IP:
http://服务器IP
mkdir -p /var/www/example.com
echo "example.com" > /var/www/example.com/index.html
vi /etc/httpd/conf.d/example.conf
内容示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example_error.log
CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
systemctl restart httpd
确认配置中:
AllowOverride All
测试 .htaccess:
vi /var/www/html/.htaccess
RewriteEngine On
RewriteRule ^test$ index.html
yum install -y certbot python3-certbot-apache
certbot --apache -d example.com -d www.example.com
证书自动续期:
certbot renew --dry-run
httpd -t # 检查配置语法
systemctl restart httpd
systemctl reload httpd
apachectl status
✅ 403 Forbidden
Require all granted✅ 页面打不开
✅ 多站点冲突
ServerName 唯一/etc/httpd/conf.d/如果你愿意,可以告诉我:
我可以直接给你定制配置示例。