Build a WordPress One-Click application on DigitalOcean and scale it with Memcache
One-click applications are also known as Marketplace images.
This tutorial will walk you through the steps of creating a simple WordPress One-Click application on DigitalOcean and then adding Memcache to prevent or alleviate a performance bottleneck.
Adding caching to your web applications can drastically improve performance. The results of complex database queries, expensive calculations, or slow calls to external resources can be stored in Memcache that can be accessed via fast O(1) lookups. Even for small sites, Memcache can make page loads snappy and help future-proof your app.
Prerequisites
Before you complete the steps in this guide, make sure you have all of the following:
- Some basic familiarity with a terminal or command prompt.
- Familiarity with PHP (and ideally WordPress).
- A DigitalOcean account.
- If you like managing DigitalOcean resource via the CLI, you will need
doctlinstalled and configured.
Create a WordPress One-Click application
To build an app you first need a droplet. Either go to your DigitalOcean dashboard and create one or launch one via the CLI:
$ doctl compute droplet create wordpress-memcache --image wordpress-20-04 --region nyc1 --size s-1vcpu-1gb --ssh-keys <KEY_FINGERPRINT>Give the droplet a minute to come up and then look up its IP via the dashboard or by typing
$ doctl compute droplet listNow you can login to your droplet via
$ ssh root@<YOUR_DROPLET_PUBLIC_IPV4>WordPress installation
A WordPress installation script will run automatically after login.
Enter a real domain if you have one you would like to use. Otherwise, for this tutorial we will use the Droplet’s public IP.
Domain/Subdomain name: <YOUR_DROPLET_PUBLIC_IPV4>Enter an email address for the WordPress admin user
Your Email Address: me@email.comEnter a username
Username: my_adminEnter a password
Password: super_secure_passwordEnter a title for this website
Blog Title: DigitalOcean + WordPress + Memcache FTW!SSL with Let’s Encrypt
Without a fully qualified domain name (FQDN), Let’s Encrypt cannot set up, so skip this step.
Would you like to use LetsEncrypt (certbot) to configure SSL(https) for your new site? (y/n): n
If all has gone well you will see the following
Installation complete. Access your new WordPress site in a browser to continue.You should now be able to access your WordPress site at http://<YOUR_DROPLET_PUBLIC_IPV4>.
Add caching to WordPress
Memcache is an in-memory, distributed cache. Its primary API consists of two operations: SET(key, value) and GET(key). Memcache is like a hashmap (or dictionary) that is spread across multiple servers, where operations are still performed in constant time.
The most common use for Memcache is to cache expensive database queries and HTML renders so that these expensive operations do not need to happen over and over again.
Configure environment
Update the package list:
[root]$ sudo apt-get updateCheck the version of PHP. At the time of writing the image uses PHP 8.0.21. If the PHP version you see is different, change the version number when installing php-memcached.
[root]$ php --versionThen, install the php-memcached library with
[root]$ sudo apt-get install -y php8.0-memcachedTo take effect, restart apache
[root]$ systemctl reload apache2Make sure the memcached module is listed when you run the following
[root]$ php -mProvision a Memcache
To use Memcache in WordPress, you first need to provision an actual Memcached cache. You can easily get one for free from MemCachier. This allows you to just use a cache without having to setup and maintain actual Memcached servers yourself. Make sure to create the cache in the same region as your droplet is in.
Configure WordPress with MemCachier
There are three config variables you will need for WordPress to be able to connect to your cache.
MEMCACHIER_SERVERSMEMCACHIER_USERNAMEMEMCACHIER_PASSWORD
Get them from your MemCachier dashboard and add them to your wp-config.php file.
Note: your droplet comes with nano or vim preinstalled. If you have never used an editor in a terminal before, I recommend using nano for now. In your
rootterminal tab or window, enter the following to edit yourwp-config.phpfile
[root]$ nano /var/www/html/wp-config.phpAdd the following just above /* That's all, stop editing! Happy publishing. */.
/** Memcached credentials */
global $memcached_servers;
$memcached_servers = array( array( '<MEMCACHIER_SERVER>', 11211 ) );
global $memcached_username;
$memcached_username = '<MEMCACHIER_USERNAME>';
global $memcached_password;
$memcached_password = '<MEMCACHIER_PASSWORD>';Note, if you are on a paid MemCachier plan with more than one server, you will add a array( '<MEMCACHIER_SERVER>', 11211 ) value per server. For example, if your plan had two servers, the $memcached_servers value would be:
$memcached_servers = array( array( '<MEMCACHIER_SERVER_1>', 11211 ), array( '<MEMCACHIER_SERVER_2>', 11211 ) );Use WordPress’ integrated caching
WP Object Cache is WordPress’ integrated object cache. It takes care caching WordPress database queries and clearing stale data automatically.
WP Object Cache is WordPress’ class for caching data which may be computationally expensive to regenerate, such as the result of complex database queries.
The MemCachier WordPress object cache backend integrates the MemCachier caching service with WP Object Cache.
To set it up, get the object-cache.php file and move it to /var/www/html/wp-content.
[root]$ wget https://raw.githubusercontent.com/memcachier/wordpress-cache/master/object-cache.php
[root]$ mv object-cache.php /var/www/html/wp-contentDebugging
Note, if the php-memcached module is not installed, WordPress will crash when object-cache.php has been moved to wp-content.
To debug issues, enable debugging in WordPress and check its error logs, by default found in /var/www/html/wp-content/debug.log.
If you see the module is installed with php -m, but the error logs show the module is missing, it’s possible the command line uses a different version of PHP than Apache. In that case, create a file to see the PHP version Apache is configured to use.
[root]$ nano /var/www/html/info.phpAnd in that file add the following
<?php phpinfo(); ?>Then, open http://<YOUR_DROPLET_PUBLIC_IPV4>/info.php in your browser and check the version of PHP being used. If it’s different than the command line version (the version you see when running php --version), then install the version of php-memcached for that version and restart Apache again, as explained in the Configure environment section earlier in this tutorial.
View Memcache statistics
To test your cache is correctly configured, check your MemCachier dashboard. You can see that the first time you access a post or page in WordPress, get misses increases. This is because the first time a post loads, the database queries related to that post are not in the cache. The set cmds also increase because the post’s database queries are saved to the cache. If you refresh the post, get hits increases because the cached database queries are being served.
When you update a post get misses will increase again because the cache was invalidated.
Further reading and resources
- MemCachier Documentation
- WordPress One-Click Application guide
- MemCachier WordPress object cache backend
- WP Object Cache Codex
Paddy