How do I check my PHP error log?
How do I check my PHP error log?
1 Reply
The following command will show you PHP's current error reporting settings:
$ php --info | grep error
Running this command will provide output similar to this:
display_errors => Off => Off
display_startup_errors => Off => Off
error_append_string => no value => no value
error_log => /var/log/php/error.log => /var/log/php/error.log
error_prepend_string => no value => no value
error_reporting => 4177 => 4177
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => Off => Off
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
The error_log
setting will show you the path to your error log file. In this example, the error log is located in the file /var/log/php/error.log
.
Should you not see an entry for error_log
in your PHP settings, PHP may not be logging its errors for you. This file may also be empty, in which case your PHP instance has not yet reported any errors or may need its error reporting levels adjusted. Either way, it would be a good opportunity to review your PHP configuration and potentially adjust it.
The most likely place where you will need to reconfigure your PHP error logging settings is PHP's main configuration file, /etc/php.ini
. Take a look at what your existing error configuration settings are using this command:
$ grep error /etc/php.ini
This will give you output similar to this:
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
error_log = /var/log/php/error.log
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
html_errors = On
The most important settings here are:
error_log
, which provides the file path to PHP's error log, should equal a properly formatted file path to which your PHP process has write accesslog_errors
, which enables error logging to theerror_log
file, should equalOn
error_reporting
, which defines the sorts of errors that PHP should report, is configurable depending on your needs and preferences, butE_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
is a sensible defaultdisplay_errors
, which writes errors to the console, should equalOff
for security and performance purposes
Should any of these settings not be configured properly, you should edit your /etc/php.ini
file and adjust them.
PHP's configuration file follows a simple, predictable syntax that defines configuration section names on their own line in square brackets (e.g., [PHP]
) and configuration variables and their values on their own line separated with an equal sign and an optional space on each side (e.g., log_errors = On
). You may read more about PHP's configuration file in their documentation:
Be sure to consult this PHP Documentation link for more details on configuring its error handling settings:
Be sure to check your /etc/php.d/
directory to see if any of the files in it contain error handling configurations as well:
$ grep -r error /etc/php.d/
If this commands does not return any output, you probably don't need to do anything else unless you were expecting it to report back some error handling configurations. Should you need to reconfigure any of the files in this directory, simply review and repeat the reconfiguration instructions noted above for those files.
Once you are comfortable with your PHP error handling configuration, you will need to reload it, generally by restarting your web server. You can generally handle this by issuing the restart using the systemctl restart
command followed by the service name of your web server.
Apache:
sudo systemctl restart httpd
Nginx:
sudo systemctl restart nginx
Once you have restarted your web server, you can verify your new settings by runningthis command again:
$ php --info | grep error
This will show output similar to this:
display_errors => Off => Off
display_startup_errors => Off => Off
error_append_string => no value => no value
error_log => /opt/log/php/error.log => /opt/log/php/error.log
error_prepend_string => no value => no value
error_reporting => 4177 => 4177
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => Off => Off
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
In this example, the error_log
file path changed from /var/log/php/error.log
to /opt/log/php/error.log
.