Install and Configure Memcached on Debian 11

In this tutorial, we want to teach you How To Install and Configure Memcached on Debian 11.

Memcached is a free and open-source high-performance memory caching system. It’s typically used to cache database data, API calls, or page rendering chunks in RAM to increase the application performance.

It can store data as little as a number, or as big as a finished HTML page.

The system is designed to be accessed through TCP so it can work in a separate server, and can also be distributed among several servers, summing up a big hash table to store data.

When running out of space, the older data is overwritten. So it must be treated as a transitory (not-persisted) cache, meaning that your data may not still be there.

Install and Configure Memcached on Debian 11

To install Memcached, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can check our guide the Initial Server Setup with Debian 11.

Now follow the steps below to complete this guide.

Set up Memcached on Debian 11

First, you need to update and upgrade your system with the following command:

sudo apt update && sudo apt upgrade -y

By default, Memcached packages are available in the default Debian repository. So use the following command to install Memcached:

sudo apt install memcached libmemcached-tools

Next, verify Memcached was installed correctly by verifying using the command below:

sudo apt-cache policy memcached

You should get the following output:

Output
memcached:
Installed: 1.6.9+dfsg-1
Candidate: 1.6.9+dfsg-1
Version table:
*** 1.6.9+dfsg-1 500
500 http://deb.debian.org/debian bullseye/main amd64 Packages
100 /var/lib/dpkg/status

By default, Memcached should be activated on Debian 11. Verify it by running the command below:

sudo systemctl status memcached
Output
● memcached.service - memcached daemon
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor pre>
Active: active (running) since Sun 2022-06-26 06:04:11 EDT; 1min 41s ago
Docs: man:memcached(1)
Main PID: 18021 (memcached)
Tasks: 10 (limit: 2340)
Memory: 1.1M
CPU: 40ms
CGroup: /system.slice/memcached.service
└─18021 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1>

To enable the Memcached service on system boot, run the following command:

sudo systemctl enable Memcached

Memcached is listening on port 11211. To verify that, run the below command:

ps -ef | grep memcached
Output
memcache 18021 1 0 06:04 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
root 18275 498 0 06:09 pts/0 00:00:00 grep memcached

Configure Memcached

At this point that you have Memcached installed on your server, you must make some configuration changes to the Memcached configuration file.

Open the file with your favorite text editor, here we use vi:

sudo vi /etc/memcached.conf

Find the following line and check the “-l” parameter that exists. Leave as the default unless you have internal IP on a local network or external IP from outside; you must modify the default IP address from 127.0.0.1 to the new IP address.

-l 127.0.0.1

It is recommended to disable UDP. Unless you require this function to be enabled, add the following line to disable it:

-U 0
Also, find the “-m” parameter and set this to something reasonable for your server:
-m 2000

When you are done, save and close the file.

To apply the changes, restart Memcached on Debian 11:

sudo systemctl restart memcached

If you have UFW installed, you need to create UFW allow rules on the TCP port 11211. Depending on your installation and requirements if using singular or in a cluster network, some examples are below:

sudo ufw allow 11211

Singular IP network connection:

sudo ufw allow proto tcp from <ip address> to any port 11211

Cluster IP network connection with many instances:

sudo ufw allow proto tcp from <ip address>/24 to any port 11211

Note: The second UFW rule is a subnet rule. Note, make sure the internal network is secure and trustworthy before allowing it.

To apply the new rules, reload the firewall:

sudo ufw reload

Memcached comes with various extensions for the programming languages, but it is primarily used for the PHP. To install the PHP library enter the following command:

sudo apt install php-memcached apache2 libapache2-mod-php php php-cli php-memcached php-memcached

Apache users can execute the following code to enable Memcached on their system:

phpenmod memcached && sudo service apache2 restart

Install Additional Libraries on Debian 11

You can install Python and or Perl support by executing the following commands.

Python support:

sudo apt install python3-pymemcache

Perl support:

sudo apt install libcache-memcached-libmemcached-perl

Access Memcached from CLI

At this point, you can use Memcached directly from the command line.

To do this, use Telnet in your service:

telnet localhost 11211
Output
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Next, you can get an overview of your Memcached service by using the following command:

stats
Output
STAT pid 18645
STAT uptime 345
STAT time 1656239614
STAT version 1.6.9
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.046042
STAT rusage_system 0.061390
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 2
STAT rejected_connections 0
STAT connection_structures 2
STAT response_obj_oom 0
STAT response_obj_count 1
STAT response_obj_bytes 16384
STAT read_buf_count 2
STAT read_buf_bytes 32768
STAT read_buf_bytes_free 0
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 0
STAT get_hits 0
STAT get_misses 0
STAT get_expired 0
STAT get_flushed 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 touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 2097152000
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT slab_reassign_rescues 0
STAT slab_reassign_chunk_rescues 0
STAT slab_reassign_evictions_nomem 0
STAT slab_reassign_inline_reclaim 0
STAT slab_reassign_busy_items 0
STAT slab_reassign_busy_deletes 0
STAT slab_reassign_running 0
STAT slabs_moved 0
STAT lru_crawler_running 0
STAT lru_crawler_starts 3
STAT lru_maintainer_juggles 394
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT unexpected_napi_ids 0
STAT round_robin_fallback 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT slab_global_page_pool 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evicted_active 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
STAT moves_to_cold 0
STAT moves_to_warm 0
STAT moves_within_lru 0
STAT direct_reclaims 0
STAT lru_bumps_dropped 0
END

You can refine the search by looking into the different Memcached slabs (partitions) of memory to return results.

For example list the slabs in the instance connected:

stats slabs

For more information, you can visit the Memcached Wikis page.

Conclusion

At this point, you learn to Install and Configure Memcached on Debian 11.

Hope you enjoy it.

You may be interested in these articles:

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!