温馨提示×

Debian下PHP错误怎么看

小樊
42
2026-07-05 20:32:05
栏目: 编程语言

在 Debian 下查看 PHP 错误,常见有 几种方式,取决于你是用 PHP-FPM、Apache、CLI 还是 Nginx。下面按最常用场景给你一个完整说明。


一、先看 PHP 错误日志配置

1️⃣ 查看 php.ini 位置

php --ini

常见路径:

  • /etc/php/8.2/cli/php.ini
  • /etc/php/8.2/fpm/php.ini

注意:CLI 和 FPM 是两套 php.ini


2️⃣ 关键配置项(非常重要)

编辑对应 php.ini:

sudo nano /etc/php/8.2/fpm/php.ini

确认以下配置:

display_errors = Off        # 生产环境建议关闭
log_errors = On
error_reporting = E_ALL
error_log = /var/log/php/error.log

重启 PHP-FPM:

sudo systemctl restart php8.2-fpm

二、查看 PHP 错误日志(最常用)

✅ 方法 1:PHP-FPM / Nginx 环境

tail -f /var/log/php/error.log

如果文件不存在:

sudo mkdir -p /var/log/php
sudo touch /var/log/php/error.log
sudo chown www-data:www-data /var/log/php/error.log

✅ 方法 2:Apache + mod_php

错误通常在 Apache 日志中:

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

✅ 方法 3:PHP CLI(命令行)

php your_script.php 2>&1

或者查看 CLI 错误日志:

tail -f /var/log/php/error.log

三、临时在代码中打开错误显示(调试用)

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

⚠️ 不要在生产环境长期开启


四、Nginx 常见 PHP 错误排查

1️⃣ Nginx 错误

tail -f /var/log/nginx/error.log

2️⃣ Nginx + PHP-FPM 常见错误

  • 502 Bad Gateway → PHP-FPM 没启动
  • 404SCRIPT_FILENAME 配置错误

检查:

systemctl status php8.2-fpm

五、快速定位错误(推荐组合)

调试时建议同时开两个终端:

# 终端 1
tail -f /var/log/nginx/error.log

# 终端 2
tail -f /var/log/php/error.log

六、常见错误示例

错误 原因
Allowed memory size exhausted 内存不足
Maximum execution time 超时
Class not found autoload / composer
Permission denied 文件权限

七、如果你愿意,我可以帮你:

  • 精确定位 你当前的 PHP 错误
  • 分析 Nginx / Apache 配置
  • 判断是 PHP、权限、还是环境 问题

你可以直接贴:

  • 报错内容
  • 使用的环境(Debian 版本 + PHP 版本 + Nginx/Apache)

0