Memcached is a high-performance, distributed memory object caching system, generic in nature, but originally intended for use in speeding up dynamic web applications by alleviating database load. Memcached is a very useful also in other cases, than only dynamic web applications.

This guide explains howto install Memcached 1.4.15/1.4.17/1.4.29 stable version of distributed memory object caching system on Fedora 25/24/23/22/21/20/19/18/17/16/15/14/13/12 and CentOS / Red Hat (RHEL) 7.2/6.8/5.11. Fedora 24/23/22/21/20/19/18/17/16/15/14/13/12, CentOS 7/6 and Red Hat (RHEL) 7/6 has Memcached on default repos, so extra repositories is not needed.

1. Install Memcached 1.4.15/1.4.17/1.4.29 on Fedora 25/24/23/22, CentOS/Red Hat (RHEL) 7.2/6.8/5.11

1.1 Install Remi repository (not needed on Fedora 25/24/23/22/21/20 and CentOS/RHEL 7/6):


## Remi Dependency on CentOS and Red Hat (RHEL)
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
 
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

1.2 Install Memcached package

Fedora 25/24/23/22


dnf install memcached

Fedora 21/20/19, CentOS/Red Hat (RHEL) 7/6.8


yum install memcached

CentOS/Red Hat (RHEL) 5.11


yum --enablerepo=remi install memcached

1.3 Configure memcached

Most important value to configure is CACHESIZE, which is cache size on Megabytes. Example Following configuration use 512 Mb memory for Memcached. Edit file /etc/sysconfig/memcached.


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

1.4 Start Memcached

Fedora 25/24/23/22/21/20/19 and CentOS/RHEL 7.2


systemctl start memcached.service ## use restart after update
 
systemctl enable memcached.service

CentOS/RHEL 6.8/5.11


# Set Memcached to start automatically on boot
chkconfig memcached on
# Start Memcached
/etc/init.d/memcached start
## OR ##
service memcached start

1.5 Check that Memcached is Running and Working


echo stats | nc localhost 11211
STAT pid 7599
STAT uptime 10
STAT time 1265288542
STAT version 1.4.4
STAT pointer_size 32
STAT rusage_user 0.003999
STAT rusage_system 0.052991
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 6
STAT bytes_written 0
STAT limit_maxbytes 536870912
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
END

# Try to get some value
echo get some_value | nc localhost 11211
END

# Not found, but check the stats again
echo stats | nc localhost 11211
STAT pid 7599
STAT uptime 10
STAT time 1265288542
STAT version 1.4.4
[...]
STAT cmd_get 1
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 1
STAT delete_misses 0
[...]
STAT evictions 0
END

Everything working nice. Then it’s time to test memcache example with some web application.

2. Open Memcached Port (11211) on Iptables Firewall (If the Memcached will also be used other local servers)

2.1 CentOS/Red Hat (RHEL) 6.8/5.11

Edit /etc/sysconfig/iptables file:

nano -w /etc/sysconfig/iptables

Add following INPUT line:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 11211 -j ACCEPT

Restart Iptables Firewall:


service iptables restart
## OR ##
/etc/init.d/iptables restart

2.2 Fedora 25/24/23/22/21/20 and CentOS/Red Hat (RHEL) 7.2

List Your Active Firewalld Zones:

firewall-cmd --get-active-zones

Example output:


public
  interfaces: wlp1s0

Add New Rule to Firewalld:

You might have active zone like public, FedoraWorkstation, FedoraServer.

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

Restart firewalld.service:


systemctl restart firewalld.service

2.3 Test remote connection:

echo stats | nc memcache_host_name_or_ip 11211

3. Install Memcache and/or Memcached PHP Module on Fedora, CentOS and Red Hat (RHEL)

More information about Memcache Module.
More information about Memcached Module.

3.1 Install Memcache Module and PHP


## Fedora 25/24/23/22 ##
dnf install php php-pecl-memcache

## Fedora 21/20/19 and CentOS/RHEL 7.2/6.8/5.11 ##
yum install php php-pecl-memcache

3.2 Install Memcached Module and PHP


## Fedora 25/24/23/22 ##
dnf install php php-pecl-memcached

## Fedora 21/20/19 and CentOS/RHEL 7.2/6.8/5.11 ##
yum install php php-pecl-memcached

3.3 Restart Web server


## Fedora 25/24/23/22/21/20/19 and CentOS/RHEL 7.2 ##
systemctl restart httpd.service

## CentOS/RHEL 6.8/5.11 ##
/etc/init.d/httpd restart
## OR ##
service httpd restart