Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current WordPress documentation for the latest information.

In this step-by-step guide, you’ll learn install memcache for php and wordpress. WordPress is the world’s most popular content management system (CMS), powering over 40% of all websites on the internet.

Prerequisites

Before you begin, make sure you have:

  • WordPress installation (self-hosted)
  • Administrator access to WordPress dashboard
  • FTP/SFTP or file manager access to server files

How to: Install (http://www.memcached.org/ “Memcached”) for php and (http://wordpress.org “WordPress”)

In what seems the eternal quest of having a good performing hosting server one of the tools that are generally referred to is Memcache. What Memcache offers you is a centralized server/cluster that holds in memory cached information. Some of the reasons why it is so popular is because:

  • It allows you to share a common cache store across servers. Think accessing db cached information obtained by Server01 on Server02 or sharing session information between servers.
  • Because it stores information in memory, it is supposed to be fast.

WordPress can leverage Memcache either through php or with plugins. Php can leverage shared session for instance while a plugin like (http://WordPress.org/extend/plugins/w3-total-cache/ “W3 Total Cache”) can use object caching through Memcache. This could provide a great performance improvement specially if your site is hosted on multiple servers. Now, this installation instructions are meant to be simple and provide a quick way to deploy Memcache. I am no expert so some important settings might be overlooked but this should be a good starting guide for someone new to Memcache that wants to deploy it on a (http://www.ubuntu.com “Ubuntu (operating system)”) box. Note: There are two flavors for the php libraries: (http://www.php.net/manual/en/intro.memcache.php) and (http://www.php.net/manual/en/intro.memcached.php). Fortunately for us W3 Total Cache has decided to go with memcache so we can avoid a lenghty discussion of which is better and why. First install memcache and the php library in order for it to work with php and WordPress:

apt-get install memcached php5-memcache

and that’s it! Well, that was rather short. So now let’s go into some settings worth taking a look at:

I. Increase Max Memory Memecached can use

I know it says that the default (64MB) is enough, but you may need more. I am already using 64 mb for what I consider a small site.

  1. Open /etc/memcached.conf
  2. Look for value -m 64
  3. Change it to -m 256
  4. Restart memcached: sudoservice memcached restart

II. Move PHP’s session storage to Memcache

Open /etc/php5/mods-available/memcache.ini Add following lines:

session.save_handler = memcache
session.save_path = "tcp://localhost:11211"

As mentioned this would effectively allow you to share session across multiple servers that use Memcache.

III. Web-Viewer for Memcache Stats

There are many options out there but I saw (https://code.google.com/p/phpmemcacheadmin/) and liked it, plus many sites referenced it. To install visit the site to obtain the latest version: http://blog.elijaa.org/index.php?pages/phpMemcachedAdmin-Download-Version-1.2.2 cd /var/www/memcache.CloudIngenium.com/wwwroot/ wget http://phpmemcacheadmin.googlecode.com/files/phpMemcachedAdmin-1.2.2-r262.tar.gz tar -xvzf phpMemcachedAdmin-1.2.2-r262.tar.gz chmod 0777 Config/Memcache.php So now we need to configure (http://https://httpd.apache.org/ “Apache HTTP Server”):

cd /etc/httpd/conf.d sudo nano memcached.conf include the following in memcached.conf

<VirtualHost *:80>

``ServerName memcached.CloudIngenium.com

``UseCanonicalName Off

``ServerAdmin ``"AdminUser@CloudIngenium.com"

``DocumentRoot ``"/var/www/memcache.CloudIngenium.com/wwwroot/"

<``/VirtualHost``>

Because we didn’t specify the Allow from directive only localhost would be able to access this site. Don´t forget to add this site to Apache and restart for it to publish it. If you wanted to use nginx you should use the following conf config as a template:

server {

``listen 80;

``server_name memcached.CloudIngenium.com;

``access_log /var/``log``/nginx/memcached.CloudIngenium.com-access.``log main;

``location / {

``allow 10.0.0.1``/32;

``deny all;

``proxy_set_header Host $host;

``proxy_set_header X-Real-IP $remote_addr;

``proxy_set_header X-Forwarded-Host $host;

``proxy_set_header X-Forwarded-Server $host;

``proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

``proxy_pass http:``//127.0.0.1:80;

``}

}

(http://img.zemanta.com/zemified_e.png?x-id=ac47e021-b9cc-4654-b960-36e7b023bd03)](http://www.zemanta.com/?px “Enhanced by Zemanta”)

Summary

You’ve successfully learned install memcache for php and wordpress. If you run into any issues, double-check the prerequisites and ensure your WordPress environment is properly configured.