How to Install Memcached on CentOS 8

Memcached is an open-source and high-performance memory caching system. It stores data in memory and is being used for optimizing dynamic websites. It speed up your database-driven website by caching objects in memory. Memcached is the first choice of any webmaster to host real-time applications in Web, Mobile Apps, Gaming and E-Commerce. In this guide, I will explain how to install Memcached in-memory caching system on CentOS 8.

Prerequisites

  • A server running CentOS 8.
  • A root password is configured the server.

Installing Memcached Server

Installing the Memcached package is a simple process. You can install it by just running the following command:

dnf install memcached libmemcached -y

Once the Memcached server is installed, start and enable the Memcached server at system reboot:

systemctl enable memcached --now

To verify the Memcached service, run the following command:

systemctl status memcached

Output:

Memcached Status

Configuring Memcached

By default, Memcached is accessible only from the localhost. If your application is hosted on the remote system then you will need to configure Memcached to allow access to port 11211 from the application server's IP.

You can allow a remote server to access the Memcached by editing the file /etc/sysconfig/memcached:

nano /etc/sysconfig/memcached

Find the following line:

OPTIONS="-l 127.0.0.1,::1"

And, replaced it with the following line:

OPTIONS="-l application-server-ip,::1"

Save and close the file after you have finished. Then, restart the Memcached service to apply the changes:

systemctl restart memcached

Configuring Firewall

Next, you will need to configure the firewall to allow TCP and UDP port 11211 and allow access from your remote application server IP.

You can allow them with the following command:

firewall-cmd --zone=public --add-port=11211/udp --permanent
firewall-cmd --zone=public --add-port=11211/tcp --permanent
firewall-cmd --zone=public --add-source=application-server-ip/32 --permanent

Next, reload the firewalld service to apply the changes:

firewall-cmd --reload

Installing Memcached Extensions

In order to connect to the Memcached server, you will need to install a language-specific client.

If you want to use Memcached for your PHP application. You will need to install the php-pecl-memcached extension to your server.

You can install it with the following command:

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

If you want to use Memcached for your Python application. You will need to install pymemcache to your server.

You can install it with the following command:

pip install pymemcache
pip install python-memcached

Conclusion

Congratulations! you have successfully installed Memcached on CentOS 8. You can now integrate Memcached with your PHP or Python-based application and increase the speed of your application.

Share this page:

0 Comment(s)