Memcached stands for “memory object caching systems”, is an open-source and high-performance distributed memory caching system used to speed up dynamic database-driven web applications. It caches data in memory that are generated from page load requests or API calls. Memcached is very useful for database query results caching, session caching, web page caching, API caching, and caching of objects such as images, files, and metadata. You can use Memcached in PHP-based applications including WordPress and Joomla to run smoothly without much latency.
In this tutorial, I will show you how to install Memcached memory caching system on Alma Linux 8.
- A server running Alma Linux 8.
- A root password is configured on the server.
Install Memcached on Alma Linux 8
By default, the Memcached package is included in the Alma Linux default repository. You can install it by running the following command:
dnf install memcached libmemcached -y
After installing Memcached, you can see the installed package information using the following command:
rpm -qi memcached
You will get the following output:
Name : memcached Epoch : 0 Version : 1.5.22 Release : 2.el8 Architecture: x86_64 Install Date: Saturday 26 March 2022 04:10:38 AM 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.
Now, start and enable the Memcached service using the following command:
systemctl enable memcached --now
To check the status of the Memcached use the following command:
systemctl status memcached
You should see the following output:
? memcached.service - memcached daemon Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2022-03-26 04:11:19 UTC; 8s ago Main PID: 3698 (memcached) Tasks: 10 (limit: 11412) Memory: 3.7M CGroup: /system.slice/memcached.service ??3698 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 127.0.0.1,::1 Mar 26 04:11:19 linux systemd: Started memcached daemon.
By default, Memcached listens on port 11211, you can check it using the following command:
ps -ef | grep memcached
You should see the following output:
memcach+ 3698 1 0 04:11 ? 00:00:00 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 127.0.0.1,::1 root 3712 1123 0 04:11 pts/0 00:00:00 grep --color=auto memcached
Memcached main configuration file is located at /etc/sysconfig/memcached. You can configure it as shown below:
Change the following lines as 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
If you are using firewalld on your system, then you will need to allow ports 11211 and 80 through the firewalld. You can allow them by running the following command:
firewall-cmd --add-port=11211/tcp --zone=public --permanent firewall-cmd --add-port=80/tcp --zone=public --permanent
Next, reload the firewalld to apply the changes:
You can now list all firewalld ports using the following command:
Install PHP with Memcached Support
If you want to integrate Memcached with a PHP-based application, then you need to install the Memcached extension for PHP.
First, install the EPEL and Remi repo using 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 using the following command:
dnf module enable php:remi-7.4 -y
Next, install the Memcached extension for PHP using the following command:
dnf install php-pecl-memcache php-pecl-memcached -y
Once all the packages are installed, you can proceed to the next step.
Verify Memcached for PHP<
Next, you will need to install the Nginx web server and create a sample PHP page to verify the Memcached. You can install Nginx and other PHP packages using the following command:
dnf install nginx php php-cli -y
Next, create an info.php page:
Add the following lines:
<?php phpinfo(); ?>
Save and close the file then create a symbolic link of the PHP page to the Nginx default web root directory:
ln -s /var/www/html/info.php /usr/share/nginx/html/
Next, start and enable the Nginx service to apply the changes:
systemctl start nginx systemctl enable nginx
Now, open your web browser and access the URL http://your-server-ip/info.php. You should see Memcached on the following page:
Congratulations! you have successfully installed Memcached on Alma Linux 8. You can now use Memcached as a caching database to speed up your PHP-based website and applications. Feel free to ask me if you have any questions.