Install and Configure Memcached on Centos 7

In this article, we want to teach you How To Install and Configure Memcached on Centos 7.

Memcached is an open-source distributed memory caching system. It is used for speeding up dynamic web applications by reducing database load.

How To Install and Configure Memcached on Centos 7

Before you start to install Memcached, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article about the Initial server Setup with Centos 7.

Also, you need to set up a basic firewall with firewalld. To do this, you can follow our article Set up a Firewall with Firewalld on centos 7.

Now follow the steps below to install Memcached on Centos 7.

Installing Memcached on Centos 7

Memcached packages are available in the default Centos repository. First, update your local package index with the following command:

sudo yum update

Then, install Memcached with the following command:

sudo yum install memcached

Next, you need to install libmemcached to manage your Memcached server. To do this, run the following command:

sudo yum install libmemcached

Here you have Memcached installed on your Centos 7. Let’s see how to configure it.

Configuring Memcached on Centos 7

You need to be sure that the Memcached service is listening on the 127.0.0.1 local interface.

Open the Memcached configuration file on Centos 7 with your favorite text editor, here we use vi:

sudo vi /etc/sysconfig/memcached

Put the following content in front of the Options variable:

OPTIONS="-l 127.0.0.1 -U 0"

When you are done, save and close the file.

Now restart and enable your Memcached service to apply the changes with the following commands:

# sudo systemctl restart memcached
# sudo systemctl enable memcached

Verify that your Memcached service is bound to the local interface and listening only on TCP connections with the following command:

netstat -plunt

In your output you will see:

Output
tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 22146/memcached

Also, you can check the stats of the server with the following command:

memcached-tool 127.0.0.1 stats

In your output you will see:

Output
#127.0.0.1:11211 Field Value
accepting_conns 1
auth_cmds 0
auth_errors 0
bytes 0
bytes_read 7
bytes_written 0
cas_badval 0
cas_hits 0
cas_misses 0
cmd_flush 0
cmd_get 0
cmd_set 0
...

Now you need to allow access to the Memcached server by opening a port 11211 on your firewall with the following command:

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

Let’s see how to connect to the Memcached on Centos 7.

Connect to the Memcached

To connect to the Memcached server you need to use a language-specific client.

To use Memcached as a caching database for your PHP application such as WordPress, Drupal, or Magento, you need to install the php-pecl-memcached extension:

sudo yum install php-pecl-memcache

There are several Python libraries for interacting with Memcached. You can install your preferred library using pip:

pip install pymemcache
pip install python-memcached

Conclusion

At this point, you learn to install Memcached on your server.

Hope you enjoy using it.

May these articles be useful for you:

Install and Configure Memcached on AlmaLinux 8

Install and Configure Memcached on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Stay informed and not overwhelmed, subscribe now!