How to Setup Memcache on CentOS 7

Try it in our public cloud & Get $50 Credit

Memcache is a key piece of infrastructure for many applications. Databases, queues and remote services are often the bottleneck in web requests, but in many instances the information they contain does not change. Memcache is a standard daemon that can cache arbitrary values, and can speed up everything from MySQL queries to REST requests.

Getting Started with Memcache

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) with CentOS 7 and a LAMP stack previously installed

When we’re finished, your LAMP stack will be augmented with Memcache, and you can begin integrating it into existing sites and apps.


To start, we’ll install the memcached package.

yum -y install memcached

The package is now installed, but it must also be started. We must also configure it to run on boot so it launches when your server comes up.

systemctl start memcached.service
systemctl enable memcached.service

Now memcached must be configured for optimal use. We’ll take care of that here.

nano /etc/sysconfig/memcached

You’ll want to allocate some memory to memcached. This is achieved by changing the CACHESIZE value, setting it to the desired amount of RAM allocated to the cache.


Since you’ve just changed the configuration, you’ll need to restart memcached so the new values take effect.

systemctl restart memcached.service

To use memcached from PHP, an extension is required. We’ll install that next.

yum -y install php-pecl-memcache

Now that PHP has been updated, you’ll need to restart Apache so it has access to the newly-installed module. Also, restart memcached for good measure.

systemctl restart httpd.service
systemctl restart memcached.service

Let’s confirm that the memcached module is loaded so you can begin using it in scripts.

php -m | grep memcache

To further check the status of the module, we’ll create a PHP info page. This function returns your PHP environment configuration information. If memcached is correctly configured, this will include details of the memcached support.

nano /var/www/html/index.php


We’ll finally check the status of the memcached module in PHP. Do his by visiting the page you just created at http://your_main_ip/index.php


Memcache is now installed, configured and included in your LAMP stack. You can now speed up your PHP applications by incorporating fast, efficient, RAM-based caches into slow portions of your scripts. If this guide was helpful to you, kindly share it with others who may also be interested.