centos

CentOS Apache2出现404怎么办

小樊
42
2025-08-12 16:56:06
栏目: 智能运维

CentOS Apache2出现404错误,可按以下步骤排查解决:

  1. 检查服务状态:确保Apache服务已启动,使用命令 sudo systemctl status httpd,未启动则执行 sudo systemctl start httpd
  2. 验证配置文件
    • 检查 /etc/httpd/conf/httpd.conf 或虚拟主机配置文件(如 /etc/httpd/conf.d/your-site.conf)中的 DocumentRoot 路径是否正确,指向网站实际目录。
    • 使用 sudo apachectl configtest 检查配置语法是否有误。
  3. 确认文件权限:确保Apache用户(通常为apache)对网站目录及文件有访问权限,执行:
    sudo chown -R apache:apache /var/www/html  
    sudo chmod -R 755 /var/www/html  
    ```。  
    
  4. 排查SELinux:若启用SELinux,可能阻止访问,可临时禁用测试:
    sudo setenforce 0  
    
    若问题解决,需调整SELinux策略或永久禁用(不推荐)。
  5. 检查防火墙:确保防火墙允许HTTP(端口80)和HTTPS(端口443)流量,执行:
    sudo firewall-cmd --permanent --add-service=http  
    sudo firewall-cmd --permanent --add-service=https  
    sudo firewall-cmd --reload  
    ```。  
    
  6. 查看错误日志:通过 sudo tail -f /var/log/httpd/error_log 获取详细错误信息,定位具体问题(如文件不存在、权限不足等)。
  7. 验证模块与重写规则:若使用.htaccess或URL重写,确保mod_rewrite模块已启用,且配置正确(如AllowOverride All)。

完成修改后,重启Apache服务:sudo systemctl restart httpd。若问题仍未解决,根据错误日志进一步排查。

0
看了该问题的人还看了