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 tutorial, you will know how to install and configure Memcached on your Almalinux 8 operating system.
- Recommended OS: AlmaLinux 8.
- User account: A user account with sudo privilages or root access (su command).
Updating Operating System
Update your AlmaLinux operating system to make sure all existing packages are up to date:
sudo dnf upgrade --refresh -y
The tutorial will be using the sudo command and assuming you have sudo status.
To verify sudo status on your account:
Example output showing sudo status:
[joshua@localhost ~]$ sudo whoami root
To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on AlmaLinux.
To use the root account, use the following command with the root password to log in.
By default, AlmaLinux has Memcached in its App stream. To install Memcached, enter the following command:
sudo dnf install memcached libmemcached
Type Y, then press ENTER KEY to proceed with the installation.
Note, the second install option, the “libmemcached” package, provides additional commands to bring extra interaction and execute abilities to Memcached.
Next, verify Memcached was installed correctly by verifying its version and build
The service, by default, will not be active. To start Memcached using the following command:
sudo systemctl enable memcached --now
Memcached should be activated by default. To verify this, use the systemctl status command as follows:
systemctl status 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
memcach+ 27220 1 0 22:24 ? 00:00:00 /usr/bin/memcached -p 11211 -u memcached -m 2000 -c 2000 -l 127.0.0.1 -U 0 joshua 29536 25923 0 22:52 pts/0 00:00:00 grep --color=auto memcached
Memcached must be listening to 127.0.0.1. Open the default setting in the configuration file located at “/etc/sysconfig/memcached“.
sudo nano /etc/sysconfig/memcached
The first option is to change the traditional port number for Memcached.
If you have this port number and have existing firewall rules, they will need to be updated. Ideally, this can be left as default.
The second option is to change the max connections to something slightly higher if your system can handle it.
The max connections should be increased slowly and tested. Do not set too high.
Next, change the default 64MB cache size to a size you believe your cache will have some breathing room.
Example of changing to 2GB:
If your server has plenty of resources, any number will do, but be careful and set within your limits if you are on a budget host.
Now scroll down and find the following line and check the “-l” parameter that exists.
From here, you can leave the localhost or specify an internal or external IP address. Also, you can add additional options which will be described below.
The first additional flag is to disable UDP if not required.
An overview of the tutorial to what your setup may look like once finished.
Now save the file CTRL+O and hit “Y,” then CTRL+X to exit and restart your Memcached instance.
sudo systemctl restart memcached
Configure Firewalld for Memcached
By default, no rules are set up for Memcached. This means you will need to create allow rules which is essential to stop attacks. Failure to secure Memcached will lead to issues down the track, so do not skip this unless you have other means to protect your installation.
First, add a new dedicated zone for Memcached firewalld policy:
sudo firewall-cmd --permanent --new-zone=memcached
Next, specify the allowed IP addresses that are permitted to access the Memcached.
sudo firewall-cmd --permanent --zone=memcached --add-source=22.214.171.124
Replace 126.96.36.199 with the IP address that will be added to the allow list.
Once you have finished adding the IP addresses, open the port of the Memcached.
By default, this is TCP port 11211.
sudo firewall-cmd --permanent --zone=memcached --add-port=11211/tcp
Note, you can change the default port in your configuration file if you change the firewall port open rule above to the new value.
After running those commands, reload the firewall to implement the new rules:
sudo firewall-cmd --reload
Example output if successful:
Install Memcached PHP Libraries
Memcached comes with various extensions for the programming languages, but it is primarily used for the PHP. However, Almalinux needs some additional repositories installed to pull these packages from as they do not feature on the default App stream.
First, install the EPEL and Remi PHP repository.
sudo dnf install epel-release -y sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
Next, enable the PHP module you are currently using. Some examples are below.
sudo dnf module enable php:remi-8.0 -y
sudo dnf module enable php:remi-7.4 -y
Next, install Memcached PHP support using the following command.
sudo dnf install php-pecl-memcache php-pecl-memcached
Type Y, then press ENTER KEY to proceed with the installation.
Additionally, for Memcached support with Python, you can install the following packages using the PIP library.
pip3 install pymemcache --user pip3 install python-memcached --user
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 '^]'.
Note, if Telnet is not installed, run the following command to install.
sudo dnf install telnet -y
Note, TELNET should never be allowed to communicate to a public network and restricted in private network environments.
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:
How to Remove (Uninstall) Memcached
To remove Memcached, use the following command in your terminal.
sudo dnf autoremove memcached libmemcached
This will remove Memcached from your system immediately.
Comments and Conclusion
The tutorial has shown you how to install Memcached on your Almalinux 8 operating system, install 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.