Lately the WordPress “Site Health” tool is reporting Object Cache as “Recommended”. Using
memcache is one of the most popular solution for this case.
Here are some initial notes:
php-memcacheis a PECL extension;
memcachedis a service;
php-memcachedare two different PECL extensions. Don’t confuse them;
- As of this writing
php-memcache8 is available only for PHP 8+. If your environment still uses PHP 7.4 please remember that it has reached End of Life in 28 Nov 2022;
- Enabling Object Cache will increase memory usage (RAM) on your machine;
- It is mandatory for your users to define a private
WP_CACHE_KEY_SALTkey in their
wp-config.phpfile. The value must be unique for each WordPress install. This helps prevent cache pollution when multiple WordPress installs are using the same
memcachedserver. Not defining a key will have unpredictable results.
This tutorial implies that you are:
- Fully understanding what you’re doing and not just copy/pasting commands;
- Using Plesk on Debian, but this can be easily adapted to RPM-based distros;
php-memcachefor PHP 8.1, but feel free to replace 8.1 with any other PHP 8.X version that you have installed on your Plesk machine;
Now let’s get to work.
1. Install dependencies.
apt install autoconf automake gcc pkg-config zlib1g-dev
2. Install, start and enable the
apt install memcached systemctl enable memcached systemctl restart memcached systemctl status memcached
php-dev in order to have
apt install plesk-php81-dev
4. Install the
php-memcache PHP PECL extension. Answer “yes” when required.
/opt/plesk/php/8.1/bin/pecl install memcache
memcache.so into PHP.
echo "extension=memcache.so" > /opt/plesk/php/8.1/etc/php.d/memcache.ini
6. Update PHP handlers:
plesk bin php_handler --reread
7. Restart services.
systemctl restart plesk-php81-fpm systemctl restart apache2
Note that on RPM-based distros
apache2 should be replaced with
8. Test if
php-memcache is loaded.
/opt/plesk/php/8.1/bin/php -m | grep memcache
At this point the
memcached service is installed/started and the the PHP PECL
php-memcache is installed/loaded.
Now let’s wrap this by installing the Memcached Object Cache WordPress Plugin.
wp-config.php and define
WP_CACHE_KEY_SALT. You can add it anywhere in the file, but I recommend adding it before
$table_prefix (you might have other keys already defined there).
10. Do not install the Memcached Object Cache plugin via the
/wp-admin UI. Doing this might break your WordPress installation. The correct way of installing the plugin is:
- Download the plugin from the official WordPress repository;
- Unpack the
zipfile and locate the
wp-content. Do not place it in
wp-content/pluginsas it might break your WordPress installation. Again, the correct location is
wp-content, even if we’re dealing with a plugin.
11. Check that everything is working as expected by going to Tools -> Site Health. WordPress should not mention Object Cache as a missing/recommended. You should also install a File Caching plugin, for example WP Super Cache. Enjoy a faster website! 🙂
Was this tutorial useful? Buy me a drink by using the “Donate” button below. :)