Google Cloud

How to install Memcached on Ubuntu 20.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

How to install Memcached on Ubuntu 20.04. Memcached is a distributed in-memory object caching system for small chunks of data from the results of database queries or API calls, etc.

In this guide you are going to learn how to install Memcached on Ubuntu 20.04.

Prerequisites

  • Root access to server or user access with sudo privileges.

Install Memcached

To install memcached you need to login to your server SSH and execute the following commands.

sudo apt update
sudo apt install memcached libmemcached-tools

The package libmemcached-tools provides a set of tools to work with memcached server.

Once the installation is completed Memcached server will start automatically, to check the status of the server you can use the following command.

sudo service memcached status

You will receive an output similar to the one below.

● memcached.service - memcached daemon
  Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
  Active: active (running) since Tue 2020-08-04 13:46:09 IST; 26s ago
    Docs: man:memcached(1)
Main PID: 12717 (memcached)
   Tasks: 10 (limit: 4370)
  CGroup: /system.slice/memcached.service
          └─12717 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Configure Memcached

To secure the memcached installation you need to make sure your Memcached server is listening only on the local interface 127.0.0.1.

Edit the /etc/memcached.conf file and make sure the -l parameter is having the 127.0.0.1.

sudo nano /etc/memcached.conf

Make sure the -l parameter looks like the one below.

. . .
-l 127.0.0.1
. . .

You can also disable UDP, as it is more likely to be exploited in denial of service attacks. To disable UDP (while leaving TCP unaffected), add the following option to the bottom of this file.

sudo nano /etc/memcached.conf
. . .
-U 0

Restart Memcached server for the changes to take effect.

sudo service memcached restart

Configure Firewall

Once Memcached is configured you can configure firewall to allow connections on the port Memcached runs by default.

Execute the following command to allow port.

sudo ufw allow from your_IP to any port 11211

Memcached for PHP

If you are planing to use Memcached for your PHP based application you need to install this PHP extension.

sudo apt install php-memcached

Memcached for Python

If you are planing to use Memcached for your Python based application you need to install this library using PIP.

pip install python-memcached

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Conclusion

Now you have learned how to install and configure Memcached on Ubuntu 20.04.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.