How to install memcache on Plesk

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-memcache is a PECL extension;
  • memcached is a service;
  • php-memcache and php-memcached are two different PECL extensions. Don’t confuse them;
  • As of this writing php-memcache 8 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_SALT key in their wp-config.php file. The value must be unique for each WordPress install. This helps prevent cache pollution when multiple WordPress installs are using the same memcached server. 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;
  • Installing php-memcache for 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 memcached service.

apt install memcached
systemctl enable memcached
systemctl restart memcached
systemctl status memcached

3. Install php-dev in order to have phpize.

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

5. Load 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 httpd.

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.

9. Edit 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).

define('WP_CACHE_KEY_SALT','AddSomeUniqueKeyHere');

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 zip file and locate the object-cache.php file;
  • Upload object-cache.php to wp-content. Do not place it in wp-content/plugins as 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. :)

Leave a Comment

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