Enable PHP Slow Log to Troubleshoot WordPress Performance Issues

When WordPress is a fresh site, loading speed is usually not an issue. However, as the site content grows and more plugins are installed, the loading speed may gradually slow down. What causes WordPress to become slow? Is it the database, plugins, or the theme? When encountering technical issues, we cannot rely on guesswork. For WordPress sites, we can enable the PHP slow log (slow log) to troubleshoot and see exactly what is causing the slowdown.

Enable Slow Logging in LNMP Environment

The LNMP environment mentioned here refers to the one configured via https://lnmp.org/.

1. Edit the PHP-FPM configuration file to enable the slow log:

vi /usr/local/php/etc/php-fpm.conf

2. Add the following configuration:

request_slowlog_timeout = 2s
slowlog = /var/log/php-slow.log

3. Restart the PHP-FPM service:

lnmp php-fpm restart

If you have other PHP versions installed, you need to modify the configuration file of the specific PHP version used by your WordPress site to enable the slow log.

Enable PHP Slow Logging in cPanel

1. First, log in to the cPanel server via SSH and run the following commands to create the configuration file:

mkdir /var/cpanel/ApachePHPFPM/
touch /var/cpanel/ApachePHPFPM/system_pool_defaults.yaml

2. Then add the following configuration items into the system_pool_defaults.yaml file:

---
_is_present: 1
slowlog: { name: 'slowlog', value: "/var/log/php-slow.log" }
request_slowlog_timeout: { name: 'request_slowlog_timeout', value: 1 }

3. Rebuild the PHP-FPM configuration and restart the PHP-FPM service:

/usr/local/cpanel/scripts/php_fpm_config --rebuild && /scripts/restartsrv_apache_php_fpm --status

Locating Issues via the Slow Log

After enabling the slow log, open your website and refresh it a few times. You will then be able to see some content in the slow log, like the following example:

[05-Nov-2024 23:25:45]  [pool sample_com] pid 101088
script_filename = /home/sample/public_html/wp-admin/edit.php
[0x00007f38ea017b60] stream_socket_client() /home/sample/public_html/wp-includes/Requests/src/Transport/Fsockopen.php:173
[0x00007f38ea0166f0] request() /home/sample/public_html/wp-includes/Requests/src/Requests.php:469
[0x00007f38ea0165f0] request() /home/sample/public_html/wp-includes/class-wp-http.php:397
[0x00007f38ea016460] request() /home/sample/public_html/wp-includes/class-wp-http.php:638
[0x00007f38ea0163c0] get() /home/sample/public_html/wp-includes/http.php:184
[0x00007f38ea016330] wp_remote_get() /home/sample/public_html/wp-content/plugins/woocommerce-advanced-product-labels/includes/admin/class-wapl-hook-check.php:126
[0x00007f38ea016280] _do_hook_check() /home/sample/public_html/wp-content/plugins/woocommerce-advanced-product-labels/includes/admin/class-wapl-hook-check.php:87
[0x00007f38ea0161e0] missing_hook_notice() /home/sample/public_html/wp-includes/class-wp-hook.php:324
[0x00007f38ea016100] apply_filters() /home/sample/public_html/wp-includes/class-wp-hook.php:348
[0x00007f38ea016090] do_action() /home/sample/public_html/wp-includes/plugin.php:517
[0x00007f38ea015fb0] do_action() /home/sample/public_html/wp-admin/admin-header.php:303
[0x00007f38ea0153f0] [INCLUDE_OR_EVAL]() /home/sample/public_html/wp-admin/edit.php:411

For those with experience in WordPress performance optimization, it’s not hard to see that the WordPress site requested an external URL, and the slow response speed of this external URL affected the opening speed of the WordPress site. Once the reason is known, we can take targeted measures to solve it.

Although this article is a tutorial focused on WordPress, the principles also apply to other PHP programs. If your site encounters slow opening speeds, feel free to try the methods described in this article.

On a side note, we also provide professional WordPress performance optimization services. If your WordPress site is slow, you can entrust us to optimize it for you. For details, please contact us via the WeChat link on the right side of the website.

Related Posts

Leave a Reply

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