In this article, we will guide you through the process of installing and configuring Memcached on Ubuntu 24.04 LTS | How to Install Memcached on Ubuntu 24.04 LTS, covering the prerequisites, step-by-step installation, and key configuration to optimize your caching system for improved performance and scalability.
Memcached is a powerful, in-memory caching system that boosts web application performance by reducing database load. In this guide, we’ll cover how to install and configure Memcached on Ubuntu 24.04 LTS to improve your app’s speed and scalability.
Table of Contents
Prerequisites
- SSH Access with admin privileges
- Ubuntu 24.04 LTS with minimal installation
How to Install Memcached on Ubuntu 24.04 LTS
Step-1: Install Memcached on Ubuntu 24.04 LTS
sudo apt install memcached -y
Step-2: Verify Memcached installation:
memcached --version
Configure Memcached on Ubuntu 24.04 LTS
The main configuration file for Memcached is located at /etc/memcached.conf
. Open this file in the text editor.
sudo nano /etc/memcached.conf
In this file, you can set the memory size, default port, IP address, and other options. Here are some important configurations:
Memory Usage: Set the maximum amount of memory to use for object storage.
-m 64
Default Port: Set the port Memcached will listen on.
-p 11211
IP Address: Specify the IP address Memcached will listen on. By default, it listens on all available addresses.
-l 127.0.0.1
Uncomment the -c 1024
directive to limit the number of simultaneous Memcached connections. Replace 1024 with your desired number of connections.
-c 1024
After making the necessary changes, save and close the file.
To apply the changes, restart the Memcached service using the following command:
sudo systemctl restart memcached
check its status:
sudo systemctl status memcached
Enable Memcached on Boot. To ensure Memcached starts automatically on boot, enable the service:
sudo systemctl enable memcached
Testing Memcached on Ubuntu 24.04 LTS
Install telnet:
sudo apt install telnet -y
Then, connect to Memcached:
telnet 127.0.0.1 11211
To test storing and retrieving data, use the following commands:
set mykey 0 900 4
data
This command sets a key mykey with the value data.
To retrieve the stored value:
get mykey
To exit telnet, type:
quit
Connect to Memcached on Ubuntu 24.04 LTS
Install PHP and the Memcached module:
sudo apt install php php-memcached -y
Create a new sample PHP script to connect to Memcached.
nano memcached.php
Add the following contents to the file.
<?php
echo "Memcached script is running!";
?>
Save and close the file.
The above PHP script connects to Memcached. A new key example with the value Greetings from HostMyCode is added to the Memcached memory and retrieved using the $memcached variable in the application.
Run the script using PHP to test the connection to Memcached:
php memcached.php
Output:
Securing Memcached on Ubuntu 24.04 LTS
Ensure that the firewall allows access to the Memcached port (11211 by default) only from trusted sources. If you are using ufw
(Uncomplicated Firewall), you can do this as follows:
Enable ufw if it is not already enabled:
sudo ufw enable
Allow access only from the localhost:
sudo ufw allow from 127.0.0.1 to any port 11211
To block all other external connections:
sudo ufw deny 11211
Verify the firewall rules:
sudo ufw status
Conclusion:
Installing Memcached on Ubuntu 24.04 is a simple process that boosts application performance by caching frequently used data. By configuring it properly and securing it, Memcached can efficiently reduce database load and improve response times, making it a valuable tool for optimizing dynamic web applications.
Related Articles:
How to Install Memcached on Ubuntu 20.04 LTS
Reference