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

2

7

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?

Olindaolinde answered 23/1, 2013 at 2:49 Comment(0)
T
6

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

Tifanie answered 18/10, 2013 at 17:18 Comment(0)
H
1

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.

Held answered 6/6, 2014 at 21:27 Comment(1)
Please next time try to edit Jake's answer (or suggest it in the comments) rather than adding very similar one...Blackford

© 2022 - 2024 — McMap. All rights reserved.