Memcached is an open-source and high-performance in-memory caching system. Generally, it is used to speed up web applications by caching session data, user authentication tokens, and API calls. Its aim is to accelerate dynamic web applications by alleviating database load.  Memcached reduces application load by storing data objects in dynamic memory.

In this tutorial, we will explain how to install Memcached on CentOS 8.

Step 1 – Install Memcached

By default, Memcached is available in the CentOS 8 default repository. You can install it by just running the following command:

dnf update -y
dnf install memcached libmemcached -y

After installing Memcached, start the Memcached service and enable it to start at boot with the following command:

systemctl start memcached
systemctl enable memcached

You can also verify the status of the Memcached service with the following command:

systemctl status memcached

You should see the following output:

memcached.service - memcached daemon
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-08-07 06:30:57 EDT; 10s ago
 Main PID: 9933 (Memcached)
    Tasks: 10 (limit: 12537)
   Memory: 3.6M
   CGroup: /system.slice/memcached.service
           └─9933 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 127.0.0.1,::1

Aug 07 06:30:57 centos8 systemd[1]: Started memcached daemon.

Step 2 – Configure Memcached

Memcached default configuration file is located at /etc/sysconfig/memcached. By default, Memcached is configured to listen on the localhost. If your application is hosted on the same server, then you don’t need to configure Memcached. If your application is hosted on the remote server, then you will need to configure Memcached to listen on that IP address and allow access to the Memcached port 11211 only from the client IP address.

To see the Memcached default configuration, run the following command:

cat -n /etc/sysconfig/memcached

You should see the following output:

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

If you want to configure Memcached for a remote application, you need to replace the 127.0.0.1 with the IP address of the remote system.

Step 3 – Integrate Memcached with PHP

If you want to use Memcached as a caching database for your PHP application, then you will need to install the Memcached extension for PHP.

First, you will need to install the EPEL and Remi repository in your system. You can install them by running the following command:

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

Next, enable the PHP Remi repository with the following command:

dnf module enable php:remi-7.4 -y

Next, install the PHP Memcached extension with the following command:

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

Once you are finished, you can proceed to the next step.

Step 4 – Verify Memcached

Next, install the Nginx web server and create a sample PHP page to test whether Memcached is enabled for PHP or not.

First, install Nginx with PHP by running the following command:

dnf install nginx php php-cli -y

Once installed, create a sample info.php page:

nano /var/www/html/info.php

Add the following lines:

<?php
phpinfo();
?>

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

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

Next, start the Nginx server and enable it to start at boot with the following command:

systemctl start nginx
systemctl enable nginx

Now, open your web browser and type the URL http://your-server-ip/info.php. You should see the Memcached section in the following screen:

Conclusion

Congratulations! You have successfully installed and configured Memcached on CentOS 8. You can now integrate your web application with Memcached and use its features to speed up your website on VPS hosting from Atlantic.Net. For more information, visit the Memcached documentation.