CentOS 上排查与修复 Apache 权限问题的实用步骤
一 快速定位问题
sudo systemctl status httpd、ss -tlnp | grep ':80\|:443'。若未运行,先启动:sudo systemctl start httpd。sudo apachectl configtest,修正报错后再继续。sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload。二 修复文件系统与目录权限
sudo chown -R apache:apache /var/www/your_site。sudo find /var/www/your_site -type d -exec chmod 755 {} \; 与 sudo find /var/www/your_site -type f -exec chmod 644 {} \;。三 校正 Apache 目录访问控制
Require all granted(Apache 2.4+)。示例:<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Order allow,deny 与 Allow from all 的组合,避免默认拒绝。AllowOverride 允许相应功能,且 .htaccess 文件本身权限正确。sudo apachectl configtest && sudo systemctl restart httpd。四 处理 SELinux 带来的权限限制
sestatus。临时切换为宽容模式用于排查:sudo setenforce 0(仅测试,不要长期关闭)。sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/your_site/uploads(/.*)?"sudo restorecon -Rv /var/wwwdisabled,应通过 semanage/restorecon 精确修正策略。五 常见场景与命令清单
Require all granted。sestatus,必要时 setenforce 0 验证是否为 SELinux 问题,若是则用 semanage/restorecon 修复。sudo systemctl restart httpd。sudo chown -R apache:apache /data/www。sudo find /data/www -type d -exec chmod 755 {} \; 与 sudo find /data/www -type f -exec chmod 644 {} \;。sudo semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?" && sudo restorecon -Rv /data/www。Require all granted。sudo systemctl status|restart httpd、ss -tlnp | grep ':80\|:443'sudo apachectl configtesttail -f /var/log/httpd/error_logsestatus、sudo setenforce 0、sudo semanage fcontext -a -t httpd_sys_content_t "/path" && sudo restorecon -Rv /pathsudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload