Adding Memcache for Magento on Centos 7

This is a quick and dirty list of the stages to install and set up memcache on centos 7 running php 5.4**

Before beginning the memcache installation I would initially run a few tests on a pageload speed test service like pindgom or google page speed test.  This will allow you to compare page load times before and after.  However you need to have visited the pages once or more to get them cached in the first place so do run your before and after tests more than once and take an average.

Step 1: The Memcache Service

Installing the Memcache Service

yum install memcached

Configuring the Memcache Service

Edit: /etc/sysconfig/memcached .  You can change the port from the default 11211 if you want but you need to remember to what though for later. However we will change the options to only allow access from localhost (if this is ok for your setup). Change the OPTIONS line to:

OPTIONS=”-l 127.0.0.1″

Set the service to run on startup

systemctl enable memcached

Start the service

systemctl start memcached

Check the status

systemctl status memcached

Step 2: Installing the Memcache PHP Plugin

yum install memcached.x86_64 php-pecl-memcache.x86_64

If successful check the /etc/php.d folder to see that you have a memcache.ini file.   If you do, restart the webservice.

systemctl restart httpd

If you have failed then some work on the getting the correct repo.   Take a look at available repos with

yum search memcache

Once successful check out your phpinfo page and make sure that memcache is now present. If you do not have a php info page then either create one temporarily or create a permanent one but I recommend giving it a unique name that only you know.  thelwdphpinfo-banana-page.php  or something.  This should help keep others from nosing into your server setup easily.

Step 3: Setting Up Magento to Use Memcache

Go to the app/etc folder in your magento installation and open the local.xml file. Check that you do not already have a cache option set up.  If you do…decide what on earth you are doing. If you do not then we can safely add in the new caching info for memcache.  Add this little lot inside the <global> …. </global>  tags

<cache>
<backend>memcached</backend>
<memcached>
<server>
<host><![CDATA[127.0.0.1]]></host> <!– assuming you set this–>
<port><![CDATA[11211]]></port><!–change this if you set a different port number earlier–>
<persistent><![CDATA[1]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>

Note:  to clear the memcache on the server you can try this in the command line:

memflush –servers=localhost

You should be able to create a small script in PHP to achieve this too:

$m = new Memcache();
$m->addServer(‘localhost’, 11211);
$m->flush(5);

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Apply your human brain cells and complete this highly complicated maths problem *