温馨提示×

怎么调试debian apache

小樊
40
2026-09-21 18:05:01
栏目: 智能运维

下面按常见调试场景给你一个比较系统的 Debian + Apache 调试指南,从“看日志 → 查配置 → 排错”一步步来。


一、基础环境确认

1. 查看 Apache 是否安装

dpkg -l | grep apache2

2. 查看 Apache 状态

systemctl status apache2

启动 / 停止 / 重启:

sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2

二、最重要:看日志(90% 问题在这里)

1. 错误日志(最核心)

sudo tail -f /var/log/apache2/error.log

2. 访问日志

sudo tail -f /var/log/apache2/access.log

如果站点有独立日志(虚拟主机),路径通常在: /var/log/apache2/你的站点名-error.log


三、配置检查(非常关键)

1. 检查配置文件语法

sudo apache2ctl configtest

✅ 输出 Syntax OK 才正常

2. 查看加载的模块

apache2ctl -M

3. 虚拟主机是否启用

ls /etc/apache2/sites-enabled/

启用站点:

sudo a2ensite example.conf
sudo systemctl reload apache2

四、常见错误及排查

❌ 403 Forbidden

可能原因:

  • 目录权限不对
  • DocumentRoot 不存在
  • 没有 Directory 权限

检查:

ls -ld /var/www/html

示例正确配置:

<Directory /var/www/html>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

❌ 404 Not Found

  • 文件不在 DocumentRoot
  • URL 写错
  • Rewrite 规则问题

❌ 500 Internal Server Error

看 error.log,常见:

  • .htaccess 语法错误
  • PHP 报错
  • 权限问题

❌ 端口被占用

sudo ss -tulnp | grep :80

五、开启调试模式(排错用)

1. 提高日志级别

编辑:

sudo nano /etc/apache2/apache2.conf

修改:

LogLevel warn
# 改为
LogLevel debug

然后:

sudo systemctl restart apache2

六、PHP 相关调试(如用 PHP)

sudo apt install php libapache2-mod-php

测试:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

浏览器访问:

http://服务器IP/info.php

七、防火墙 & SELinux(Debian 一般没 SELinux)

sudo ufw allow 80
sudo ufw allow 443

八、快速排错清单(建议收藏)

✅ Apache 是否在跑
✅ error.log 说什么
✅ configtest 是否 OK
✅ 虚拟主机是否启用
✅ 文件权限是否正确
✅ 端口是否通


如果你愿意,可以直接贴一段 error.log 或你的虚拟主机配置,我可以帮你精确分析。

0 踩