How to display PHP errors when using Lighttpd and Fast-CGI?

T. Brian Jones picture T. Brian Jones · Jan 23, 2013 · Viewed 8.8k times · Source

I'm running a Lighttpd webserver using FastCGI and the webserver does not output PHP Parse Errors.

My php.ini file has the following settings:

error_reporting = E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = On
html_errors = On

I enable error output for development in my PHP scripts like this ( with redundancy for other environments ):

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

Most errors output fine. Parse Errors do not. Below is example code that throws a parse error. The error is not outputted by the Lighttpd webserver but is when executed from the command line because it's not using FastCGI. ( notice the missing concatenation operator ):

<?php echo 'foo' 'bar'; ?>

I've discovered that if I set display_errors = On in php.ini then parse errors output correctly with FastCGI and Lighttpd, but then I cannot turn them off within my PHP scripts using ini_set( 'display_errors', 0 ).

I'd like to be able to display these within my application when developing it, and be able to turn them off for production without changing php.ini configurations. Is there no way to do this within my PHP application when using FastCGI?

Answer

Jake W. picture Jake W. · Oct 18, 2013

The reason errors aren't being displayed is because when a fatal error is present in the code (such as your example), the code is not executed at all, and hence the display_errors value is never set.

From php.net (http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors):

Although display_errors may be set at runtime (with ini_set()), it won't have any affect if the script has fatal errors. This is because the desired runtime action does not get executed.

A solution that I have found is to use .user.ini files in directories where you are developing and would like to see error output (they function like .htaccess files for Apache). There isn't a good way to modify PHP code to display fatal errors, as code that contains a fatal error is never run.

So for your example above you could use a .user.ini file in the same directory as your php file containing the lines:

error_reporting = -1
display_errors = On
html_errors = On

For more info see http://php.net/manual/en/configuration.file.per-user.php