Are you looking to improve the performance of you WordPress website running on Azure websites service? If yes, then what you need a cache to help speed up your website. If you website gets heavy traffic, it is optimum to set up some form of distributed memory caching mechanism.
Memcached is a general purpose distributed memory caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached system uses a client–server architecture. The Client which would be your website will use client-side libraries, in this case PECL memcached library to contact the servers which expose their service at port 11211. Each client knows all servers; the servers do not communicate with each other.
If a client wishes to set or read the value corresponding to a certain key, the client’s library first computes a hash of the key to determine the server to use. Then it contacts that server. The server will compute a second hash of the key to determine where to store or read the corresponding value.
Memcached Client must treat memcached server as a transitory cache; they cannot assume that data stored in Memcached is still there when they need it. MemcacheDB, Couchbase Server,
Varnish and other database servers provide persistent storage while maintaining Memcached protocol compatibility.
In this tutorial you will learn how to:
- Setup a Memcached server on an Azure Ubuntu VM
- Configure your WordPress site with Memcached
Setup Memcached on Azure VM
Login to Azure Management portal and create an Ubuntu VM. For more information, see How to create a Linux VM from Virtual Machine Gallery . If you don’t have an Azure account, try out our 30-day free trial with get $200 of Windows Azure resources.
To access your Ubuntu VM, install an SSH client like Putty. For more details, See How to SSH into a Linux VM . Open the putty client and enter your VM name, for example memcachesrv.cloudapp.net and click on Open
Run the following commands after logging in using the machine admin user (in this case , it is azureuser) to start a Linux shell with root user privileges and download/update the package lists from the existing repositories to the newest versions of packages and their dependencies on the VM (See Figure 2)
By default the port 11211 is blocked. To open this port, you need to login to the Azure Management portal to access you VM’s dashboard. Click on ENDPOINTS and add a new endpoint for port 11211.
Installing memcached takes several steps.
To start, install memcached via apt-get.
sudo apt-get install memcached
If you do not have a compiler on your server, you can download build-essential in order to install memcached:
sudo apt-get install build-essential
Edit memcached (memcached.conf) configuration file using the command
sudo nano /etc/memcached.conf
Comment out the line by adding # in front of the line 35
Configure WordPress with Memcached
- Use FTP to access your site, See How to access your website content
- Create a bin folder. Download and copy memcached PECL extension from here . Note this extension is for PHP5.4 (32 bit). If your website is using different configuration then choose the appropriate DLL from here
Login to your Management portal to update you WordPress website configuration. In the AppSettings section under Configure, set the following
- Download the Memcached Object Cache plugin from this link
- Copy object-cache.php to wp-content folder.
Specify the memcached server details in wp-config.php file.
Add something similar just above /* that’s all, stop editing! Happy blogging. */
$memcached_servers = array(
‘default’ => array(‘memcachesrv.cloudapp.net:11211’ )
- Download the Batcache plugin from here
- Upload advanced-cache.php to the /wp-content/ directory
Add the below line to wp-config.php file at the beginning of the file
How to Test if cache is working
Test the caching is working by reloading the home page for more than two times and view the html source. Just above the </head> closing tag you should see something as below:
<!– generated in 1.156 seconds
12382 bytes batcached for 300 seconds –>
If you “Refresh” the WordPress page a few times you will see that the page is being loaded from cache when you view the source of the page
<!– generated 32 seconds ago
generated in 25.842 seconds
served from batcache in 1.955 seconds
expires in 268 seconds –>
Check Memcache Server statistics
I would highly recommend to keep a check on your memcached server regularly to understand the state of the server. Here are a few ways to do this:
You can use netcat that is already available on your Ubuntu VM if you executed “apt-get update” command. This netcat utility along with the command “stats” for the memcached server listening on port 11211 will give you the status of your memcached server.
echo "stats" | nc memcachesrv.cloudapp.net 11211
You can use watch
command to check the status every 2 seconds with this command and list out the status
watch "echo stats |nc memcachesrv.cloudapp.net 11211"
You can do this programmatically by calling PHP memcached extension’s Memcache::getStats () API.
// Enter the memcached server name here