How to Install the Memcached PHP Extension in cPanel
In this article, we will walk through the steps for installing the Memcached PHP extension in cPanel, covering specific commands for CentOS and Debian systems, as well as how to verify if the installation was successful. Installing the Memcached extension can significantly optimize cache performance for WordPress and tools like Cachify, ultimately improving website speed.
Introduction to Memcached
Memcached is a popular caching service that can be used as an object cache backend for WordPress or as a storage solution for Cachify’s static cache. After setting up Memcached on the server, you will also need to install the Memcached PHP extension to enable PHP scripts to connect to the Memcached service.
How to Install the Memcached PHP Extension in cPanel
To install the Memcached PHP extension on a cPanel server, you can easily do so via the command line.
For CentOS-based Linux Systems:
Run the following command to install the Memcached PHP extension:
yum install ea-phpXX-php-memcached
Make sure to replace XX
with your PHP version number (for example, ea-php72-php-memcached
for PHP 7.2).
For Debian-based Systems:
On Debian or Ubuntu, use the following command to install the extension:
apt-get install ea-phpXX-php-memcached
Again, replace XX
with the PHP version number that you are using (for example, ea-php72-php-memcached
for PHP 7.2).
Restart PHP-FPM
After installation is complete, restart PHP-FPM to apply the changes. Use the following command to restart PHP-FPM:
/scripts/restartsrv_apache_php_fpm --status
This will restart the PHP process and make sure that the Memcached extension is active.
Installation in LNMP or BT Panel Environments
If you are using an LNMP (Linux, Nginx, MySQL, PHP) stack or a BT Panel (宝塔面板) environment, both of these platforms provide easy-to-use tools to install PHP extensions. You can simply use the provided tools in the panel to install the Memcached PHP extension.
Verifying the Installation
Once you’ve completed the installation steps, you can create a simple PHP script to verify whether Memcached is working correctly.
Create a PHP file with the following code:
$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
if ($memcache->set('test_key', 'Hello World')) {
echo "Connection successful, data written successfully.\n";
echo "Read data: " . $memcache->get('test_key');
} else {
echo "Connection failed";
}
When you run the script, if the message “Connection successful, data written successfully.” is displayed, it means that PHP can successfully connect to the Memcached service. You can now start using Memcached to optimize and speed up your WordPress site.
Conclusion
Installing the Memcached PHP extension on a cPanel server is a simple process that can help optimize your website’s performance by enhancing caching. By following these steps, you’ll have Memcached up and running for WordPress and other applications. Whether you’re using CentOS, Debian, or an LNMP/BT Panel setup, this guide will help you leverage the power of Memcached to speed up your site.