How to configure memcached on CentOS


Memcached is used by apps to store sessioning information in system memory rather them storing them locally on the physical disks. For redundancy, an application is hosted on multiple web servers that are load balanced so that requests are divided between the farm. In such scenario your load balancing device should be configured with sticky sessions so that users experience seamless experiences without sudden logoff. Memcached offers the possibility of sharing sessioning information the farm’s web servers thus eliminates the load balacer configuration.
For this scenario I will be using two web servers with the following configuration:

  • web01 – CentOS running Apache; IP address: 10.10.1.1
  • web02 – CentOS running Apache; IP address: 10.10.1.2
Install Memcached by executing yum install memcached on both machines. You’ll also need to make sure that the following packets have been installed on boths machines:
[root@web01 ~]# rpm -qa *php* | sort
php55-php-5.5.28-1.el6.remi.x86_64
php55-php-cli-5.5.28-1.el6.remi.x86_64
php55-php-common-5.5.28-1.el6.remi.x86_64
php55-php-mysqlnd-5.5.28-1.el6.remi.x86_64
php55-php-pdo-5.5.28-1.el6.remi.x86_64
php55-php-pear-1.9.5-12.el6.remi.noarch
php55-php-pecl-jsonc-1.3.7-1.el6.remi.x86_64
php55-php-pecl-memcache-3.0.8-5.el6.remi.x86_64
php55-php-pecl-zip-1.12.5-1.el6.remi.x86_64
php55-php-process-5.5.28-1.el6.remi.x86_64
php55-php-xml-5.5.28-1.el6.remi.x86_64
php55-runtime-2.1-1.el6.remi.x86_64
[root@web01  ~]# rpm -qa *php* | sort | wc -l
12
php55-php-pecl-memcache-3.0.8-5.el6.remi.x86_64 is the php module for memcache:
[root@web01  ~]# rpm -qa *memcache*
memcached-1.4.22-1.el6.remi.x86_64
php55-php-pecl-memcache-3.0.8-5.el6.remi.x86_64
What version of memcache you install depends on the PHP version you use
NOTE:
memcached – is the daemon process (similar with Windows Services if you want)
memcache – is the PHP module that is used to connect to memcached. This is loaded into PHP config file
Once the installation process is completed, we’ll need to modify the config file from /etc/sysconfig/memcached. Modify this file and add in the OPTIONS section the server’s IP address. Proceed with similar configurations for both machines. This is how your configuration files should look like (note that I’ve also modified the cache size and set it to 256):
[root@web01 ~]# cat /etc/sysconfig/memcached
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”256″
OPTIONS=””
[root@web02 ~]# cat /etc/sysconfig/memcached
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”256″
OPTIONS=””

Now we’ll start the memcached service and set it to ON for 2,3 and 5 runlevels. Execute the following commands on both servers:
/etc/init.d/memcached start
chkconfig –levels 235 memcached on

Now it ‘s time to modify the php.ini file to allow memcache. My servers are running php55-php-5.5.28-1.el6.remi.x86_64 (remi version) and not the basic php so the path to the php.ini file is different  (/opt/remi/php55/root/etc/php.ini). Open this file and navigate to the [Session] section and the the following lines:

session.save_handler = memcache
session.save_path = ‘tcp://10.10.1.1:11211,tcp://10.10.1.2:11211’
The session.save_path must contain the IP address and memcached port for each server. Once you’ve made the necessary configurations, save the file and close it.
Now open /opt/remi/php55/root/etc/php.d/memcache.ini in the text editor and uncomment the following lines:
;  Whether to transparently failover to other servers on errors
memcache.allow_failover=1
;  Redundancy : When enabled the client sends requests to N servers in parallel
memcache.redundancy=3
That’s about it for this article, you should now have memcached installed and configured on your two web servers. Note that the version of memcached depends on what php version are you using on the machines so make sure to verify that before installing this service.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s