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.6.14 stable version of distributed memory object caching system on Fedora 2 and CentOS / Red Hat (RHEL) 7.5/6.10. Fedora 29/28/27/26, CentOS 7/6 and Red Hat (RHEL) 7/6 has Memcached on default repos, so extra repositories is not needed.

1. Install Memcached 1.6.14 on Fedora 36/35, CentOS/Red Hat (RHEL) 9/8

1.1 Install Memcached package

Fedora 36/35 and CentOS/Red Hat (RHEL) 9/8

dnf install memcached

1.2 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.3 Start Memcached

Fedora 36/235 and CentOS/RHEL 9/8

systemctl start memcached.service ## use restart after update

systemctl enable memcached.service

1.4 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 Fedora 36/35 and CentOS/Red Hat (RHEL) 9/8

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 36/35 and CentOS/Red Hat (RHEL) 9/8

dnf install php php-pecl-memcache

3.2 Install Memcached Module and PHP

Fedora 36/35 and CentOS/Red Hat (RHEL) 9/8

dnf install php php-pecl-memcached

3.3 Restart Web server

Fedora 36/35 and CentOS/Red Hat (RHEL) 9/8

systemctl restart httpd.service