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.
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:
Give the droplet a minute to come up and then look up its IP via the dashboard or by typing
Now you can login to your droplet via
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 IP.
Enter an email address for the WordPress admin user
Enter a username
Enter a password
Enter a title for this website
SSL with Let’s Encrypt
Without a fully qualified domain name (FQDN), Let’s Encrypt cannot set up, so skip this step.
If all has gone well you will see the following
You should now be able to access your WordPress site at
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.
php-memcached library with
To take effect, restart apache
Make sure the
memcached module is listed when you run the following
Provision 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.
Get them from your MemCachier dashboard and add them to your
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 your
Add the following just above
/* That's all, stop editing! Happy publishing. */.
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:
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
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.