====== PHP FPM Logging ======
PHP FPM is a separate daemon and passes its logs to apache by default and they will be included in apache logs. However, apache does not support line breaks in those logs which makes the log entries very difficult to read. To avoid this, php fpm can be configured to log into its own files instead.
This is for php8.2 on debian
vi /etc/php/8.2/fpm/pool.d/www.conf
access.log = /var/log/php/fpm-access.log
catch_workers_output = yes
php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php/fpm-error.log
php_admin_flag[log_errors] = on
mkdir -p /var/log/php
touch /var/log/php/fpm-access.log
touch /var/log/php/fpm-error.log
chown -R www-data:www-data /var/log/php
/etc/init.d/php8.2-fpm restart
a2enmod proxy_fcgi setenvif
a2enconf php8.2-fpm
systemctl reload apache2
source:
https://ypereirareis.github.io/blog/2017/02/20/php-fpm-cli-error-log/
Log rotation daily or when logfile reaches 200M in size:
vi /etc/logrotate.d/php8.2-fpm
add the following:
/var/log/php/fpm-*.log {
rotate 5
daily
maxsize 200M
missingok
notifempty
compress
delaycompress
postrotate
if [ -x /usr/lib/php/php8.2-fpm-reopenlogs ]; then
/usr/lib/php/php8.2-fpm-reopenlogs;
fi
endscript
}
then restart logrotate:
service logrotate restart
email unique php errors of php fpm logs:
Requirement:
apt-get install logtail
#!/bin/bash
# Mail out PHP errors that are in the apache error log.
errorLog=/var/log/php/fpm-error.log
email=postmaster@example.com # Send report here
#FPM Logging
errors=$(/usr/sbin/logtail2 $errorLog | grep "PHP " | awk -F'] PHP ' '{print $2}')
errors=$(echo "$errors" | sort | uniq | rev | sort -k4 | rev)
# Check that we actually have some errors
if [ -n "$errors" ]
then
echo "$errors" | mail "$email" -s "Apps PHP Log entries `date +'%Y-%m-%d %H:%M'`"
fi
Make executable
chmod 755 phperrors.sh
add to crontab
#PHP Errors
0 21 * * * /root/phperrors.sh >/dev/null 2>&1