Tutorials

Install and Configure Memcached on CentOS 7

Introduction

Memcached is a distributed, high-performance, in-memory caching system that allows you to improve and speed up the performance of dynamic web applications by caching data and objects in memory. It is primarily used to speed up sites that make heavy use of databases.

Memcached is also used to cache entire database tables and queries to improve the performance of the database. Memcached is free and is used by many sites like Facebook, YouTube and Twitter.

This tutorial shows you how to install and configure Memcached on CentOS 7.

Requirements

  • A server running CentOS 7 with Apache and PHP installed

Installing Memcached

First make sure all packages are up to date:

sudo yum -y update

Now execute the following commands to install Memcached:

sudo yum -y install memcached

Start Memcached services and add it to automatically start on your system start-up with:

sudo systemctl enable memcached
sudo systemctl start memcached

Configure Memcached

The default Memcached configuration file is located in the /etc/sysconfig directory.

To configure Memcached, edit the /etc/sysconfig/memcached file:

sudo nano /etc/sysconfig/memcached

Set or update parameters as follows, customizing them to your needs:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

This is a brief description of the parameters:

**PORT**: The default port used by Memcached to run.
**USER**: The user Memcached runs as.
**MAXCONN**: The maximum number of allowed connections to Memcached. You can increase to any number based on your requirements.
**CACHESIZE**: The cache size for memory.
**OPTIONS**: Set IP address of server, so that Apache or Nginx web servers can connect to it.

Exit and save the configuration file, and then restart Memcached:

sudo systemctl restart memcached

Run the following command to verify that Memcached is running:

sudo netstat -tulpn | grep :11211

You will see the following output:

Netstat output when using Memcached

To check the stats of the server using memcached-tool:

sudo memcached-tool 127.0.0.1 stats

Installing the Memcached PHP Extension

You can install PHP extension to work with Memcached daemon with the command:

sudo yum install php-pecl-memcache

Now, restart Apache so that the changes take place:

sudo systemctl restart memcached
sudo systemctl restart httpd

To test Memcached PHP extension, create an info.php file in apache web root directory.

sudo nano /var/www/html/info.php

Add the following content:

<?php
    phpinfo();
?>

Save the file and access it at http://your_server_ip/info.php in a web browser.

When the PHP info page is rendered in your browser look for this section:

Output of a PHP info page with Memcached installed

Installing the Memcached Python Library

You can install Memcached Python library by running the following command:

sudo yum install python-memcached

Now, restart the Apache service so that the changes take place:

sudo systemctl restart httpd

Congratulations! You have successfully installed Memcached

 
  • Nicely explained. Why some prefer CentOS over Debian? To me Debian has been really easy to setup. It was also really easy to configure Memcached with PHP on Debian (I used this guide: https://www.cloudways.com/blog/memcached-with-php/ ). Do you think it is because of performance or maybe security?

Log In, Add a Comment