Sep 27, 2023 4 min read

How to Install and Configure Memcached on Debian 12

Install and Configure Memcached on Debian 12 with our step-by-step tutorial. It enhances website performance with its open-source caching system.

Install and Configure Memcached on Debian 12
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to install and configure Memcached on Debian 12, let's briefly understand – What is Memcached?

Memcached is a popular open-source, high-performance, distributed caching system used to improve website and application performance. It stores frequently accessed data in the server's memory, allowing quick retrieval and reducing database load.

Memcached is commonly employed in web applications to speed up dynamic content delivery by caching database queries, API responses, and HTML fragments. Its simplicity, scalability, and compatibility with various programming languages make it a preferred choice for optimizing website performance. With Memcached, developers can enhance user experience and handle high traffic volumes efficiently.

In this tutorial, you will install and configure Memcached in Debian 12. We will also address a few FAQs on how to install and configure Memcached in Debian 12.

Advantages of Memcached

  1. Enhanced Performance: Memcached improves website speed by caching frequently accessed data in memory, reducing database load.
  2. Scalability: It can be easily scaled by adding more servers, accommodating increasing traffic and data demands.
  3. Reduced Database Load: Memcached minimizes the number of database queries, freeing up server resources.
  4. Language Compatibility: It supports various programming languages, making it accessible for developers across different platforms.
  5. Simple Implementation: Memcached is easy to install and integrate into existing systems, requiring minimal configuration and maintenance.

1) Ensure that the server is up-to-date

It's always a good idea to check if the server packages have been updated before proceeding. Use this command to do so:

sudo apt update
sudo apt upgrade -y

Let's also install some common packages that we'll require for installation and configuration.

sudo apt install -y vim

2) Install Memcached

Memcached packages can be obtained in the default Ubuntu repositories. To install them, use the below command:

sudo apt install -y memcached

Verify the installation of Memcached

sudo apt-cache policy memcached
Output

memcached:
  Installed: 1.6.9+dfsg-1
  Candidate: 1.6.9+dfsg-1
  Version table:
 *** 1.6.9+dfsg-1 500
        500 http://cdn-aws.deb.debian.org/debian bullseye/main amd64 Packages
        100 /var/lib/dpkg/status

3) Start and enable the Memcached service

Start the service by using the below command:

sudo systemctl start memcached

With this command, ensure that the service has been started:

sudo systemctl status memcached
Output

● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-22 14:02:18 UTC; 25s ago
       Docs: man:memcached(1)
   Main PID: 52238 (memcached)
      Tasks: 10 (limit: 4626)
     Memory: 3.1M
        CPU: 19ms
     CGroup: /system.slice/memcached.service
             └─52238 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jan 22 14:02:18 dev-debiansrv.inv.re systemd[1]: Started memcached daemon.

It is evident from the output above that Memcached is operational.

To make the service available upon boot:

sudo systemctl enable memcached

4) Enable Memcached on the firewall

Use this command to open port 11211 on the firewall to allow traffic from the client host if you have ufw installed and enabled.

sudo ufw allow 11211/tcp

Execute the command to verify that port 11211 is open on the firewall.

sudo ufw app list

5) Configure Memcached

The path to the Memcached configuration file is /etc/memcached.conf. To open a file for editing, use this command:

sudo vim /etc/memcached.conf

This is how Memcached is set up by default:

-d

logfile /var/log/memcached.log

-m 64

-p 11211

-u memcache

-l 127.0.0.1

-P /var/run/memcached/memcached.pid

As shown in the final line, Memcached is set up by default to only listen to the localhost system on port 11211. You must either modify the localhost address 127.0.0.1 to the IP of the remote host or configure Memcached to listen on all interfaces in order for applications from remote systems to connect to the server.

Replace the localhost address with the remote client's IP address, 10.70.5.214, to use the remote host.

-l 10.70.5.214

Use 0.0.0.0 instead of 127.0.0.1 to listen on all network interfaces. Modify the OPTIONS line to the one below.

-l 0.0.0.0,::1

Use this command to authenticate the modifications:

sudo ss -tulpn | grep 11211
Output

tcp   LISTEN 0      128      10.70.5.214:11211      0.0.0.0:*    users:(("memcached",pid=55522,fd=28))
tcp   LISTEN 0      128            [::1]:11211         [::]:*    users:(("memcached",pid=55522,fd=29))

FAQs to Install Memcached on Debian 12

Are there any dependencies I need to install before installing Memcached on Debian 12?

No, there are no specific dependencies required. The installation process handles the necessary dependencies automatically.

How do I start and stop Memcached on Debian 12?

To start Memcached, use the command: sudo systemctl start memcached. To stop it, run: sudo systemctl stop memcached.

Can I configure Memcached on Debian 12?

Yes, you can configure Memcached by modifying its configuration file located at /etc/memcached.conf.

Does Memcached start automatically on system boot?

Yes, Memcached is configured to start automatically during system boot on Debian 12.

How can I enable Memcached extensions for PHP on Debian 12?

You can enable the Memcached extension for PHP on Debian 12 by running: sudo apt-get install php-memcached and restarting the PHP service.

Is it possible to use Memcached with other programming languages on Debian 12?

Yes, Memcached is compatible with multiple programming languages, including PHP, Python, Java, and more.

Are there any security considerations for Memcached on Debian 12?

By default, Memcached listens on the local interface only, reducing the exposure to external threats. However, it's important to configure proper firewall settings to enhance security.

Conclusion

You now know how to install and configure the Memcached server. You may now set up your applications to use the Memcached instance and connect to it.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.