Debian系统下PHP日志文件的查看位置及方法
若使用Apache作为Web服务器且通过mod_php模块运行PHP,PHP错误日志默认整合到Apache的全局错误日志中,路径为:
/var/log/apache2/error.log
查看实时日志的命令:
sudo tail -f /var/log/apache2/error.log
若使用Nginx作为Web服务器并通过PHP-FPM(FastCGI进程管理器)处理PHP请求,PHP错误日志通常由PHP-FPM单独记录,路径为:
/var/log/php-fpm.log(部分系统可能按版本命名,如/var/log/php7.4-fpm.log)
查看实时日志的命令:
sudo tail -f /var/log/php-fpm.log
PHP-FPM自身的错误日志位置可通过其配置文件调整,默认路径为:
/etc/php/{version}/fpm/php-fpm.conf({version}为PHP版本,如7.4、8.1)/etc/php/{version}/fpm/pool.d/www.conferror_log参数确认具体路径,例如:error_log = /var/log/php-fpm/error.log
修改后需重启PHP-FPM服务使配置生效:
sudo systemctl restart php{version}-fpm
若需将PHP错误记录到自定义文件,可在脚本中通过error_log()函数设置,例如:
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/custom_php_errors.log');
error_log("This is a custom PHP error message.");
查看自定义日志的命令:
sudo tail -f /var/log/custom_php_errors.log
/var/log/apache2/error.log)需管理员权限(使用sudo)。logrotate工具管理日志轮转,避免日志文件过大。自定义日志可通过编辑/etc/logrotate.d/下的对应配置文件(如php-fpm)调整轮转策略。php7.4-fpm.log),需根据实际安装的PHP版本调整路径。