memcache

Since we moved to PHP 5.5 and it’s built in zend opcache, we decided to stop using APC.

Since zend opcache filled opcaching need only, we needed something for data-cache. There is one APC fork trying to extract data-caching code on Github. But we wanted something stable and mature so we choose to go with memcache.

I always hated presence of php5-memcache and php5-memcached – two solutions for similar needs. I read this and this and many more threads but could reach a conclusion.

Actually for our trivial needs both extensions should work fine. But we (forced) to choose php5-memcache as w3-total-cache doesn’t support only this!

Installation

apt-get install memcached php5-memcache
service php5-fpm restart

Config

Increase Max Memory memcache can use

By default, its 64MB. You may need more.

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

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"

Extra: Since memcached can works in multiserver mode, you can share session across server. Google for more info on this. I will post our config when we will write one! 😉

Changes in w3-total-cache plugin

In w3-total-cache plugin you need to choose memcache as backend for object-cache and database-cache.

Web-based Stats Viewer for Memcache

There are many but we choose phpmemcacheadmin

cd /var/www/example.com/htdocs/
mkdir mem && cd mem
wget http://phpmemcacheadmin.googlecode.com/files/phpMemcachedAdmin-1.2.2-r262.tar.gz
tar -xvzf phpMemcachedAdmin-1.2.2-r262.tar.gz
chmod +r *
chmod 0777 Config/Memcache.php

 

16 responses to “memcache”

  1. Hi, as soon as I set session.save_handler = memcache (and restart php5) I get PHP Internal Server Errors all around… I cannot for the life of me figure out why ;(
    Can anyone share some tips on debugging this?

  2. I’m using also php5.5 with the zend optimiser. I’ve got issues each time I save the settings in W3TC. I think the setting page is cached by the zend optimiser (not by nginx) so the only way to see the changes is to force a browser refresh. Weird. Any way to force the zend optimiser to check the cache for each request? (like apc stat 0)

  3. So, i use php 5.3…and want use memcached also
    when i uncomment memcache.ini

    session.save_handler = memcache
    session.save_path = “tcp://127.0.0.1:11211”

    my WP not work, blank page
    memcached server is starting of course…and config to -l 127.0.0.1:11211

    • Yes. It seems redis is faster but did not proper support for redis in wordpress & nginx world. So for now using memcache.

      Also difference is too small to matter for WordPress/content sites.

  4. Just to add that if installing all this you need to do service php5-fpm restart so w3-total cache will recognize the install

  5. Hello!
    When i’m trying connect to memcache-server using connect – all work very good. But when i trying use addServer – I all time see error timeout connect to server.
    Can you help?

  6. Hi, really like your posts about wordpress and nginx setup
    how can I make sure I’m using memcache right?

    I install the phpmemcacheadmin, but all the status is 0 ??

    tks

    • Run service memcached status
      You should see: * memcached is running

      Also, make sure you have configured WordPress/W3-Total-Cache plugin to use database-cache/object-cache.