在 Debian 下调试 PHP 错误,通常从开启错误显示、查看日志、使用调试工具几个方面入手。下面按实用顺序说明。
先确认用的是什么 PHP:
php -v
which php
编辑对应 php.ini(常见位置):
# CLI
/etc/php/8.2/cli/php.ini
# Apache
/etc/php/8.2/apache2/php.ini
# FPM(Nginx)
/etc/php/8.2/fpm/php.ini
修改或确认以下配置:
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
log_errors = On
error_log = /var/log/php/php_errors.log
修改后重启服务:
# Apache
systemctl restart apache2
# Nginx + FPM
systemctl restart php8.2-fpm
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
tail -f /var/log/php/php_errors.log
如果没有该文件:
mkdir -p /var/log/php
chown www-data:www-data /var/log/php
# Apache
tail -f /var/log/apache2/error.log
# Nginx
tail -f /var/log/nginx/error.log
通常是:
解决:
php -l file.php # 检查语法
查看:
tail -f /var/log/apache2/error.log
或
journalctl -u php8.2-fpm
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html
apt install php-xdebug
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
重启 PHP:
systemctl restart php8.2-fpm
php -d display_errors=1 script.php
php -d error_reporting=E_ALL script.php
display_errors = Offlog_errors = On如果你能说明:
我可以给你更精准的调试方案。