Memcached is an open-source, high-performance, superfast in-memory key-value store and caching service. It is used to speed up web applications by caching session data, user authentication tokens, and API calls. It also helps when sharing a large amount of data across multiple application instances. It is used by some major companies including Facebook, Youtube, and Twitter. Memcached is multithreaded and scales vertically.

In this post, we will show you how to install Memcached on Rocky Linux 8.

Step 1 – Install Memcached on Rocky Linux 8

By default, the Memcached package is included in the Rocky Linux 8 default repo. You can install it by just running the following command:

dnf install memcached libmemcached -y

Once the Memcached is installed, you can see the detailed information of Memcached with the following command:

rpm -qi memcached

Sample output:

Name        : memcached
Epoch       : 0
Version     : 1.5.22
Release     : 2.el8
Architecture: x86_64
Install Date: Friday 29 October 2021 01:44:50 PM UTC
Group       : System Environment/Daemons
Size        : 414743
License     : BSD
Signature   : RSA/SHA256, Monday 12 April 2021 07:04:12 AM UTC, Key ID 15af5dac6d745a60
Source RPM  : memcached-1.5.22-2.el8.src.rpm
Build Date  : Monday 12 April 2021 04:45:42 AM UTC
Build Host  : ord1-prod-x86build003.svc.aws.rockylinux.org
Relocations : (not relocatable)
Packager    : [email protected]
Vendor      : Rocky
URL         : https://www.memcached.org/
Summary     : High Performance, Distributed Memory Object Cache
Description :
memcached is a high-performance, distributed memory object caching
system, generic in nature, but intended for use in speeding up dynamic
web applications by alleviating database load.

Step 2 – Manage Memcached Service

You can manage the Memcached service easily via systemd. To start the Memcached service, run the following command:

systemctl start memcached

To enable the Memcached service to start after the reboot, run the following command:

systemctl enable memcached

To check the status of the Memcached service, run the following command:

systemctl status memcached

Sample output:

● memcached.service - memcached daemon
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-10-29 13:45:33 UTC; 4s ago
 Main PID: 10526 (memcached)
    Tasks: 10 (limit: 11411)
   Memory: 3.6M
   CGroup: /system.slice/memcached.service
           └─10526 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 127.0.0.1,::1

Oct 29 13:45:33 RockyLinux8 systemd[1]: Started memcached daemon.

Step 3 – Configure Memcached

The Memcached default configuration file is located at /etc/sysconfig/memcached. You can edit it with the following command:

nano /etc/sysconfig/memcached

The default configuration is shown below. You can change it per your requirements:

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l 127.0.0.1,::1"

Save and close the file then restart the Memcached service to apply the changes:

systemctl restart memcached

Step 4 – Integrate Memcached with PHP-Based Applications

You can use Memcached as a caching service for all PHP based applications. You can do it by installing the Memcached extension for PHP.

First, install the EPEL and Remi repository with the following command:

dnf install epel-release -y
dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y

Next, disable the default PHP repo and enable the Remi PHP repo with the following command:

dnf module list reset php
dnf module enable php:remi-7.4 -y

Next, install the Memcached PHP extensions using the following command:

dnf install php-pecl-memcache php-pecl-memcached -y

Step 5 – Verify Memcached for PHP

To verify the Memcached integration with PHP, you will need to install the Nginx web server and PHP package to your server.

dnf install nginx php php-cli -y

Next, create a info.php file with the following command:

nano /var/www/html/info.php

Add the following code:

<?php
phpinfo();
?>

Save and close the file, then create a symbolic link to the info.php file in the Nginx default web root directory:

ln -s /var/www/html/info.php /usr/share/nginx/html/

Next, restart the Nginx service to apply the changes:

systemctl start nginx

Now, open your web browser and type the URL http://your-server-ip/info.php. You should see the following page:
Memcached PHP verification
As you can see, both Memcache and Memcached PHP extensions are enabled.

Conclusion

In the above post, you learned how to install the Memcached service on Rocky Linux 8. You also learned how to integrate Memcached with PHP applications. You should now be able to use Memcached to speed up your web applications – give it a try on VPS hosting from Atlantic.Net!