January 27, 2022
How to install Memcached on Ubuntu 20.04

How To Install Memcached On Ubuntu 20.04

On this article we will learn how to install Memcached the latest stable release (1.6.9) with Apache on Ubuntu 20.04 LTS.

Introduction

Memcached is an open-source general-purpose distributed memory-caching system. Memcached is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Memcached was first developed by Brad Fitzpatrick on May 22, 2003. It was originally written in Perl, then later rewritten in C by Anatoly Vorobey, Memcached runs on Unix-like operating systems (Linux and macOS) and on Microsoft Windows. On this article, we will learn how to install Memcached on Ubuntu 20.04 LTS operating system.

Memcached Installation on Ubuntu 20.04

On this tutorial, we will learn how to install Memcached with Apache on an Ubuntu 20.04. Before continuing, there are prerequisites that must be met before installing Memcached.

Prerequisite

  1. Ubuntu server 20.04 with sufficient disk space
  2. root or non root user with sudo privilege
  3. Good internet connection for downloading requested package

Memcached Installation Steps

To install Memcached on Ubuntu 20.04 LTS, will be following the steps below :

  • Update the Ubuntu repository
  • Install Memcached On Ubuntu 20.04
  • Verify Memcached Installation

Update the Ubuntu Repository

By updating our Ubuntu system, it will refresh the local list of available packages software to the newer stable version. To update Ubuntu 20.04 LTS repositories, we will use the command line:

$ sudo apt-get update

Install Memcached On Ubuntu 20.04

The Memcached package is available in the Ubuntu 20.04 default OS repository, by updating the repository package on the previous step, we will have the latest Memcached version. We can install Memcached by running the following command:

$ sudo apt-get install memcached libmemcached-tools -y

The output will be as follow :

mpik@worker1:~$ sudo apt-get install memcached libmemcached-tools -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  chromium-bsu-data fonts-uralic libalut0 libglc0 libglewmx1.13 libgtkglext1 libminizip1 libopenal-data
  libopenal1 libpangox-1.0-0 libsdl2-2.0-0 libsdl2-image-2.0-0 libsndio7.0
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libmemcached11 libmemcachedutil2
Suggested packages:
  libanyevent-perl libcache-memcached-perl libmemcached libterm-readkey-perl libyaml-perl
The following NEW packages will be installed:
  libmemcached-tools libmemcached11 libmemcachedutil2 memcached
0 upgraded, 4 newly installed, 0 to remove and 322 not upgraded.
Need to get 295 kB of archives.
After this operation, 1,072 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 memcached amd64 1.5.22-2ubuntu0.2 [128 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libmemcached11 amd64 1.0.18-4.2ubuntu2 [83.5 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libmemcachedutil2 amd64 1.0.18-4.2ubuntu2 [9,644 B]
Get:4 http://us.archive.ubuntu.com/ubuntu focal/main amd64 libmemcached-tools amd64 1.0.18-4.2ubuntu2 [73.5 kB]
Fetched 295 kB in 3s (103 kB/s)          
Selecting previously unselected package memcached.
(Reading database ... 191944 files and directories currently installed.)
Preparing to unpack .../memcached_1.5.22-2ubuntu0.2_amd64.deb ...
Unpacking memcached (1.5.22-2ubuntu0.2) ...
Selecting previously unselected package libmemcached11:amd64.
Preparing to unpack .../libmemcached11_1.0.18-4.2ubuntu2_amd64.deb ...
Unpacking libmemcached11:amd64 (1.0.18-4.2ubuntu2) ...
Selecting previously unselected package libmemcachedutil2:amd64.
Preparing to unpack .../libmemcachedutil2_1.0.18-4.2ubuntu2_amd64.deb ...
Unpacking libmemcachedutil2:amd64 (1.0.18-4.2ubuntu2) ...
Selecting previously unselected package libmemcached-tools.
Preparing to unpack .../libmemcached-tools_1.0.18-4.2ubuntu2_amd64.deb ...
Unpacking libmemcached-tools (1.0.18-4.2ubuntu2) ...
Setting up memcached (1.5.22-2ubuntu0.2) ...
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /lib/systemd/system/memcached.service.
Setting up libmemcached11:amd64 (1.0.18-4.2ubuntu2) ...
Setting up libmemcachedutil2:amd64 (1.0.18-4.2ubuntu2) ...
Setting up libmemcached-tools (1.0.18-4.2ubuntu2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Install Memcached on Ubuntu 20.04 LTS
Install Memcached on Ubuntu 20.04 LTS

The Memcached service will start automatically after installing is completed.We can verify it, to check its status with the following command:

$ sudo systemctl status memcached

The output will be as follow :

mpik@worker1:~$ sudo systemctl status memcached
● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-08 15:06:35 PST; 57s ago
       Docs: man:memcached(1)
   Main PID: 5933 (memcached)
      Tasks: 10 (limit: 9430)
     Memory: 1.7M
     CGroup: /system.slice/memcached.service
             └─5933 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jan 08 15:06:35 worker1.otodiginet.com systemd[1]: Started memcached daemon.
Memcached service is running on Ubuntu 20.04
Memcached service is running on Ubuntu 20.04

Memcached is binding on port 11211, we can also verify it using command line :

$ ss -antpl | grep 11211

The output will be shown below :

mpik@worker1:~$ ss -antpl | grep 11211
LISTEN  0        1024           127.0.0.1:11211          0.0.0.0:*              

Configuring Memcached

By default, Memcached listens on localhost on port 11211. If any services running on remote host will use our Memcahced, then we will need to configure Memcached to allow connection from the remote host. The Memcached main configuration file is located at /etc/memcached.conf, we will update it to enable connection from any host.

Here is the steps to configure Memcached:

By using text editor, such as nano or vi edit the file. Find and replace the

-l 127.0.0.1

as shown below:

mpik@worker1:~$ sudo vi /etc/memcached.conf
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
#-l 127.0.0.1
-l 192.168.19.137

After all are set, then we will restart Memcached service by typing command line :

$sudo systemctl restart memcached
$sudo systemctl status memcached

The output will be as follow :

mpik@worker1:~$sudo systemctl restart memcached
mpik@worker1:~$sudo systemctl status memcached
● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2022-01-08 15:15:55 PST; 11s ago
       Docs: man:memcached(1)
   Main PID: 9126 (memcached)
      Tasks: 10 (limit: 9430)
     Memory: 1.4M
     CGroup: /system.slice/memcached.service
             └─9126 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 192.168.19.137 -P /var/run/memcached/memcached.p>

Jan 08 15:15:55 worker1.otodiginet.com systemd[1]: Started memcached daemon.

Installing and Configuring Apache With Memcached Support

On this section, we will install and configure Apache web service, PHP, and enabling Memcached support. To do so, we will install Apache2, PHP and restart Apache2 and Memcached services. The task will be shown below.

  1. Installing Apache2 and PHP
  2. Restarting Apache2 and Memcached Service
  3. Verifying Memcached

Installing Apache2 and PHP

At this point, we will install Apache, PHP and PHP Mecached extensions by typiing command line :

$ sudo apt-get install apache2 php libapache2-mod-php php-memcached php-cli -y

The output will be as shown below :

mpik@worker1:~$ sudo apt-get install apache2 php libapache2-mod-php php-memcached php-cli -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  chromium-bsu-data fonts-uralic libalut0 libglc0 libglewmx1.13 libgtkglext1 libminizip1 libopenal-data libopenal1
  libpangox-1.0-0 libsdl2-2.0-0 libsdl2-image-2.0-0 libsndio7.0
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
  libaprutil1-ldap liblua5.2-0 php-common php-igbinary php-msgpack php7.4 php7.4-cli php7.4-common php7.4-json
  php7.4-opcache php7.4-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php libapache2-mod-php7.4 libapr1 libaprutil1
  libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0 php php-cli php-common php-igbinary php-memcached php-msgpack
  php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

...

info: Switch to mpm prefork for package libapache2-mod-php7.4
Module mpm_event disabled.
Enabling module mpm_prefork.
info: Executing deferred 'a2enmod php7.4' for package libapache2-mod-php7.4
Enabling module php7.4.
Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service → /lib/systemd/system/apache-htcacheclean.service.
Setting up php7.4 (7.4.3-4ubuntu2.8) ...
Setting up libapache2-mod-php (2:7.4+75) ...
Setting up php (2:7.4+75) ...
Processing triggers for ufw (0.36-6) ...
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...
Processing triggers for php7.4-cli (7.4.3-4ubuntu2.8) ...
Processing triggers for libapache2-mod-php7.4 (7.4.3-4ubuntu2.8) ...

Restarting Apache2 and Memcached Service

To take effect to the Memcached service, we will restart Apache2 and Memcached services, by following the command line :

$ sudo systemctl restart apache2
$ sudo systemctl restart memcached

Verifying Memcached

To test whether Memcached works or not, we will need a php.info file to show its status. For this purpose we have to create a php.info file located on inside the Apache web root directory.

$ sudo vi /var/www/html/phpinfo.php

<?php
phpinfo();
?>

By using browser, we will query the Memcahced status as shown below.

Memcached on Ubuntu 20.04
Memcached on Ubuntu 20.04

Conclusion

On this article we have shown you how to install Memcached version 1.6.9 on Ubuntu 20.04 LTS. I hope this tutorial we be helpful for anyone who need it. More information about Memcached can be found on Official Memcached website.

Share this article via :

Leave a Reply

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