Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 552
T. Brian Jones Avatar asked Jan 23 '13 02:01

T. Brian Jones


2 Answers

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

like image 180
Jake W. Avatar answered Oct 31 '22 20:10

Jake W.


Thank you Jake for this answer, this was really helpful. However I would like to add that creating the .user.ini file and add the lines of code didn't work for me.

I added these lines to the .user.ini file instead:

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

After this I was able to see the error and solve the problem.

like image 1
user3629283 Avatar answered Oct 31 '22 19:10

user3629283