Contents...
Monitoring of your site’s PHP error is crucial to operating a secure, healthy and well performing websites. PHP errors can reduce performance, waste bandwidth and leave your site vulnerable to attack. There are lots of reasons that may cause error. For example user might have entered an invalid value in a form field, The Web server might run out of disk space, The file or database record that you were trying to access may not exit, The application might not have permission to write a file on system disk. Even if your website working fine on the surface, it may in fact be suffering from undetected PHP error that should be fixed asap.
In this tutorial I am going to explain how we can setup cron script to monitor PHP error on email.
Suggested Read: Top 20 Crontab Examples to Schedule Tasks
Enabling Error Reporting in php.ini File
First of all you will need to enable PHP error reporting in php.in file. Open it and make below changes.
# vim /etc/php.ini error_reporting = E_ALL & ~E_DEPRECATED
As mention above all PHP error will print in log file except deprecated.
; E_USER_DEPRECATED - user-generated deprecation warnings ; ; Common Values: ; E_ALL (Show all errors, warnings and notices including coding standards.) ; E_ALL & ~E_NOTICE (Show all errors, except for notices) ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.) ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors) ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED ; Development Value: E_ALL ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT ; http://php.net/error-reporting error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT ; This directive controls whether or not and where PHP will output errors, ; notices and warnings too. Error output is very useful during development, but ; it could be very dangerous in production environments. Depending on the code ; which is triggering the error, sensitive information could potentially leak ; out of your application such as database usernames and passwords or worse. ; It's recommended that errors be logged on production servers rather than ; having the errors sent to STDOUT. ; Possible Values: ; Off = Do not display any errors ; stderr = Display errors to STDERR (affects only CGI/CLI binaries!) ; On or stdout = Display errors to STDOUT ; Default Value: On ; Development Value: On ; Production Value: Off ; http://php.net/display-errors
After making changes restart your Web server.
# service httpd restart
Now find out your error log file location. In my case my log file is located at “/var/log/php-fpm/error.log“.
Create a Script For PHP Fatal, Warning and Notice Error
Before creating a script make sure PHP errors are printing in your log file. Then create a script like below.
For PHP Fatal Error
Use Below script for all PHP Fatal error.
# vim /root/scripts/php_fatal_error.sh #!/bin/sh # Grep PHP Fatal Error tail -n 100000 /var/log/php-fpm/error.log |awk -F'PHP Fatal error' '{print $2}'| grep -v '^$'| sort | uniq -c | sort -nr | head -n20| sed G > /tmp/php_fatal_error.log # send the email with the unix/linux mail command mail -s "PHP Fatal Error Report" [email protected] -c [email protected] < /tmp/php_fatal_error.log
Save and exit.
Now set executable permission on the script.
# chmod +x /root/scripts/php_fatal_error.sh
Setup Cron For PHP Fatal Error
Now schedule a cron for php_fatal_error.sh script to execute ever 10 minutes. You can set time as per your requirement.
# crontab -e */10 * * * * /root/scripts/php_fatal_error.sh > /dev/null
For PHP Warning Error
Use Below script for all PHP Warning error.
# vim /root/scripts/php_warning_error.sh #!/bin/sh # Grep PHP Warning Error tail -n 100000 /var/log/php-fpm/error.log |awk -F'PHP Warning:' '{print $2}'| grep -v '^$'| sort | uniq -c | sort -nr | head -n20| sed G > /tmp/php_warning_error.log # send the email with the unix/linux mail command mail -s "PHP Warning Error Report" [email protected] -c [email protected] < /tmp/php_Warning_error.log
Save and exit.
Now set executable permission.
# chmod +x /root/scripts/php_warning_error.sh
Setup Cron PHP Warning Error
# crontab -e */10 * * * * /root/scripts/php_warning_error.sh > /dev/null
For PHP Notice Error
Use Below script for all PHP Notice error.
# vim /root/scripts/php_notice_error.sh #!/bin/sh # Grep PHP Notice Error tail -n 100000 /var/log/php-fpm/error.log |awk -F'PHP Notice:' '{print $2}'| grep -v '^$'| sort | uniq -c | sort -nr | head -n20| sed G > /tmp/php_notice_error.log # send the email with the unix/linux mail command mail -s "PHP Notice Error Report" [email protected] -c [email protected] < /tmp/php_notice_error.log
Save and exit.
Now set exectable permission.
# chmod +x /root/scripts/php_notice_error.sh
Setup Cron for PHP Notice Error
# crontab -e */10 * * * * /root/scripts/php_notice_error.sh > /dev/null
For all PHP Fatal, Warning and Notice Error
Use Below script for all PHP error Fatal, Warning and Notice.
# vim /root/scripts/php_all_error.sh #!/bin/sh # Grep all PHP Error tail -n 100000 /var/log/php-fpm/error.log |awk -F'PHP Warning:|PHP Notice:|PHP Fatal error' '{print $2}'| grep -v '^$'| sort | uniq -c | sort -nr | head -n20| sed G > /tmp/php_all_error.log # send the email with the unix/linux mail command mail -s "PHP All Error Report" [email protected] -c [email protected] < /tmp/php_all_error.log
Save and exit.
Now set executable permission.
# chmod +x /root/scripts/php_all_error.sh
Setup Cron for All PHP Error
# crontab -e */10 * * * * /root/scripts/php_all_error.sh > /dev/null
Mail Sample
You will get all PHP error in mail inbox like bleow.
882 DOMDocument::loadHTML(): error parsing attribute name in Entity, line: 1 in /var/www/looklinux/web/post/story.php on line 5045 661 trim() expects parameter 1 to be string, array given in /var/www/looklinux/web/post/manage/_ajax_UpdateEdit_ques.php on line 762 584 MemcachePool::get(): Invalid key in /var/www/looklinux/web/post/_ajax_report_insert.php on line 23 580 html_entity_decode() expects parameter 1 to be string, array given in /var/www/looklinux/web/post/certificate/pdfpdf.php on line 1149 303 implode(): Invalid arguments passed in /var/www/looklinux/web/post/_ajax_lookshow_practice.php on line 2194 276 Invalid argument supplied for foreach() in /var/www/looklinux/web/post/lookreport.php on line 10566 257 mktime() expects parameter 1 to be long, string given in /var/www/looklinux/web/post/lookshowall.php on line 5026 257 Invalid argument supplied for foreach() in /var/www/looklinux/web/post/lookshowall.php on line 6964 238 html_entity_decode() expects parameter 1 to be string, array given in /var/www/looklinux/web/post/certificate/pdf.php on line 1149 207 end() expects parameter 1 to be array, boolean given in /var/www/looklinux/web/post/update_report.php on line 17
Please read man page to know more details about tail, awk, grep, sort, uniq, head and sed command.
I hope this article will help to monitor your all PHP error on email. If you have any queries and problem please comment in comment section.
Thanks:)
If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.
We had an issue where we had an outside development company build and host a microsite for us, which includes a lead generation form that feeds into our lead nurture system in Salesforce. Once a user completes and submits the form, a program that resides on their server sends the data over to our system.
We noticed a period with no leads, so I used HttpFox and saw a 500 (Internal Server Error). We’re trying to figure out a way to automatically monitor this without having control or access to the server. I was wondering if your solution above would have caught this error and sent us a notification.