Using PHP sessions can be a problem when your PHP applications are load-balanced on many web servers. You can store them on a NFS export or recode the session_set_save_handler using a SQL backend for example. But there is no solution more efficient, more scalable, more performant and easier to deploy than using memcached…
Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load…
Many well-known huge architecture (Facebook, Livejournal, Youtube…) are using it as memory caching to reduce the load on their servers. It can also be used to share PHP sessions among several servers. Let’s see how…
Installation
The first thing is to install the memcached server on your Debian server :
apt-get install memcached |
Then, since the memcache PECL extension now provides its own session handler, it’s easy to plug PHP and memcached servers. Just install the appropriate extension (from Dotdeb) :
apt-get install php5-memcache |
and change some of your PHP settings :
session.save_handler = files ; session.save_path = "N;/path" |
to :
session.save_handler = memcache ; change server:port to fit your needs... session.save_path="tcp://server:port?persistent=1&weight=1&timeout=1&retry_interval=15" |
That’s all! After relaunching your Apache2 server, your PHP sessions will be stored on the memcached server.