How to Install and Configure Memcached on Ubuntu 24.04

16 October 2024

1_healthytip

Memcache is the powerful in memory database server widely used for high performance data storage. It stored the object in format of key-value pairs, which is very suitable format for string objects. It is widely used for storing sessions in a load balancing environment. Many of the dynamic websites uses Memcahe for caching object to achieve and high throughput

This article will help you to install and configure Memcached server on Ubuntu 24.04 LTS systems.

Step 1: Install Memcached

First of all, update Apt package cache on your system then install Memcached service on your system. Execute below commands from command prompt.

sudo apt update
sudo apt install memcached

Step 2: Configure Memcached

You can find the details information about Memcache configuration here. For inital level configuration check for the following settings under Memcache configuration file /etc/memcached.conf.

  • -d => Run Memcached in deamon mode. You can use this option to configure your Memcached server to run as service.
  • -m => Define the maximum number of Memory can be used by Memcached deamon. (default: 64 MB)
  • -p => Defind port for Memcached to listen on. (default: 11211)
  • -l => Defind the IP address to Memcached listen on. Set 0.0.0.0 to listen on all IPs(interfaces) configured on system.

After making changes, restart the Memcached service.

Step 3: Verify Memcached

Use the following command to check and verify that Memcached service is running properly on your system. This will show you the current statstics of your Memcached server. So the values may be differ that below results.

echo "stats settings" | nc localhost 11211
STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 0
STAT inter 127.0.0.1,::1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT shutdown_command no
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 1
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT auth_enabled_ascii no
STAT item_size_max 1048576
STAT maxconns_fast yes
STAT hashpower_init 0
STAT slab_reassign yes
STAT slab_automove 1
STAT slab_automove_ratio 0.80
STAT slab_automove_window 30
STAT slab_chunk_max 524288
STAT lru_crawler yes
STAT lru_crawler_sleep 100
STAT lru_crawler_tocrawl 0
STAT tail_repair_time 0
STAT flush_enabled yes
STAT dump_enabled yes
STAT hash_algorithm murmur3
STAT lru_maintainer_thread yes
STAT lru_segmented yes
STAT hot_lru_pct 20
STAT warm_lru_pct 40
STAT hot_max_factor 0.20
STAT warm_max_factor 2.00
STAT temp_lru no
STAT temporary_ttl 61
STAT idle_timeout 0
STAT watcher_logbuf_size 262144
STAT worker_logbuf_size 65536
STAT read_buf_mem_limit 0
STAT track_sizes no
STAT inline_ascii_response no
STAT ext_item_size 512
STAT ext_item_age 4294967295
STAT ext_low_ttl 0
STAT ext_recache_rate 2000
STAT ext_wbuf_size 4194304
STAT ext_compact_under 0
STAT ext_drop_under 0
STAT ext_max_sleep 1000000
STAT ext_max_frag 0.80
STAT slab_automove_freeratio 0.010
STAT ext_drop_unread no
STAT ssl_enabled no
STAT ssl_chain_cert (null)
STAT ssl_key (null)
STAT ssl_verify_mode 0
STAT ssl_keyformat 1
STAT ssl_ciphers NULL
STAT ssl_ca_cert NULL
STAT ssl_wbuf_size 16384
STAT ssl_session_cache no
STAT ssl_kernel_tls no
STAT ssl_min_version tlsv1.2
STAT num_napi_ids (null)
STAT memory_file (null)
STAT client_flags_size 4
END
How to Install and Configure Memcached on Ubuntu 24.04
How to Install Memcached on Ubuntu 24.04

Step 4: Install Memcached PHP Module (Optional)

To connect Memcached using PHP application required to installed its PHP extension. First, make sure you have installed latest PHP from ppa:ondrej/php PPA on your Ubuntu system. If you have already installed PHP on your system, just skip PHP installation commands below.

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php php-dev php-pear libapache2-mod-php

Now install PHP Memcached module on your system. The below command will also do the required configuration.

sudo apt-get install -y php-memcached

After completing the installation, you must restart the Apache service.

sudo systemctl restart apache2

Check if Memcache php extension is enabled and working properly. Create a info.php file using following code


<?php
  phpinfo();
?>

Now access info.php on the web interface and search for Memcache, You will get the result like below.

Conclusion

This tutorial will help you to install Memcached server on Ubuntu 24.04. You can connect the Memcache server on port 11211 using command line as well as programming languages.

Leave a comment