{"id":57605,"date":"2020-09-30T10:53:27","date_gmt":"2020-09-30T15:53:27","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=57605"},"modified":"2020-09-30T10:53:27","modified_gmt":"2020-09-30T15:53:27","slug":"how-to-monitor-php-error-logs-in-wordpress-and-cpanel","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/how-to-monitor-php-error-logs-in-wordpress-and-cpanel\/","title":{"rendered":"How to Monitor PHP Error Logs in WordPress and cPanel"},"content":{"rendered":"\n
PHP error logs are one of the most useful tools for diagnosing web hosting issues. It\u2019s often difficult to find the cause of unexpected behavior in WordPress\u00ae and other PHP applications. PHP error logs, including WordPress logs, can help you to spot problems and identify the offending plugin, theme, or custom code. <\/p>\n\n\n\n
In this article, we describe what PHP error logs are and why they\u2019re useful, before explaining how to use cPanel & WHM to activate and configure both WordPress logs and the PHP runtime\u2019s logging functionality. <\/p>\n\n\n\n
A PHP error log lists warnings and error messages, which are generated by the language runtime and saved to a file. WordPress is written in PHP, so it handles WordPress\u2019s error messages and logging. <\/p>\n\n\n\n
Errors occur for lots of reasons. A line of code might have a typo in it, or the code might be fine, but something unexpected happens when it\u2019s executed. Either way, the developers want to let you know, so they write code to log a message to a file. Error logs are a time-ordered list of these messages. <\/p>\n\n\n\n
Error logs are incredibly useful for figuring out why WordPress isn\u2019t behaving as you think it should. If it\u2019s consuming excessive server resources, a plugin is broken, or pages don\u2019t load, the logs can tell you why. If you\u2019re in a \u201cWhite Screen of Death\u201d situation where WordPress isn\u2019t working at all, the logs might be the only way to see what\u2019s going on under the hood.<\/p>\n\n\n\n
Before you can troubleshoot with logs, you\u2019ll need to tell WordPress or PHP to start logging. Error logs are off by default because logging consumes server resources. They can also be a security risk if the wrong person sees them; logs sometimes have clues about potential vulnerabilities. <\/p>\n\n\n\n
We\u2019re going to look at two approaches to configuring error logging in cPanel. They are:<\/p>\n\n\n\n
Both can be done quickly in cPanel & WHM. <\/p>\n\n\n\n
The wp-config.php <\/em>file contains WordPress\u2019s configuration, and, with a couple of lines of code, you can turn on debugging mode and tell WordPress to write errors to a log. <\/p>\n\n\n\n First, fire up cPanel\u2019s File Manager<\/em>, which you will find in the main page\u2019s Files<\/em> section. <\/p>\n\n\n\n Your WordPress site is probably in the root or a subdirectory of public_html, <\/em>although it might be somewhere else if your server has a non-standard configuration.<\/p>\n\n\n\n Click on the directory containing the site and find the wp-config.php <\/em>file in the right-hand pane. Select it and click Edit <\/em>in the menu bar. <\/p>\n\n\n\n The file opens in cPanel\u2019s text editor. Scroll down to the line that reads: <\/p>\n\n\n\n Add the following code above <\/strong>that line and then click Save<\/em>:<\/p>\n\n\n\n We\u2019re telling WordPress to turn on debugging mode and output error messages to the log file. We also tell it not to display errors in the interface, because that\u2019s not a good look for a production website. <\/p>\n\n\n\n WordPress will now write error messages to a file called error.log <\/em>in the wp-content <\/em>directory, which is in the root directory of your WordPress site. <\/p>\n\n\n\n You can use the cPanel File Manager <\/em>and text editor to open this file and see the error messages. The most recent messages are at the bottom of the file. They tell you the type of error and which code triggered it, allowing you to track down the plugin or theme file responsible. <\/p>\n\n\n\n When you have diagnosed the problem, be sure to delete the logging code you added to the configuration file. <\/em>The error file will keep growing and will eventually consume a large chunk of your disk allocation. <\/p>\n\n\n\n The method outlined above is great if you want to manage logging through WordPress, but what if you want to log errors for other content management systems and applications? <\/p>\n\n\n\n To achieve widespread logging, you can add code to the php.ini <\/em>file, which you can edit in cPanel\u2019s MultiPHP INI Editor<\/em>. You will only be able to edit this file if your web hosting environment allows it. <\/p>\n\n\n\n Select the Editor <\/em>tab and then a location in the dropdown menu. cPanel displays the existing configuration file, which may be blank. <\/p>\n\n\n\n Add the following to the text entry field and click Save<\/em>. <\/p>\n\n\n\n These directives tell the runtime to log errors to the file designated with error_log, <\/em>which you should change to your preferred location. We\u2019ve turned off startup errors because they are rarely relevant for debugging misbehaving applications. We also instruct PHP not to display errors in web pages because we don\u2019t want users to see them. <\/p>\n\n\n\n Click Save, <\/em>and PHP will start to log errors to the file you chose. You can access the log via the cPanel File Manager<\/em> or by logging in with SSH. <\/p>\n\n\n\n The php.ini <\/em>directives we used are suitable for most web servers, but you can use many other directives to configure PHP. To learn more, take a look at the list of php.ini directives<\/a> in the language\u2019s documentation and our MultiPHP INI Editor<\/a> documentation.<\/p>\n\n\n\n Once you have used the logs to figure out your issue\u2019s source, remove the code you just added or change the log_errors <\/em>value to false<\/em> to deactivate logging.<\/p>\n\n\n\n Logs are an enormously useful tool for diagnosing and fixing unexpected behavior in WordPress sites and other web applications. With cPanel & WHM, it’s straightforward to activate and deactivate logging, reducing the time you spend hunting down elusive issues. <\/p>\n\n\n\n As always, if you have any feedback or comments, please let us know. We are here to help in the best ways we can. You\u2019ll find us on Discord<\/a>, the cPanel forums<\/a>, and Reddit<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" PHP error logs are one of the most useful tools for diagnosing web hosting issues. It\u2019s often difficult to find the cause of unexpected behavior in WordPress\u00ae and other PHP applications. PHP error logs, including WordPress logs, can help you to spot problems and identify the offending plugin, theme, or custom code. In this article, […]<\/p>\n","protected":false},"author":77,"featured_media":65533,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-57605","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\n<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
\/* That's all, stop editing! Happy blogging. *\/<\/code><\/pre>\n\n\n\n
define( 'WP_DEBUG', true );\n\tdefine( 'WP_DEBUG_LOG', true );\n\tdefine( 'WP_DEBUG_DISPLAY', false );\n@ini_set( 'display_errors', 0 );<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
How To Log PHP Errors Beyond WordPress<\/strong><\/h2>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
log_errors = true\nerror_log = \/home\/user1\/logs\/error.log\ndisplay_startup_errors = false\ndisplay_errors = false\nhtml_errors = false\nerror_reporting = E_ALL\nlog_errors_max_len = 0<\/code><\/pre>\n\n\n\n
Efficient Issue Resolution with PHP Error Logs and cPanel<\/strong><\/h2>\n\n\n\n