How to install & use memcached on CentOS

memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load.

memcached allows you to take memory from parts of your system where you have more than you need and make it accessible to areas where you have less than you need.

memcached also allows you to make better use of your memory. If you consider the diagram to the right, you can see two deployment scenarios:

  1. Each node is completely independent (top).
  2. Each node can make use of memory from other nodes (bottom).

Install memcached on CentOS 6/7

yum install memcached

Configure memcached

  1. Edit memcached file via the command
    nano /etc/sysconfig/memcached

    PORT=”11211″
    USER=”memcached”
    MAXCONN=”10240″
    CACHESIZE=”128″
    OPTIONS=”-l 127.0.0.1 -U 0″

  2. Restart memcached service

chkconfig memcached on
service memcached start

To use memcached on Php, you need to install some Php modules.

PHP 5.6

yum –enablerepo=remi,remi-php56 install php-pecl-memcached php-pecl-memcache

PHP 7.0

yum –enablerepo=remi,remi-php70 install php-pecl-memcached php-pecl-memcache

PHP 7.1

yum –enablerepo=remi,remi-php71 install php-pecl-memcached php-pecl-memcache

Restart webserver

service php-fpm restart
service nginx restart