When PHP encounters errors, it can easily display them on the screen. Sometimes this is needed, sometimes it isn't. In particular, when a site is running on a production server, displaying minor errors on screen tends to be unnecessary, but keeping some log file of such errors can be useful. For example, everything seems to work, but after a week it's worth looking into this file — what if somewhere, in some spot, a bug is lurking that isn't immediately visible.
By default PHP does not keep such a file, but here we'll learn how to enable its use.
Enabling error logging to a file
So, open the php.ini file /etc/php7/
and find the log_errors directive in it. Set this directive to On:
log_errors = On
Now go further down and find another directive: error_log. It is commented out by default, and the default value is empty. Well, let's set it ourselves and specify as the parameter the full path to the file where error logs should be stored:
error_log = /var/log/apache2/php_errors.log
Don't forget to make sure that creating a file at the specified location is actually possible (i.e. check that there are sufficient permissions for that). The file is created under the name of the web server.
That's it, now restart the web server and look at the location specified above.
Disabling display of errors on screenIf you need to disable displaying errors on screen, find the display_errors option and set it to Off. I also recommend switching the error display content (the error_reporting option) to E_ALL & ~E_DEPRECATED in this case (as recommended in the php.ini file itself).
error_reporting = E_ALL & ~E_DEPRECATED
display_errors = Off
Specifying a separate error file for an individual siteYou can, of course, also specify a separate error file for an individual site. To do this, you need to set the PHP error_log option in the web server settings for this site. In the case of Apache, this would look like this:
<VirtualHost .....>
php_value error_log "/www/mysite/logs/php_errors.log"
</VirtualHost>
Applies to: PHP 5.2.x, 5.3.x
Comments