• 3 min read

WordPress with Memcached on Azure websites

Are you looking to improve the performance of you WordPress website running on Azure websites service? If yes, then what you need is a cache to help speed up your website. If you website gets heavy…

Are you looking to improve the performance of you WordPress website running on Azure websites service? If yes, then what you need is 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 on 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 Clients must treat memcached servers 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 (That’s $200 of Windows Azure resources!).

To access your Ubuntu VM, install an SSH client such as 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

8272.graphic.png-550x526

 

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)

sudo -s

apt-get update

4377.Sunitha Muthukrishna - WordPress with Memcached on Azure websites-img2.PNG-550x526

By default 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.

1385.Sunitha Muthukrishna - WordPress with Memcached on Azure websites-img3.png-550x526

Install Memcached

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 the memcached configuration file (memcached.conf) using the following command:

sudo nano /etc/memcached.conf

Comment out the line by adding # in front of line 35

-l 127.0.0.1

Configure WordPress with Memcached

  • Use FTP to access your site.
  • Create a bin folder.
  • Download and copy the memcached PECL extension from here . Note that this extension is for PHP5.4 (32 bit). If your website is using a different configuration then choose the appropriate DLL from here
  • Login to your Management portal to update you WordPress website configuration. In the app settings section under Configure, set the following:

4617.Sunitha Muthukrishna - WordPress with Memcached on Azure websites-img4.png-550x526

  • Download the Memcached Object Cache plugin from this link
  • Copy object-cache.php to wp-content folder.
  • Specify the memcached server details in the 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 the wp-config.php file at the beginning of the file

define(‘WP_CACHE’,true);

How to Test if cache is working

Test whether the caching is working by reloading the home page for more than two times and view the html source. Upon the initial load, justabove the closing tag you should see a comment similar to this:

2235.Screenshot 1.png-550x526

If you refresh the WordPress page a few times, the comment will change, indicating the page is loaded from the cache:

1830.Screenshot 2.png-550x526

Check Memcache Server statistics

I would highly recommend to keep track of your memcached server regularly to understand the state of the server.  Here are a few ways to do this:

  1. You can use the netcat utility that is already available on your Ubuntu VM if you executed “apt-get update” command. Running Netcat 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

4682.Sunitha Muthukrishna - WordPress with Memcached on Azure websites-img5.PNG-550x526

 

2. You can use the watch command to check the status every 2 seconds and list out the status

             watch "echo stats |nc memcachesrv.cloudapp.net 11211"

3. You can do this programmatically by calling PHPs memcached extension’s Memcache::getStats () API.

6557.new image.png-550x526

Future Reading