I used to think that a hard part of scaling a website from one webserver to two webservers was sharing the sessions between the machines to keep the users logged in whichever machine they were served by. I was pleasantly surprized that it is possible to accomplish sharing sessions between two servers by changing only 5 lines in the php.ini file (the 2 biggies are
Here is a solution for RHEL 5.5:
- Install the memcached server – a distributed memory object caching system:
- libmemcached is unfortunately not part of the RHEL standard packages so we have to install a third party repository first following instructions at http://blog.famillecollet.com/pages/Config-en.
- After installing the 3rd party repo, install libmemcached: sudo yum –enablerepo=epel –enablerepo=remi update memcached && sudo yum –enablerepo=epel –enablerepo=remi install libmemcached libmemcached-devel
- Install the PHP memcache client: sudo pecl install memcache-3.0.3
- The memcache PECL extension includes a session handler which gives us a alternative option to the default PHP session handler (which uses files). Be sure to answer “yes” when the installer asks to “Enable memcache session handler support?“.
- Modify the php.ini file of both servers (e.g. /etc/php.ini) so that the PHP memcache client is used instead of session files:
session.save_handler = files
session.save_handler = memcache
- Modify the PHP memcache.ini file of both servers (e.g. /etc/php.d/memcache.ini) so that the PHP memcache client is configured to share sessions with redundancy and failover support:
memcache.hash_strategy = consistent
memcache.allow_failover = 1
This approach shares sessions between both machines using RAM as storage. The PHP memcache client cleverly writes the session data to both machines so that if one machine fails all users will still be logged in when they are served by the remaining machine.
My exploration of this subject is documented on serverfault: can-a-pool-of-memcache-daemons-be-used-to-share-sessions-more-efficiently. My conclusion is that this method of session sharing is not the best approach but it is the simplest to set up.