How to Install Memcached on CentOS 6/7

Memcached is a free, open source, high-performance memory object caching system. It is typically used to cache data acquired from database calls and cache various objects to speed up application acquisition of data to speed up application response.

This article explains how to install latest Memcached stable version of distributed memory object caching system on CentOS 6/7。

Prerequisites

  • Make sure you have a non-root account with sudo privileges to install Memcached.
  • Make sure your system is able to access the Internet properly to download the latest version of Memcached.

Install Memcached on CentOS

The Memcached installation package is included by default in the CentOS 7 system source. Therefore, installing Memcached is very easy, just enter the following command in the command line terminal:

sudo yum install memcached libmemcached

The libmemcached package contains several command line tools for managing the Memcached service.

Once the installation is complete, type the command to start and set the Memcached service to start automatically.

sudo systemctl start memcached
sudo systemctl enable memcached

At this point, you can have Memcached installed and running on your CentOS 7 server.

Configuring Memcached on CentOS

Configure Memcached by editing the /etc/sysconfig/memcached file. By default, Memcached does not restrict local and remote access. In the following sections, we will show you how to configure how to configure local and remote access to Memcached.

It is recommended to configure access restrictions according to actual needs to prevent attacks.

Local access to Memcached

If the client and server are running on the same machine, you can set the Memcached service to allow only local access.

01、First use the system’s own vi editor to open the memcached configuration file:

sudo vi /etc/sysconfig/memcached

Find the OPTIONS parameter and add -l 127.0.0.1 to let memcached only bind to the specified interface.

OPTIONS="-l 127.0.0.1"

Restart the Memcached service by typing the following command to make the changes take effect:

sudo systemctl restart memcached

Remote Access

If client access and Memcached are not on the same machine, you can configure the firewall and only allow client IP access to Memcached service port 11211 (11211 is the default port for the Memcached service).

The following example assumes that you are connecting to a Memcached server over a private network. The IP address of the Memcached server is 192.168.1.101, and the IP address of the client is 192.168.1.102.

CentOS comes with the firewall configuration tool FirewallD. The following command will create a new zone called memcached, open port 11211 and only allow access from the client IP address.

01、First add a dedicated Memcached area for your firewalld policy:

sudo firewall-cmd --new-zone=memcached --permanent

02、 specify the open port, Memcached uses 11211 port by default, here use tcp and udp to listen:

sudo firewall-cmd --zone=memcached --add-port=11211/udp --permanent
sudo firewall-cmd --zone=memcached --add-port=11211/tcp --permanent

03、Specify a private IP address that allows access to the Memcached service:

sudo firewall-cmd --zone=memcached --add-source=192.168.1.102 --permanent

04、Reload the firewall rules to ensure that they take effect:

sudo firewall-cmd --reload

After the firewall is set up, we need to reconfigure OPTIONS in the /etc/sysconfig/memcached file to set the Memcached service to listen on the IP address.

01、First open the /etc/sysconfig/memcached file:

sudo vi /etc/sysconfig/memcached

02、 the configuration OPTIONS option content is -l 192.168.1.102:

OPTIONS="-l 192.168.100.20"

03、Save your changes and restart the Memcached service:

sudo systemctl restart memcached

Client access to the Memcached service

The Memcached service can be accessed through a variety of programming language clients, as illustrated by PHP and Python.

01、PHP

PHP uses Memcached as a cache database for many popular applications (such as WordPress, Drupal, Typeo, etc.) and requires the php-peel-memcached extension.

sudo yum install php-pecl-memcache

02、Python

There are multiple python libraries that can be used to link the memcached service. It is recommended to use pip to manage the libraries needed to install python. By default, pip is used to install the preferred python library.

pip install pymemcache
pip install python-memcached

Conclusion

In this tutorial, you have learned how to install Memcached on CentOS 7. For more information on Memcached, check out the official Memcached Wiki.

Leave a Reply