If you’re running WordPress on a VPS – managed or unmanaged – your web hosting provider won’t include memcached as part of the installation. A managed VPS takes care of OS updates, setting up the control panel, e-mail, etc., but only goes so far. Unlike shared hosting, where your web host might have a checkmark in the PHP extensions interface where you can enable or disable various modules, with a VPS, you’ll have to install memcached for WordPress manually.
It’s not very hard to install memcached on a VPS, but you’ll have to take into consideration things like PHP versions for integration into WordPress.
Two Components to Memcached
For your PHP script in WordPress to be able to access the memcached system, two components must be installed:
- The memcached server component
- The memcached PHP component
The first is the memcached service that should be installed on your VPS. It creates and enables the daemon that runs in the background. The second is what allows your PHP scripts to access memcached and use its features. So let’s see how to install them one by one.
Step 1: Installing the memcached Service on a VPS
For the following commands, I’m assuming a CentOS distribution. The commands will be different for Ubuntu, which uses apt-get instead of yum. So make the changes as necessary! Note that all these commands will require at least sudo privileges. In general, I don’t recommend that you log in as root, so create a user with sudo privileges, and dive in!
First, we update the existing repositories:
Then we install memcached and the libmemcached library:
yum install memcached libmemcached
Once this is done, you’ve installed the first component of memcached on your VPS!
Step 2: Secure memcached by Restricting Access to the Local Machine
You don’t want anyone outside your VPS to access memcached and potentially cause a DDoS attack. So we change the configuration file to only allow requests from the local machine to access it. The configuration file is located at:
Open it and paste the following code in the OPTIONS line like this:
OPTIONS=”-l 127.0.0.1 -U 0″
This also blocks the UDP protocol that has been used previously for DDoS attacks.
Start and enable the memcached daemon like this:
systemctl restart memcached
systemctl enable memcached
And you’re done!
Step 3: Install the PHP Component of Memcached
It can get tricky here. I happen to have multiple versions of PHP on my machine, and it was hard for me to figure out how to install the right version of memcached.
To do this, search for the right module for your PHP version. In my case, my site runs on PHP 7.4. So I first do this:
yum search memcached | grep php74
This brings up the following package in the list:
As you can see, the correct name for the memcached package I want is:
So now I install this using the command:
yum install php74-php-pecl-memcached
Let the installation proceed. When it’s done, restart apache and PHP. The process of restarting PHP will be different depending on what kind of PHP set up you have. My installation uses PHP-FPM, so I restart it like this:
systemctl reload php-fpm
Your way might be different. So make sure you read the documentation to see how to do it!
Using memcached on WordPress
To fully utilize memcached, you should have applications that call it. One great example is the caching plugin W3 Total Cache, which you can configure to use memcached. If you have the LiteSpeed web server, then you shouldn’t use a 3rd party caching plugin. But if your server is Apache, then caching is a must! Memcached will speed up the process of caching, so make sure you use it as much as possible!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply