Memcached is used to speed up dynamic database-driven websites by caching data and objects in RAM. This reduces the number of times an external data source must be read, which lowers overheads and speeds up response times. The memory caching software is a free, open-source project that anyone can use.
At the end of the guide, you will know how to install and configure Memcached on your Debian 11 Bullseye operating system.
Table of Contents
- Recommended OS: Debian 11 Bullseye
- User account: A user account with sudo privilages or root access (su command).
Updating Operating System
Update your Debian 11 operating system to make sure all existing packages are up to date:
sudo apt update && sudo apt upgrade
Root or Sudo Access
By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.
By default, Debian 11 has Memcached in its repositories. To install Memcached, enter the following command:
sudo apt install memcached libmemcached-tools
Type Y, then press ENTER KEY to proceed with the installation.
Note, the second install option, the “libmemcached-tools” package, provides additional commands to bring extra interaction and execute abilities to Memcached.
Next, verify Memcached was installed correctly by verifying using the apt-cache policy command:
sudo apt-cache policy memcached
Memcached should be activated by default. To verify this, use the systemctl status command as follows:
systemctl status memcached
If the service has not been activated, start Memcached using the following command:
sudo systemctl start memcached
Other useful commands to managing the Memcached service are as follows:
To enable the Memcached service on system boot:
sudo systemctl enable Memcached
To stop the Memcached service:
sudo systemctl stop memcached
To disable the Memcached service on system boot:
sudo systemctl disable memcached
To restart the Memcached service:
sudo systemctl restart memcached
Next, verify Memcached is actively listening to localhost on the default port 11211. To confirm this type, the following:
ps -ef | grep memcached
memcache 5934 1 0 09:36 ? 00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid root 6591 3653 0 09:55 pts/0 00:00:00 grep memcached
Memcached must be listening to 127.0.0.1. Next, open the default setting in the configuration file located at “/etc/memcached.conf“.
sudo nano /etc/memcached.conf
Now scroll down and 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.
It is recommended to disable UDP. Unless you require this function to be enabled, add the following line to disable it.
Before you finish, changing the default 64MB memory allocation is recommended as this isn’t much for larger websites, and you will not see much benefit using Memcached.
You need to set this to something reasonable for your server. If you have 3 to 6 GB of hardly used RAM, put it to 1GB or 2GB. This is a setting you must decide that runs best for your server. The guide’s server runs on 8GB, so we adjusted our cache to 2GB as an example only.
Now save the file CTRL+O and hit “Y,” then CTRL+X to exit and restart your Memcached instance.
sudo systemctl restart memcached
Optional – Allow Memcached UFW Rules
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:
Singular IP network connection example:
sudo ufw allow proto tcp from <ip address> to any port 11211
Cluster IP network connection with many instances example:
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.
Install Memcached PHP Libraries
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.
sudo apt install php-memcached apache2 libapache2-mod-php php php-cli php-memcached php-memcached
Apache HTTP Server
Apache users can execute the following code to enable Memcached on their system.
phpenmod memcached && sudo service apache2 restart
Nginx HTTP Server
Memcached for Nginx will be enabled in your PHP block by default once installed.
Install Additional Libraries
You can install Python and or Perl support by executing the following commands.
sudo apt install python3-pymemcache
sudo apt install libcache-memcached-libmemcached-perl
Accessing Memcached from Command Line
Memcached stats can be gathered by much additional software and WEB UI’s that it works in conjunction with. However, a better way to check is to interact with Memcached directly using the command line.
First, TELNET into your service:
telnet localhost 11211
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:
STAT pid 5934 STAT uptime 1275 STAT time 1631930242 STAT version 1.6.9 STAT libevent 2.1.12-stable STAT pointer_size 64 STAT rusage_user 0.113635 STAT rusage_system 0.000000 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 67108864 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 6 STAT lru_maintainer_juggles 1325 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
As above, this shows some crucial things you may want to see, such as uptime, number of items in the cache, and the number of client connections to the instance
You can refine the search by looking into the different Memcached slabs (partitions) of memory to return results.
List the slabs in the instance connected:
List of slabs which includes a count of the items stored within each slab:
Next, you can access and delete data using the cachedump command to list the keys.
First, execute the cachedump command:
stats cachedump [slab ID] [number of items, 0 for all items]
Example in action:
stats cachedump 1 0
ITEM testkey [9 b; 1296857316 s] END
As above, SLAB 1 has one item with the key “testkey.” To get the actual value, you can use the “get key” command as follows:
VALUE testkey 0 9 test data END
Lastly, to delete a cached item, in this case, the “testkey” use the following command:
Comments and Conclusion
The tutorial has shown you how to install Memcached on your Debian 11 Bullseye operating system, installing the additional libraries and how to access the terminal command list.
For further documentation, the project’s Github Wiki page explains further in great detail for server admins any specifications or information they require.