Install Memcached on Ubuntu 20.04

2 min read

Memcached is a free and open-source high-performance in-memory key-value data store. It is generally used to speed up applications by caching various objects from the results of API and database calls.

This article shows how to install and configure Memcached on Ubuntu 20.04.

Installing Memcached

The memcached package is included in the default Ubuntu 20.04 repositories. To install it, enter the following command as root or user with sudo privileges :

sudo apt updatesudo apt install memcached libmemcached-tools

The libmemcached-tools package provides command-line utilities that you can use to manage the memcached server.

Once the installation is completed, the memcached service will start automatically. To check the status of the service, enter:

sudo systemctl status memcached

The output will look something like this:

● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2020-07-13 19:32:01 UTC; 23s ago

That’s it, you have installed memcached on your Ubuntu 20.04 server, and you can start using it.

Configuring Memcached

Memcached options can be configured in the /etc/memcached.conf file. By default, memcached is set to listen only on localhost.

The default configuration settings are sufficient for most users.

Remote Access

If the client connecting to memcached is running on the same host, you should not allow remote access.

When improperly configured, memcached can be used to perform a distributed denial-of-service (DDoS) attack. To allow remote access to the memcached server, you need to configure the firewall and open the memcached UDP port 11211 only from trusted clients.

The following example assumes that you want to connect to the memcached server over a private network. The memcached server IP is 192.168.100.20, and the client’s IP address is 192.168.100.30.

The first step is to edit the memcached configuration and set the service to listen on the server’s private networking interface:

To do so, open the memcached.conf configuration file:

sudo nano /etc/memcached.conf

Locate the line that begins with -l 127.0.0.1 and replace 127.0.0.1 with the server IP address 192.168.100.20./etc/memcached.conf

-l 192.168.100.20

Copy

Restart the Memcached service for the changes to take effect:

sudo systemctl restart memcached

Once the service is configured, the next step is to open the memcached port in the firewall .

sudo ufw allow from 192.168.100.30 to any port 11211

Connecting to Memcached

There are many implementations of memcached clients written for different programming languages.

PHP

To use Memcached as a caching database for your PHP application such as WordPress, Drupal, Joomla or Magento, you need to install the php-memcached extension:

sudo apt install php-memcached

Python

There are several Python libraries for interacting with memcache. You can install your preferred library using pip :

pip install pymemcache
pip install python-memcached


We want to hear your thoughts about this. Is there anything in particular that you love about it? What can we do to improve our services and experience? Leave a comment below or open a ticket on our helpdesk and we’ll personally review all suggestions and feedback. 

About Servercheap.NET

Since it was founded in 2015, Servercheap has always strived to provide its clients with enterprise-level performance at an unbeatable cost. Servercheap offers a wide range of customizable hybrid and virtual private server hosting services. All Servercheap clients enjoy a 99.9% uptime SLA and 24/7 rapid response support team.
At Servercheap, our core directive has always been to provide our clients with the best services and infrastructure possible, whether you’re hosting a game server, a high-intensity database, a development environment, or anything in-between.
For more information, visit https://www.servercheap.net

Leave a Reply

Your email address will not be published. Required fields are marked *