Memcached is a high-performance memory object caching system that is used to speed up dynamic web applications by caching data and objects in RAM. WHM/cPanel is a popular web hosting control panel used to manage web hosting services. In this tutorial, we will explain how to install, optimize, and harden Memcached with WHM/cPanel on CentOS 7.
Step 1: Install Memcached on CentOS 7
The first step is to install Memcached on your CentOS 7 server. To do this, SSH into your server and run the following command:
sudo yum install memcached
Step 2: Configure Memcached
Once Memcached is installed, you will need to configure it to work with WHM/cPanel. To do this, you will need to edit the /etc/sysconfig/memcached file. Open the file using your favorite text editor and add the following line:
PORT="11211"
Save and close the file.
Step 3: Start and Enable Memcached
Next, start Memcached and enable it to start automatically on system boot by running the following commands:
sudo systemctl start memcached
sudo systemctl enable memcached
Step 4: Optimize Memcached
To optimize Memcached, you will need to modify the /etc/sysconfig/memcached file again. Open the file and add the following line:
OPTIONS="-l 127.0.0.1 -t 8 -m 64"
Here, we are setting the Memcached cache size to 64MB (-m 64/ For WordPress We Use 128-1024 Depending On The Size Of The Site), the number of threads to 8 (-t 8/The More vCPU or CPU Cores You Have The Higher You Can Take This), and restricting access to the local server only (-l 127.0.0.1/Only In Advanced Installations With Multi-Server Deployments Would This Be Anything Different). You can adjust these values based on your specific needs.
Step 5: Harden Memcached
By default, Memcached is not secure and can be vulnerable to attacks. To harden Memcached, you will need to create a firewall rule to restrict access to the Memcached port (11211) to trusted IP addresses only. You can do this using WHM/cPanel’s built-in firewall tool, CSF.
To add a firewall rule, log in to WHM and navigate to the CSF Firewall page. Click on “Firewall Configuration” and scroll down to the “IPv4 Port Settings” section. Add the following line to the “Custom TCP_IN” field:
tcp, dport=11211, s=trusted.ip.address
Replace “trusted.ip.address” with the IP address of the trusted client that should have access to Memcached.
Conclusion
In this tutorial, we have explained how to install, optimize, and harden Memcached with WHM/cPanel on CentOS 7. By following these steps, you can ensure that your Memcached installation is secure and optimized for performance.