Hamsa K
6 min read | 4 years ago

How to Install Memcached on Centos 7

Install Memcached on Centos 7

Memcached is a free and open-source high-performance in-memory key-value data store. It is mainly used as a caching system to speed up applications by caching various objects from the results of database calls.

Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. 

Here is the simple steps to install Memcached on Centos 7.

Open Terminal and then switch to root user.

Step 1: Install Memcached

Install following package as below

[root@centos ~]# yum install memcached

you can also install libmemcached, a library that provides several tools to work with your Memcached server.

[root@centos ~]# yum install libmemcached

Once the installation is completed, start and enable the Memcached service.

[root@centos ~]# systemctl start memcached
[root@centos ~]# systemctl enable memcached

Step 2: configure Memcached

By default, Memcached is set to listen on all interfaces.we will show you how to configure the service for local and remote access.When improperly configured Memcached can be used to perform a distributed denial-of-service (DDoS) attack.

Local access

If the client connecting to the server is also running on the same host it is recommended to set the Memcached service to listen to localhost only.

Let's bind Memcached to the local interface and disable the UDP port to avoid potential DDOS attacks.

[root@centos ~]# vi /etc/sysconfig/memcached

Change the line 'options' to the following

OPTIONS="-l -U 0"

Save the file and exit the editor.

Restart the Memcached service for the changes to take effect

[root@centos ~]# systemctl restart memcached
[root@centos ~]# systemctl status memcached
 memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-23 15:34:44 IST; 8s ago
 Main PID: 3800 (memcached)
    Tasks: 6
   CGroup: /system.slice/memcached.service
           ââ3800 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024 -l -U 0
Aug 23 15:34:44 centos systemd[1]: Started Memcached.
Aug 23 15:34:44 centos systemd[1]: Starting Memcached...

You can verify Memcached is bound only to the local interface and listening only to TCP connections with the command netstat.

[root@centos ~]# netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0*               LISTEN      3800/memcached 

This confirms that memcached is bound to address using only TCP.

Remote Access

If the application that will connect to Memcached is hosted on a remote server, you need to configure your firewall and allow access to the Memcached port 11211 only from the client IP address.

For example, the Memcached server IP is and the client’s IP address is then allow client ip address in firewall like below

firewall-cmd --new-zone=memcached --permanent
firewall-cmd --zone=memcached --add-port=11211/tcp --permanent
firewall-cmd --zone=memcached --add-source= --permanent
firewall-cmd --reload

Then open Memcached configuration file and change In the OPTIONS parameter add the server ip address -l

Verify Memcached

[root@centos ~]# memcached-tool stats

Install php Module

[root@centos ~]# yum install php php-pecl-memcache

Now restart the Memcached and Apache server to take effect.

[root@centos ~]# systemctl restart memcached.service
[root@centos ~]# systemctl restart httpd

Note: There are also a few other options that can be modified in the configration file

[root@centos ~]# cat /etc/sysconfig/memcached
CACHESIZE: Caps the amount of memory available to Memcached. The default is 64 MB.
PORT: Specifies on which port Memcached should listen. The default port is 11211.
USER: Specifies which user the service will use to run. By default, the service will run as memcached.
MAXCONN: Caps the number of concurrent connections. The default is 1024.

That’s all!



Warning! This site uses cookies
By continuing to browse the site, you are agreeing to our use of cookies. Read our terms and privacy policy