Installing Memcache on CentOS 7 running PHP 5.6

What is Memcache? It is a free & open source, high-performance, distributed memory object caching system.

Let's get started

First install nc:

sudo yum update && install nc

Next, we need to install Pecl memcache beta version because this is version 3.0.8 (which is required by the Drupal module). Otherwise, it we install the stable version and we will get version 2.2.7 instead.

pecl install memcache-beta

yum install memcached

Wait for it to complete compiling. You don’t need to add memcached.so to php.ini file, it should do this for you.

Now, we need to configure it, enter:

sudo nano /etc/sysconfig/memcached

Update it as follow:

PORT="11211"
USER="memcached"
# max connection 2048
MAXCONN="2048"
# set ram size to 2048 - 2GiB
CACHESIZE="4096"
# listen to loopback ip 127.0.0.1, for network connection use real ip e.g., 10.0.0.4
OPTIONS="-l 127.0.0.1"

The above will starts memcached up as a daemon, using 4GB of memory, and listening on IP 127.0.0.1, port 11211. Save and close the file.

Type the following command to start memcached, enter:

chkconfig memcached on
service memcached start

To stop / restart use the following commands:

service memcached stop
service memcached restart

To view the memcached stat, type the following command:

memcached-tool 127.0.0.1:11211 stats

If fail to connect to 127.0.0.1 11211, check this: http://serverfault.com/questions/669636/why-is-memcache-not-starting-automatically

Finally, restart Apache:

sudo systemctl restart httpd.service

Once it is working, we can begin to use this with Drupal to improve caching.

Let's now setup the Drupal module to use Memcached.

- Install and enable the module: https://www.drupal.org/project/memcache
- Open up /sites/default/settings.php and add the following to the bottom of your file:

For Drupal 7:

$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
$conf['memcache_stampede_protection'] = TRUE;
$conf['cache_default_class'] = 'MemCacheDrupal';
// The 'cache_form' bin must be assigned to non-volatile storage.
$conf['cache_class_cache_form'] = 'DrupalDatabaseCache';
// Don't bootstrap the database when serving pages from the cache.
$conf['page_cache_without_database'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;
// If this server has multiple Drupal installation
// assign unique key for memcache namespace purposes
$conf['memcache_key_prefix'] = 'mydomain_com';

For 'memcache_key_prefix', give it a unique value, such as mydomain_com

For Drupal 8:

$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = 'mydomain_com';

For memcache_key_prefix, give it a unique value, such as mydomain_com

These are just standard values that should be sufficient but if you have more specific needs for a different configured server, check the module's README.txt file

 

Thanks to this article: http://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/

If you are looking for installing memcache on  EA4 for PHP 7, follow this link