如何让 PHP 显示错误而不是给我 500 Internal Server Error

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2687730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:20:51  来源:igfitidea点击:

How can I make PHP display the error instead of giving me 500 Internal Server Error

apachephp

提问by Rob

This has never happened before. Usually it displays the error, but now it just gives me a 500 internal server error. Of course before, when it displayed the error, it was different servers. Now I'm on a new server (I have full root, so if I need to configure it somewhere in the php.ini, I can.) Or perhaps its something with Apache?

这是以前从未发生过的。通常它会显示错误,但现在它只给我一个 500 内部服务器错误。当然之前,当它显示错误时,是不同的服务器。现在我在一个新服务器上(我有完整的 root,所以如果我需要在 php.ini 中的某个地方配置它,我可以。)或者它可能是 Apache 的东西?

I've been putting up with it by just transferring the file to my other server and running it there to find the error, but that's become too tedious. Is there a way to fix this?

我一直在忍受它,只是将文件传输到我的另一台服务器并在那里运行它以查找错误,但这变得太乏味了。有没有办法来解决这个问题?

回答by awgy

Check the error_reporting, display_errorsand display_startup_errorssettings in your php.inifile. They should be set to E_ALLand "On"respectively (though you should not use display_errorson a production server, so disable this and use log_errorsinstead if/when you deploy it). You can also change these settings (except display_startup_errors) at the very beginning of your script to set them at runtime (though you may not catch all errors this way):

检查文件中的error_reporting,display_errorsdisplay_startup_errors设置php.ini。它们应该分别设置为E_ALL"On"(虽然你不应该display_errors在生产服务器上使用,所以禁用它并log_errors在部署它时使用)。您还可以display_startup_errors在脚本的最开始更改这些设置(除了)以在运行时设置它们(尽管您可能无法通过这种方式捕获所有错误):

error_reporting(E_ALL);
ini_set('display_errors', 'On');

After that, restart server.

之后,重新启动服务器。

回答by dtbarne

It's worth noting that if your error is due to .htaccess, for example a missing rewrite_module, you'll still see the 500 internal server error.

值得注意的是,如果您的错误是由于 .htaccess 引起的,例如缺少 rewrite_module,您仍然会看到 500 内部服务器错误。

回答by Aaron

Use "php -l <filename>" (that's an 'L') from the command line to output the syntax error that could be causing PHP to throw the status 500 error. It'll output something like:

在命令行中使用“php -l <​​filename>”(这是一个“L”)来输出可能导致 PHP 抛出状态 500 错误的语法错误。它会输出如下内容:

PHP Parse error: syntax error, unexpected '}' in <filename> on line 18

PHP 解析错误:语法错误,第 18 行 <filename> 中出现意外的 '}'

回答by Max

Be careful to check if

小心检查是否

display_errors

or

或者

error_reporting

is active (not a comment) somewhere else in the ini file.

在 ini 文件的其他地方处于活动状态(不是评论)。

My development server refused to display errors after upgrade to Kubuntu 16.04 - I had checked php.ini numerous times ... turned out that there was a diplay_errors = off; about 100 lines below my

升级到 Kubuntu 16.04 后,我的开发服务器拒绝显示错误 - 我已经多次检查 php.ini ......结果是 diplay_errors = off; 在我的下方大约 100 行

display_errors = on;

So remember the last one counts!

所以请记住最后一项很重要!

回答by von verletzt

Try not to go

尽量不要去

MAMP > conf > [your PHP version] > php.ini

but

MAMP > bin > php > [your PHP version] > conf > php.ini

and change it there, it worked for me...

并在那里改变它,它对我有用......

回答by Hao Nguyen

Enabling error displaying from PHP code doesn't work out for me. In my case, using NGINX and PHP-FMP, I track the log file using grep. For instance, I know the file name mycode.phpcauses the error 500, but don't know which line. From the console, I use this:

从 PHP 代码启用错误显示对我来说不起作用。就我而言,使用 NGINX 和 PHP-FMP,我使用grep跟踪日志文件。例如,我知道文件名mycode.php导致错误 500,但不知道哪一行。从控制台,我使用这个:

/var/log/php-fpm# cat www-error.log | grep mycode.php

And I have the output:

我有输出:

[04-Apr-2016 06:58:27] PHP Parse error:  syntax error, unexpected ';' in /var/www/html/system/mycode.php on line 1458

This helps me find the line where I have the typo.

这有助于我找到有错字的那一行。

回答by WoodrowShigeru

If all else fails try moving (i.e. in bash) all files and directories "away" and adding them back one by one.

如果所有其他方法都失败,请尝试移动(即在 bash 中)所有文件和目录“离开”并将它们一个一个地添加回来。

I just found out that way that my .htaccess file was referencing a non-existant .htpasswd file. (#silly)

我刚刚发现我的 .htaccess 文件引用了一个不存在的 .htpasswd 文件。(#愚蠢的)