The performance of the default WordPresss setup here at Opalstack is OK for a small low-traffic site, but if you've got a lot of content or you're getting a lot of traffic then it's time to start optimizing for performance. One way to do that is to use memcached as a cache for WordPress.
Start a memcached instance
Memcached is a memory object caching system. It runs in the background and stores the results of database queries and other computed data from WordPress in memory. Since the results are stored in memory, the query or computation doesn't need to be performed again and WordPress can get the results directly from the cache. Accessing objects directly from memory is much faster than running a database query or PHP script which in turn makes your WordPress site faster.
To start a memcached instance for your WordPress application, log in to a SSH session as your WordPress app's shell user then execute the following command (replacing NAME_OF_APP
with the name of your WordPress app):
memcached -d -s $HOME/logs/apps/NAME_OF_APP/memcached.sock -P $HOME/logs/apps/NAME_OF_APP/memcached.pid -m 100
Please note the following:
- Run memcached as shown above - do not run it on a network port on shared hosting, and especially not on the default memcached port 11211. If you do then other customers will eventually connect to your memcached instance and access or overwrite your memcached data.
- If you have multiple WordPress apps, run a separate memcached instance for each app. Do not connect multiple WordPress apps to the same memcached instance as they will overwrite each others' data.
Now that you have your memcached instance running you can proceed with connecting it to WordPress. What follows are 2 examples of how to proceed.
Use memcached with W3 Total Cache
W3 Total Cache is a full-featured performance plugin that includes the ability use memcached for several types of caches. This is a commercial plugin, but the free version supports the features will be using here. To use it:
Log in to your WordPress admin dashboard and go to Plugins > Add New.
Use the plugin search box to search for "W3 Total Cache".
Locate the "W3 Total Cache" plugin in the search results and click its "Install Now" button.
After the installation is complete, click the plugin's "Activate" button.
Click "Skip this setup guide." on the Setup Guide page.
In the WordPress admin sidebar under "Performance", click "General Settings".
Scroll through the page and configure the following settings:
Page cache: Enabled (check the box)
Page cache method: Memcached
Database cache: Enabled (check the box)
Database cache method: Memcached
Object cache: Enabled (check the box)
Object cache method: Memcached
Click any of the "Save all settings" buttons to save your changes. If you see an error about memcached servers not running, ignore it. We'll configure that part next.
In the WordPress admin sidebar under "Performance", click "Page Cache".
Scroll down to the "Advanced" section and configure the following setting:
Click "Save all settings" to save the Page Cache settings.
In the WordPress admin sidebar under "Performance", click "Database Cache".
Scroll down to the "Advanced" section and configure the following setting:
Click "Save all settings" to save the Database Cache settings.
In the WordPress admin sidebar under "Performance", click "Object Cache".
Scroll down to the "Advanced" section and configure the following setting:
Click "Save all settings" to save the Object Cache settings.
At this point the W3 Total Cache page, database, and object caches are configured. As you click around the site, the caches will populate and you should begin to notice an improvement in your site's performance.
W3 Total Cache has many other options associated with these caches. For more information about them please visit the W3 Total Cache support page.
Use memcached as an object cache
As a basic optimization you can use the Memcached Redux plugin to configure an object cache. It's an older plugin and doesn't provide as many features as other WordPress performance plugins, but it works well and doesn't inject a bunch of advertising into your WordPress admin dashboard. To use it:
- Log in to your WordPress admin dashboard and go to Plugins > Add New.
- Use the plugin search box to search for "Memcached Redux".
- Locate the "Memcached Redux" plugin in the search results and click its "Install Now" button. Do not click the "Activate" button after the installation is complete as it is not needed for this plugin.
- Log in to a SSH session as your WordPress app's shell user.
- Open the file
~/apps/NAME_OF_APP/wp-content/plugins/memcached-redux/object-cache.php
in your preferred text editor.
- Scroll to line 444 and change this...
$buckets = array( '127.0.0.1:11211' );
... to this, replacing NAME_OF_SHELL_USER
with your shell user name and NAME_OF_APP
with your WordPress app name:
$buckets = array( '/home/NAME_OF_SHELL_USER/logs/apps/NAME_OF_APP/memcached.sock:0' );
- Save the file and exit your editor.
- Execute the following command, replacing
NAME OF APP
with your WordPress app name:
cp ~/apps/NAME_OF_APP/wp-content/plugins/memcached-redux/object-cache.php ~/apps/NAME_OF_APP/wp-content/
At this point the object cache is configured. As you click around the site, the cache will populate and you should begin to notice an improvement in your site's performance.
Other solutions
If you've implemented either of the above methods then you should already be enjoying a faster WordPress site, but those were just 2 examples. There are many other WordPress plugins that can use memcached to help speed up your site, so if the above examples don't suit you then feel free to search the plugin ecosystem for something else.
If you have trouble adapting the above information for use with some other plugin then feel free to ask for help in the comments or contact Opalstack support directly for assistance.