下面给你一份CentOS 下 Apache(httpd)常见配置指南,从安装到常用配置,适用于 CentOS 7 / 8 / Stream(Systemd 体系)。
# CentOS 7/8
sudo yum install httpd -y
# 或 CentOS 8/Stream
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
/etc/httpd/
├── conf/httpd.conf # 主配置文件
├── conf.d/ # 额外配置文件(推荐放虚拟主机)
├── logs/ # 日志
/var/www/html/ # 默认网站根目录
常见需要改的地方:
ServerName www.example.com:80
DocumentRoot "/var/www/html"
修改后重启:
sudo systemctl restart httpd
sudo mkdir -p /var/www/example.com/html
sudo chown -R apache:apache /var/www/example.com
sudo vi /etc/httpd/conf.d/example.conf
示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/html
<Directory /var/www/example.com/html>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/example_error.log
CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>
重启 Apache:
sudo systemctl restart httpd
sudo yum install php php-cli php-common -y
sudo systemctl restart httpd
测试:
echo "<?php phpinfo(); ?>" > /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
sudo yum install certbot python3-certbot-apache -y
sudo certbot --apache
自动续期测试:
sudo certbot renew --dry-run
sudo setsebool -P httpd_read_user_content 1
sudo chcon -R -t httpd_sys_content_t /var/www
systemctl start httpd
systemctl stop httpd
systemctl restart httpd
systemctl status httpd
httpd -t # 检查配置文件语法
如果你有具体需求(比如:多站点 / 反向代理 / 重定向 / 绑定域名 / 内网环境),可以直接说,我可以给你针对性配置示例。