How to Install Memcached on Ubuntu 24.04 LTS

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.

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
How to Install Memcached on Ubuntu 24.04 LTS 1

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
How to Install Memcached on Ubuntu 24.04 LTS 2

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
How to Install Memcached on Ubuntu 24.04 LTS 3

To test storing and retrieving data, use the following commands:

set mykey 0 900 4
data
How to Install Memcached on Ubuntu 24.04 LTS 4

This command sets a key mykey with the value data.

To retrieve the stored value:

get mykey
How to Install Memcached on Ubuntu 24.04 LTS 5

To exit telnet, type:

quit
How to Install Memcached on Ubuntu 24.04 LTS 6

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:

How to Install Memcached on Ubuntu 24.04 LTS 7

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
How to Install Memcached on Ubuntu 24.04 LTS 8

Allow access only from the localhost:

sudo ufw allow from 127.0.0.1 to any port 11211
How to Install Memcached on Ubuntu 24.04 LTS 9

To block all other external connections:

sudo ufw deny 11211
How to Install Memcached on Ubuntu 24.04 LTS 10

Verify the firewall rules:

sudo ufw status
How to Install Memcached on Ubuntu 24.04 LTS 11

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

Memcached Official Page

Harish Reddy

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap