Memcache has proven to be an excellent option not only for caching but sharing information. One of the best use case scenarios I can think right away is with High Availability and Load Balancing. Wit HA and LB you don't have a certitude what server is serving, there sharing information with Memcache is the best way to have session information shared.
I will discuss here how to set it up using CentOS 6 and 7. You might figure out correct paths for your own distribution.
Be sure the following things are done:
- PHP Memcached support on each node you are going to use, you can do that with yum install php-pecl-memcache php-pecl-memcached
- Memcached daemon on each node you are going to use, you can do that with yum install memcached
PHP and Apache Configuration
Edit /etc/php.ini file and look for the lines session.save_handler and session.save_path. You can comment them out as we are going to put these values in another file.
Edit /etc/php.d/memcache.ini and add the following content:
- session.save_handler = memcache
- session.save_path = 'tcp://IP1:PORT,tcp://IP2:PORT'
- memcache.allow_failover = 1
- memcache.session_redundancy = 3
Change IP and PORT for your own values. By default, memcached listen into the port 11211/tcp. There is a documented bug about session_redundancy, value should be N + 1. I haven't verified this, but since the bug affects pecl-memcache 3.0.4 and CentOS 7 ships 3.0.8 I bet it should be fixed. Anyway, it is not a bad idea to use the N + 1 value.
Edit /etc/httpd/conf.d/php.conf and look for the PHP parameters. You need to put the following parameters:
- php_value session.save_handler "memcache"
- php_value session.save_path = "tcp://IP1:PORT,tcp://IP2:PORT"
In CentOS, sessions are stored in /var/lib/php/sessions/ you can delete all files there. After that, just restart Apache. You are done!
Good Luckblog comments powered by Disqus