How to Install and Configure Memcached with WordPress on Ubuntu and DDEV

Memcached, WordPress and Ubuntu

In this blog post, I’ll guide you on how to install Memcached on Ubuntu and DDEV to boost your website’s speed. We’ll also explore the configuration process and its integration with WordPress using the Object Cache 4 Everyone plugin, simplifying the steps to achieve enhanced site performance.

What’s Memcached?

Memcached is a high-performance, distributed memory object caching system intended to speed up dynamic web applications by alleviating database load. Essentially, it allows you to store data objects in dynamic memory (RAM) for quick retrieval by websites and applications.

When a web application runs, it typically needs to read data from a database. This process can be slow and resource-intensive, especially under heavy load or with complex queries. Memcached mitigates this by holding the most frequently accessed data in memory. When a request is made, instead of querying the database, the application first checks Memcached. If the data is there (a cache hit), it’s returned immediately, vastly reducing retrieval time. If the data is not in the cache (a cache miss), it’s fetched from the database and then stored in Memcached for future requests.

This approach significantly decreases the number of times an application must query the database, thus reducing database load and improving the application’s overall speed and performance. While it doesn’t eliminate the need for a database, it acts as a highly efficient intermediary for data retrieval.

Think of it as a shortcut for your website’s data!

Step 1: Get Your Server Ready

In this blog post, I’m using an Ubuntu server for all the steps and examples. If you have a different setup, the steps might be a bit different, but you can still follow along!

Update Your System:
Type sudo apt update. This command makes sure your server’s list of programs is up to date.

Install Memcached:
Now, type sudo apt install memcached. This command installs Memcached on your server.

Install Tools for Memcached and PHP:
Type sudo apt install libmemcached-tools. These tools help your website’s language (PHP) talk to Memcached.

Start Memcached:
Type sudo systemctl start memcached. This command turns on the Memcached service.

Step 2: Confirm Your PHP and PHP-FPM Versions

Before proceeding with installing the necessary PHP extensions, it’s crucial to know exactly which version of PHP and PHP-FPM you are running, as mismatches can lead to compatibility issues.

Check Your PHP Version:
Type php -v in your terminal and note down the version of PHP you’re using.

Check PHP-FPM Version:
Enter systemctl list-units | grep php and look for something like php8.1-fpm.service. This line tells you that PHP 8.1 FastCGI Process Manager is running. Make sure that the version of PHP-FPM aligns with your PHP version.

Step 3: Install PHP Memcached Extension for the Correct PHP-FPM Version

Why It Matters:
To ensure the “Object Cache 4 Everyone” plugin can use Memcached effectively, you need the correct PHP Memcached extension. If there’s a version mismatch or the extension isn’t installed, the plugin might revert to using disk support for object caching, which is significantly slower than Memcached.

Install PHP Memcached Extension:
Depending on the PHP version confirmed earlier, install the corresponding Memcached extension. For example, if you are running PHP 8.1-FPM, you would use sudo apt install php8.1-memcached.

Restart PHP-FPM:
After installation, ensure that you restart PHP-FPM to apply the new extension. Use sudo systemctl restart php8.1-fpm, adjusting the command to match your specific PHP-FPM version.

Step 4: Test If Memcached Is Working

Type telnet 127.0.0.1 11211. This command checks if Memcached is ready to go. If you see a connected message, you’re good!

Telnet connect to Memcached server

Step 5: Set Up Your WordPress Plugin

Configure wp-config.php:
Begin by configuring your WordPress installation to work with Memcached by making changes to the wp-config.php file. This ensures once the plugin is activated, it operates correctly without further adjustments.

  1. Locate and Edit wp-config.php: Access the wp-config.php file in the root directory of your WordPress installation using a text editor.
  2. Add Cache Key Salt: Insert the following line to define a unique namespace for your site’s cache: define( 'WP_CACHE_KEY_SALT', 'my-key-salt' ); Customize ‘my-key-salt’ with a unique phrase for your site.
  3. Specify Memcached Server: Add the following line to specify your Memcached server’s address and port: define( 'OC4EVERYONE_MEMCACHED_SERVER' , '127.0.0.1:11211' ); Ensure the details match your Memcached server configuration.
  4. Save and Close: Save the changes and close the wp-config.php file.

Install the Plugin:
Now that your wp-config.php is configured, proceed to your WordPress dashboard, add a new plugin, and search for “Object Cache 4 Everyone.” Install and activate it.

If the plugin is working correctly, you’ll see detailed Memcached statistics and an option to flush the cache directly from the plugins page in your WordPress dashboard. Look for information similar to what’s shown in the screenshot below, which includes performance metrics and a ‘Flush Cache’ link.

Object Cache 4 everyone running

Setting Up Memcached Locally with DDEV

If you’re developing your WordPress site locally and using DDEV as your local development environment, you can also benefit from Memcached’s performance enhancements. Here’s how to set up Memcached in your local DDEV environment:

Install Memcached in DDEV:

  1. Install DDEV-Memcached: Open your terminal and navigate to your project directory. Run the following command to add the Memcached service to your DDEV project: ddev get ddev/ddev-memcached && ddev restart This command fetches the necessary Memcached components for DDEV and restarts your DDEV environment, incorporating Memcached.

Configure WordPress to Use Memcached:

  1. Update Your wp-config.php: Just as with your live server, you need to specify the Memcached server in your local development environment. Use the same lines you added earlier to your wp-config.php file: define( 'WP_CACHE_KEY_SALT', 'your-unique-phrase' ); define( 'OC4EVERYONE_MEMCACHED_SERVER' , 'ddev-yourprojectname-memcached:11211'); Replace your-unique-phrase and ddev-yourprojectname-memcached with appropriate values for your local setup.

By following these additional steps, you can enjoy the speed and efficiency of Memcached even while developing locally with DDEV. This ensures a more consistent development and testing environment, closely mirroring the performance enhancements you would expect in your live environment.

That’s it! You’ve set up Memcached with your WordPress site. Your website should be faster now. Happy debugging!


Posted

in

, ,

by