How to Install and Configure Memcached on Debian 11
Memcached is an open-source and distributed memory object caching system that holds the most frequently queried data in memory. This will reduce data load time as well as provide ease of access to the database. You can use Memcached to speed up dynamic web applications by alleviating database load. It is simple, easy to deploy and can be integrated with several programming languages including PHP, Python and more.
In this tutorial, I will show you how to install Memcached on Debian 11.
- A server running Debian 11.
- A root password is configured on the server.
Install Memcached on Debian 11
By default, Memcached is included in the Debian 11 default repository. You can install it by running the following command:
apt-get install memcached libmemcached-tools -y
Once the Memcached is installed, start the Memcached service and enable it to start at system reboot:
systemctl start memcached
systemctl enable memcached
You can also check the status of the Memcached using the following command:
systemctl status memcached
You should get the following output:
? memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-11-01 10:12:25 UTC; 14s ago Docs: man:memcached(1) Main PID: 28398 (memcached) Tasks: 10 (limit: 4679) Memory: 3.1M CPU: 28ms CGroup: /system.slice/memcached.service ??28398 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid Nov 01 10:12:25 debian11 systemd: Started memcached daemon.
By default, Memcached listens on port 11211. You can check it using the following command:
ss -antpl | grep memcached
You will get the following output:
LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:* users:(("memcached",pid=28398,fd=26))
Memcached default configuration file is located at /etc/memcached.conf. You can edit it with the following command:
You can change some default options as per your requirements:
-l 127.0.0.1 -U 0 -p 11211 -u memcache -m 2000
Save and close the file then restart the Memcached service to apply the changes:
systemctl restart memcached
Enable Memcached for PHP and Python Applications
In order to use Memcached with a PHP-based application, you will need to install the Memcached PHP library to your server. You can install it using the following command:
apt-get install php-memcached -y
If you want to enable Python and Perl support, run the following command:
apt-get install python3-pymemcache libcache-memcached-libmemcached-perl -y
Once all the libraries are installed, you can proceed to the next step.
Memcached is now installed and configured. Now, create a simple PHP script to verify the Memcached. First, install the Apache and PHP with the following command:
apt-get install apache2 php libapache2-mod-php -y
Once all the packages are installed, create a info.php file using the following command:
Add the following lines:
<?php // Show all information, defaults to INFO_ALL phpinfo(); ?>
Save and close the file then open your web browser and access the URL http://your-server-ip/info.php. You should see that Memcached is enabled on the following test page:
Access Memcached CLI Interface
Memcached provides a command-line interface to interact with Memcached directly using the command line.
First, connect to Memcached using the following command:
telnet localhost 11211
Once you are connected, you will get the following output:
Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'.
Now, check the status of the Memcached using the following command:
You should see some important information such as uptime, number of items in the cache, and the number of client connections to the instance in the following output:
STAT pid 28398 STAT uptime 222 STAT time 1635761765 STAT version 1.6.9 STAT libevent 2.1.12-stable STAT pointer_size 64 STAT rusage_user 0.049028 STAT rusage_system 0.049054 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 3 STAT lru_maintainer_juggles 272 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
Congratulations! you have successfully installed Memcached on Debian 11. You can now integrate Memcached with PHP or Python-based applications and increase your website speed. Feel free to ask me if you have any questions.