In the previous article, we looked at setting up WordPress on AWS EC2 using a MySQL RDS instance for database storage. This was in fact how the present blog was set up. In what follows, we updated the configuration of this blog to use Amazon ElastiCache for Memcached. You can also check this post for some of the advantages ElastiCache provides.
For the current setup, the EC2 instance, RDS instance and Memcache cluster are located in the default VPC as per Amazon’s recommendation.
Set up the Memcached cluster
The first step is to create the Memcached cluster by clicking on Create Cluster > Create memcached cluster.
I went with the options of: Easy Create, Demo, and provided a name for the new cluster.
You may also need to create a new subnet group, if none is available for selection, as seen below.
Wait for your cluster to be created and note its endpoint address.
On the same page, go to Network and security, and note the name of the security group associated with the cluster:
Connect the Memcached cluster to the EC2 instance
In the AWS console, search for VPC, and on the VPC page, go to “Security groups” in the menu, and select this security group to edit. Edit inbound rules to add a new rule. The new rule should be a custom TCP rule opening TCP port 11211 to the WordPress EC2 instance security group.
Install the Memcached client on the EC2 instance
The next step is to install the Amazon memcached client on the EC2 instance. For this to be successful, I needed to install php-dev first (with all its other requirements) and then identify the PHP extensions directory on the system. These are the commands I used:
sudo apt install php-dev php-config --extension-dir
Then, the following commands need to be executed in order to install the memcached client. You may need to replace the folder “/usr/lib/php/20210902” with the result of the php-config command above. Check any other folder paths to make sure they are correct.
cd /home/ubuntu wget https://elasticache-downloads.s3.amazonaws.com/ClusterClient/PHP-7.0/latest-64bit tar -zxvf latest-64bit sudo cp artifact/amazon-elasticache-cluster-client.so /usr/lib/php/20210902 sudo echo 'extension=amazon-elasticache-cluster-client.so' >> /etc/php/8.1/apache2/php.ini sudo apt-get -y install php-memcached sudo reboot
Install W3 Total Cache WordPress plugin
Following the reboot, in WordPress, you can install the W3 Total Cache plugin, activate it and skip the initial setup wizard.
In the WordPress menu, under Performance > General Settings > enable the Database cache using Memcached for the method. You can do the same for Object Cache if desired.
The next step is to navigate to Performance > Database Cache and enter endpoint information, which is the endpoint address of the Elasticache cluster.
Enable “Persistent connection” and Test it. Fingers crossed, it works.
Under Benchmarking, you can run a Google Page Speed benchmark and your results should be satisfactory. Amazon recommends wrk to measure the performance impact of caching.
In W3 Total Cache, you can also enable Page Cache with Disk (enhanced) or Memcached as methods.
If you have any comments on this tutorial, corrections or suggestions, I welcome your input.