Being notified quickly of errors triggered on a server allow for a quick resolution of problems on a production site. This guide set up a e-mail notification of PHP errors.

This howto is tested on:

  • Debian 10.0 Buster

Requirements

This howto requires:

Settings

Set the e-mail address receiving the notifications:

targetEmail="root@localhost"

Set the PHP messages log file path:

logPath='/var/log/php.log'

Installation

Detect if sudo is available (“command” is used if not):

cmdProxy='command'
command type -f 'sudo' &>'/dev/null' && cmdProxy='sudo'

Install Simple Event Correlator:

${cmdProxy} apt-get install 'sec' 'bsd-mailx'

Créez le dossier pour les fichiers de configuration:

${cmdProxy} mkdir --parent \
    "/etc/sec.d" \
    "/etc/default/sec.d"

Configuration

Set the PHP messages detection pattern:

pattern='^.+: PHP .+$'

Set the files watched by the daemon:

inputFiles="${logPath}"

If PHP messages are not logged by Syslog, but by Apache 2 instead, use:

pattern='^\[.+\] \[error\] \[client .+\] PHP .+$'
inputFiles='/var/log/apache2/*error.log'

or by Lighttpd:

pattern='^.+: \(mod_fastcgi\.c\..+\) .+$'
inputFiles='/var/log/lighttpd/*error.log'

Set the configuration file name:

confFile='/etc/sec.d/php-errors.conf'

Create the software configuration file:

${cmdProxy} tee "${confFile}" \
<<< "# Capture error lines and store them in php-errors
type=Single
ptype=RegExp
pattern=${pattern}
desc=PHP error or warning
action=add php-errors \$0
 
# Report errors every minute if php-errors is set
type=Calendar
time=* * * * *
desc=Mail PHP errors
context=php-errors
action=report php-errors /usr/bin/mail -s \"PHP errors\" ${targetEmail}; delete php-errors;
"

Create the daemon options file:

${cmdProxy} tee '/etc/default/sec.d/php-errors' \
<<< "# sec options to send email notifications on PHP errors.
RUN_DAEMON=\"yes\"
DAEMON_ARGS=\"--conf='${confFile}' --input=${inputFiles} --pid='/var/run/sec.pid' --detach --syslog='daemon'"

Start the daemon at boot:

${cmdProxy} systemctl enable sec

Restart the daemon:

${cmdProxy} systemctl restart sec

If needed, remove deprecated and notice messages from PHP logs to only send notifications on error and warning messages:

${cmdProxy} php-tools --fix 'error_reporting' 'E_ALL & ~E_DEPRECATED & ~E_NOTICE' \
   && ${cmdProxy} php-tools --reload

Thanks

Categories: PHP

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.