How to Install Memcached on Ubuntu 20.04 LTS

In this article, We are going to perform How to Install Memcached on Ubuntu 20.04 LTS

Introduction

Memcached is free and open source distributed memory object caching system which is stored in memory key-value pair. It is commonly used to reduce database load and speed up dynamic web applications.

Prerequisites

  • Ubuntu 20.04 LTS
  • SSH Access with sudo privileges
  • Open Ports 11211

update the system packages

 sudo apt-get update

Step #1: How to Install Memcached on Ubuntu 20.04 LTS

Install memcached and libmemcached-tools using below command.

 sudo apt-get install memcached  libmemcached-tools -y

once installation done, check the if Memcached service is running using below command,

 sudo systemctl status memcached.service

Output

● memcached.service - memcached daemon 
Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) 
Active: active (running) 
Main PID: 10919 (memcached) 
Tasks: 6 
Memory: 876.0K 
CPU: 16ms CGroup: /system.slice/memcached.service 
└─10919 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 11.4.0.13

Enable Memcached service at system startup

 sudo systemctl enable memcached.service

Output

Synchronizing state of memcached.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable memcached

So we have covered how to install Memcached on ubuntu 20.04 LTS, Below  are commands to start, restart and stop Memcached service.

 sudo systemctl start memcached.service
 sudo systemctl restart memcached.service
 sudo systemctl stop memcached.service

Step #2: Configure Memcached on Ubuntu

Next is to configure Memcached, By default Memcached listen on localhost, If you want to change open Memcached config file  /etc/memcached.config

 sudo vi /etc/memcached.config

change your server private or public IP as shown below

# memcached default config file
# 2003 - Jay Bonci <[email protected]>
# This configuration file is read by the start-memcached script provided as
# part of the Debian GNU/Linux distribution.

# Run memcached as a daemon. This command is implied, and is not needed for the
# daemon to run. See the README.Debian that comes with this package for more
# information.
-d

# Log memcached's output to /var/log/memcached
logfile /var/log/memcached.log

# Be verbose
# -v

# Be even more verbose (print client commands as well)
# -vv

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache

# 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 192.168.10.15

# Limit the number of simultaneous incoming connections. The daemon default is 1024
# -c 1024

# Lock down all paged memory. Consult with the README and homepage before you do this
# -k

# Return error when memory is exhausted (rather than removing items)
# -M

# Maximize core file limit
# -r

Restart the Memcached Service to take effect

 sudo systemctl restart memcached.service

Step #3: Install Memcached PHP Module

If you have not installed php on your ubuntu system, follow below commands to install latest php.

Skip this step if you have already installed

 sudo add-apt-repository ppa:ondrej/php
 sudo apt-get update
 sudo apt-get install -y php php-dev php-pear libapache2-mod-php

OR

sudo apt-get install apache2 php7.2 libapache2-mod-php7.2 php-memcached php7.2-cli

Use below command to install PHP Memcached on your system

 sudo apt-get install php-memcached -y

Reload or Restart the Apache service to take effect

 sudo systemctl reload apache2

OR

 sudo systemctl restart apache2

Lest test and verify if Memcached is installed and memcached php extension is enabled, create a info.php file in /var/www/html directory

 sudo nano /var/www/html/info.php

paste the below lines into it and save the file.

<?php
phpinfo();
?>

open your favorite browser and type below your server name or IP followed by info.php.

http://localhost/info.php

Step #4: Add Firewall Rule

Allow port 11211/tcp in Firewall to Access Memcached from remotely

 sudo ufw allow 11211

Verify if port is enables

 sudo ufw status

If you have installed Memcached on Cloud Instance Allow the Port in Incoming or Network Security Group

Conclusion:

In this article, We have performed ,How to Install Memcached on Ubuntu 20.04 LTS System.

Related Articles:

How to Install Memcached on Ubuntu 18.04/16.04 LTS

How to Install MariaDB on Ubuntu 18.04/16.04 LTS

How to Install phpMyAdmin on Ubuntu 18.04/16.04

How to Install PHP on Ubuntu 20.04 LTS

Reference

Memcached Official Page

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

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

Share via
Copy link