Ubuntu PHP日志常见错误代码及解决方法
Fatal error: Uncaught Error: Call to undefined function someFunction() in /path/to/script.php:10(调用未定义函数)、Fatal error: Class 'MyClass' not found in /path/to/script.php:15(类未找到)。composer require package/name);require_once 'functions.php';)或命名空间是否正确。Parse error: syntax error, unexpected '}' in /path/to/script.php:20(意外右花括号)、Parse error: syntax error, unexpected T_VARIABLE in /path/to/script.php:30(意外变量)。Warning: Division by zero in /path/to/script.php:8(除以零)、Warning: file_get_contents(/path/to/file.txt): failed to open stream: No such file or directory in /path/to/script.php:12(文件不存在)。if ($denominator != 0) { $result = $numerator / $denominator; });chmod 644 /path/to/file.txt);Notice: Undefined variable: someVariable in /path/to/script.php:5(未定义变量)、Notice: Undefined index: key in /path/to/script.php:10(数组索引不存在)。$someVariable = '';);isset()函数判断(如if (isset($array['key'])) { $value = $array['key']; });php.ini中设置error_reporting = E_ALL,display_errors = On,便于调试。Deprecated: Function session_register() is deprecated in /path/to/script.php:18(session_register已弃用)、Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /path/to/script.php:25(mysql扩展已弃用)。session_register()替代为$_SESSION超全局变量,mysql_connect()替代为mysqli_connect()或PDO);php.ini中memory_limit的设置,导致脚本终止。Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in /path/to/script.php:40(内存耗尽)。ini_set('memory_limit', '256M');(根据需求调整大小);php.ini文件(如/etc/php/8.0/apache2/php.ini),找到memory_limit行,修改为memory_limit = 256M,重启Web服务器(sudo systemctl restart apache2或sudo systemctl restart nginx);unset($variable))。www-data)无权访问脚本、目录或文件,导致无法读取/写入。Warning: fopen(/var/www/html/uploads/file.txt): failed to open stream: Permission denied in /path/to/script.php:15(无法打开文件)、Fatal error: Uncaught Exception: Directory /var/www/html/cache/ is not writable in /path/to/script.php:22(目录不可写)。sudo chown -R www-data:www-data /var/www/html;755(sudo chmod -R 755 /var/www/html),文件设为644(sudo chmod -R 644 /var/www/html);sudo chmod -R 775 /var/www/html/uploads(谨慎使用777,存在安全风险)。PHP Fatal error: Uncaught Error: Call to undefined function mb_strlen() in /path/to/script.php:10(mbstring扩展未安装)、PHP Warning: PHP Startup: Unable to load dynamic library 'gd.so' in /path/to/php.ini:50(gd扩展未加载)。sudo apt install php-mbstring(mbstring)、sudo apt install php-gd(gd);php.ini文件,取消对应扩展的注释(如;extension=gd改为extension=gd),重启Web服务器;phpinfo.php文件(<?php phpinfo(); ?>),访问该文件查看扩展列表。/var/log/apache2/error.log或/var/log/php/php.log),定位具体错误;php.ini设置display_errors = On,error_reporting = E_ALL,重启Web服务器;mod_php模块是否启用(sudo a2enmod php8.0),Nginx的PHP-FPM配置是否正确;