June 18, 2024

Why WordPress Gets Slower Over Time and How to Use PHP Slow Log to Find the Reason

When a WordPress website is freshly installed and empty, the page loading speed is usually fast. But over time, as the website content grows and more plugins are installed, the site can become slower and slower.

What causes WordPress to slow down? Is it the database? Too many plugins? Or maybe the theme?

When facing technical issues, we shouldn’t just guess. In WordPress, a good way to find the real reason is by turning on the PHP slow log to analyze which part is slowing down the site.

Enable Slow Log in LNMP Environment

The LNMP stack here refers to the one installed via https://lnmp.org/.

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

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

2. Add the following config:

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

3. Restart PHP-FPM service:

lnmp php-fpm restart

If you have installed multiple PHP versions, make sure you edit the correct PHP config file used by your WordPress site.

Enable Slow Log in cPanel

1. SSH into the cPanel server and create config file:

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

2. Add the config to the 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 PHP-FPM config and restart service:

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

Use the Slow Log to Locate the Problem

Once the slow log is enabled, visit your site a few times, and then check the log file. You will see something like this:

[05-Nov-2021 13:25:45]  [pool sample_com] pid 101034
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 developers with WordPress performance experience, it’s easy to see that the site is making an external request, and the slow response from that external URL is causing the delay.

Now that we’ve found the cause, we can fix it specifically instead of guessing.