PHP没有显示错误-内部服务器错误(500)


PHP not displaying errors - Internal Server Error (500)

我已经在亚马逊AWS上用*Apache2/MySQL/PHP5重新安装了Ubuntu Server 12.04 LTS。当我运行一个PHP脚本,它遇到错误时,我没有看到任何来自PHP的错误报告,我看到的只是

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

我已经检查了我的/etc/php5/apache2/php.ini文件,据我所知,应该设置错误报告。文件的内容(关于错误)是:

;    display_errors
;      Default Value: On
;      Development Value: On
;      Production Value: Off
;    display_startup_errors
;      Default Value: Off
;      Development Value: On
;      Production Value: Off
;    error_reporting
;      Default Value: E_ALL & ~E_NOTICE
;      Development Value: E_ALL | E_STRICT
;      Production Value: E_ALL & ~E_DEPRECATED

有人能提出建议吗?如果我尝试类似$obj = new ObjectDoesntExist;的东西,它不会告诉我Fatal error: Class 'ObjectDoesntExist',它会给我一个服务器500错误。

有什么建议吗?

*我安装的模块有:mysql-server mysql-client apache2 php5 libapache2-mod-php5 phpmyadmin。除此之外,它是Ubuntu Server 12.04 LTS 的完全基础安装

编辑:如果我在脚本开始时使用ini_set('display_errors', '1');,它会正常显示错误,但我如何在整个站点范围内启用此功能?

粘贴的php.ini片段每行都有一个分号(;字符)。分号是php.ini中注释的开头,因此不会使用分号后面一行中的所有内容。请尝试手动将display_errors设置为on,将error_reporting设置为E_ALL,或者删除适当的分号来解决此问题。

此外,检查您的apache的错误日志,php可能会在那里记录它的错误。

请使用sudo gedit/etc/php5/apache2/php.ini和Production Value编辑php.ini文件,也可以使用ini_set('display_errors','1');在php文件的顶部

我遇到了同样的问题,但与php.ini无关若您有同样的错误,在您开始剪切服务器之前,首先检查您的代码。错误可以是一个缺失的})或;或者类似的东西。这只是参考先做什么。

// Disable displaying errors, enable logging
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/php_error.log');
// Handle errors and exceptions
set_error_handler(function($errno, $errstr, $errfile, $errline) {
    error_log(sprintf("[%s] %s in %s on line %d", date('Y-m-d H:i:s'), $errstr, $errfile, $errline));
    http_response_code(500);
    exit();
});
set_exception_handler(function($exception) {
    error_log(sprintf("[%s] Uncaught exception '%s' with message '%s' in %s:%d'nStack trace:'n%s'n", 
        date('Y-m-d H:i:s'), 
        get_class($exception), 
        $exception->getMessage(), 
        $exception->getFile(), 
        $exception->getLine(), 
        $exception->getTraceAsString()
    ));
    http_response_code(500);
    exit();
});
  • set_error_handler用于处理PHP错误,set_exception_handler则用于处理异常。当发生错误或异常时,会使用error_log函数将消息记录到错误日志文件中,并将响应代码设置为500以指示服务器错误。最后,尝试使用未定义的变量会触发一个示例错误