How to: Install Memcache for php and WordPress
How to: Install Memcache for php and 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 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 Ubuntu box.
Note: There are two flavors for the php libraries: php5-memcache and php5-memcached. 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.
- Open
/etc/memcached.conf
- Look for value
-m 64
- Change it to
-m 256
- Restart memcached: sudo
service 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 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 Apache:
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;
}
}