Logging PHP errors

Somehow, this feels like a personal failing.

The scenario

I try to avoid PHP as much as possible (just not my thing) but I'd installed a PHP-based webapp (REDCap) and there was something occuring that seemed like it should have generated an error. However I could not find any PHP errors logged anywhere on the machine. So, in order to track down a possible error, I had to ensure PHP was logging errors at all, then test it with an actual error.

Note

You may need to use sudo for some parts of this.

PHP configuration

There's a lot of confusing advice out there regarding this issue, much is which is right and some of which is incomplete (especially regarding the issue of permissions). Here's what I found to be relevant:

First, make sure that PHP is actually configured to log errors. In php.ini (which is usually in /etc), make sure that log_errors = On. There is a setting to "display" errors, but that shows errors on the webpage. Don't switch that on. It may also be worthwhile seeing what level of error PHP is logging, although the default value is sensible.

(There is another suggestion that may be worthwhile: Set log_errors_max_len = 0. I'm not sure if it's critical but it was in my "working" group of settings.)

Next, set error_log to the full path of where you want the log to appear. People often recommend that you put it in with the apache / httpd errors, but you could run into permission issues. (See below.) Just use /var/log/ and you should be fine.

The file has to be writable by the webserver process, so find out what that is:

% ps aux | egrep '(apache|httpd)'

Then create the file and give it the correct owner:

% touch /var/log/php_error.log
% chown apache /var/log/php_error.log

Now restart the webserver:

% service httpd restart

Generate an error

To test that everything is working, create a webpage in a useful location:

% vi /var/www/html/error.php

and give it contents like this:

<?php
$denominator = 0;
echo 2/ $denominator;
?>

Load that webpage and you should get something in the error log like:

[22-Apr-2015 14:21:59 UTC] PHP Warning:  Division by zero in /var/www/html/error.php on line 5
[22-Apr-2015 14:22:23 UTC] PHP Parse error:  syntax error, unexpected T_VARIABLE in /var/www/html/error.php on line 5

Postscript

The "error" turned out to be in a user's botched analysis of the data they'd downloaded from the webapp. The app was running fine. Moral: check everything.