Lurn.Cloud

Using AWS ElastiCache Memcached with WordPress

Nearly 30 percent of your website ranking according to Google SEO is determined by your website load time. WordPress TTFB (time to first byte) can be greatly affected by the performance of your database. Using memcached can significantly improve your website speed.

“Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.”  –Memcached.org

Amazon ElastiCache – Memcached offers a fully managed in-memory data store/cache service. This is great for improving the read performance of your mySQL database on WordPress as well your website object cache. Thanks to the effort of  W3 Total Cache we can attach our memcached nodes created in AWS to our WordPress Server running on our EC2 instance.

These results below will show the improvement of using Amazon ElastiCache Memcached in the real world by 0.803s.

Before activating memcached. Load time: 1.22s
After activating memcached. Load time: 0.417s. Big improvement!
Installing memcached on Amazon Linux 2 EC2

Your EC2 Instance needs to be able to communicate with your Memcached Node, in order to do that we need to install the PHP Client. As of the writing of this guide, I hope that your WordPress site is upgraded to at least Verizon 7.x of PHP as it can affect the overall performance of your site. First, we need to validate the version of PHP installed. Make note of it. 

CMD PROMPT:

php -v

The output should be:

PHP 7.4.15 (cli) (built: Feb 11 2021 17:53:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Second, we need to download and install the client. Login in the AWS Console. Navigate to the “ElasticCache Service” and locate the “ElastiCache Cluster Client” tab find your version of PHP and copy the download link URL. Example ARM ec2: https://elasticache-downloads.s3.amazonaws.com/ClusterClient/PHP-7.4/latest-64bit-arm

CMD PROMPT:

cd ~/your-download-folder

wget “amazon-elasticache-cluster-client.so download URL”

tar -zxvf “amazon-elasticache-cluster-client.so download file”

Install the Client in the PHP Module folder in Amazon Linux 2

sudo mv amazon-elasticache-cluster-client.so /usr/lib64/php/modules/

Allow PHP to activate the Client, to so add the following.

Add extension=amazon-elasticache-cluster-client.so into file /etc/php.ini

sudo nano /etc/php.ini, add it at the end of the file. Everything should look like this. Then ^X and save.

I recommend restarting apache and the php service by using:

sudo systemctl restart php-fpm

and 

sudo systemctl restart httpd

This will ensure that Apache and PHP see the client. 

Lanching ElasticCache Memcached

Navigate to ElasticCache Cluster and click create. Select Memcached and AWS 

Make sure to open the 1122 port on both the Memcached node as well as the ec2 security group. I recommend that you use the node in the same AZ as your instance and if you are launching instances in a launch Template your assign a node to each AZ. However, if you choose to use one node your system will not be highly available but it shall work fine as long as the AZ does not go down. Expand the drop-down arrow button of the Memcache just launched and copy the Configuration Endpoint.

Example: Configuration Endpoint: clusteryourname-mem.xxxxx.xyz.use2.cache.amazonaws.com:11211

Install W3 cache on WordPress

Install the W3 Total Cache Plugin in wp-admin 

Navigate to the W3 Settings in WordPress and run the setup guide. If you’re using CloudFront you should use Database Caching and Page Cache only to enhance DB reads, as CloudFront handles most object caching. In WP admin -> Performance -> Database Cache. Change Memcached hostname:port / IP:port: clusteryourname-mem.xxxxx.xyz.use2.cache.amazonaws.com:11211 (Configuration Endpoint). Click Test then, run the setup guide to activate Memcached!

Kevin Harrigan

AWS Certified

Hi there, I Hope I can Help. Showcasing AWS Cloud Tutorials 

Search
Topics
New Posts

Host Multiple WordPress URL(s) in Apache Server Apache has the advantage of hosting more than one website at a time; however, setting …

How to setup AWS Site to Site VPN with a $50 Router! So after passing my AWS Architect Associate exam, I noticed …

Using AWS ElastiCache Memcached with WordPress Nearly 30 percent of your website ranking according to Google SEO is determined by your website …

How To Use CloudFront with WordPress What is CloudFront:  “Amazon CloudFront is a content delivery network operated by Amazon Web Services. Content …