在 Debian 上调试 PHP,通常分为环境检查、错误日志、运行时调试、性能分析几个层面。下面按“从入门到进阶”的方式说明。
php -v
which php
如果没装:
sudo apt update
sudo apt install php php-cli
php --ini
常见路径:
/etc/php/*/cli/php.ini/etc/php/*/fpm/php.inidisplay_errors = On
error_reporting = E_ALL
log_errors = On
CLI 调试:
php -d display_errors=1 script.php
sudo systemctl restart apache2sudo systemctl restart php*-fpm
sudo systemctl restart nginx
error_log = /var/log/php_errors.log
创建并授权:
sudo touch /var/log/php_errors.log
sudo chown www-data:www-data /var/log/php_errors.log
查看:
tail -f /var/log/php_errors.log
/var/log/apache2/error.log/var/log/nginx/error.logphp index.php
var_dump($var);
print_r($var);
exit;
php -l file.php
sudo apt install php-xdebug
确认:
php -m | grep xdebug
/etc/php/*/cli/conf.d/20-xdebug.ini
zend_extension=xdebug
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
php -m
xdebug.mode=profile
生成 cachegrind 文件,用:
sudo apt install kcachegrind
memory_limit = 256M
max_execution_time = 30
| 问题 | 排查方式 |
|---|---|
| 白屏 | 看 error_log |
| 502 | 看 php-fpm 日志 |
| 权限问题 | `ps aux |
| 版本不对 | update-alternatives --list php |
如果你能说明:
我可以给你更精确的调试步骤。