install memcache in apache

How to Install memcached in Apache

Memcached is a powerful caching system that improves website speed and performance. It can be used with PHP, Apache, NGINX and other popular platforms. Here are the steps to install memcached in Apache for Ubuntu, CentOS. You can also use them to install memcached in WordPress, Drupal, Magento.


How to Install memcached in Apache

We will look at how to install memcached in Ubuntu as well as CentOS. The following steps will also enable memcached after installation.


Installing memcached in Ubuntu

Open terminal and run the following command to update packages

$ sudo apt-get update

Install memcached and its dependencies

$ sudo apt-get install php-memcached memcached

Restart Apache web server to apply changes

$ sudo service restart apache2

Bonus Read : How to Install mod_session in Ubuntu

Installing memcached in CentOS

Open terminal and run the following commands to install memcached and its dependencies.

# yum clean all 
​# yum -y update ​
# yum -y install memcached php-memcached

This will install memcached but not enable it to run on startup.

systemctl start memcached ​
systemctl enable memcached ​
systemctl status memcached

Restart Apache web server to apply changes.

sudo systemctl restart httpd.service

Bonus Read : How to Disable mod_security in Apache


Verify memcache

To ensure that memcached is installed and enabled, create a test file /var/www/html/test.php with the following contents.

<?php 
$mem = new Memcached(); 
$mem->addServer("127.0.0.1", 11211); 

$result = $mem->get("blah"); 

if ($result) { 
    echo $result; 
} else { 
   echo "No matching key found yet"; 
   $mem->set("blah", "Data from memcached") 
    or die("Couldn't save anything to memcached..."); 
} 
?>

Save and close the file. Open browser and go to http://IP_OF_SERVER/test.php

Replace IP_OF_SERVER with your server’s IP or domain name.

First time, you will see the message “No matching key found yet”. Refresh your page on browser. This time, you will see the message “Data from memcached”

Hopefully, the above article will help you install and configure memcached in Apache for Ubuntu as well as CentOS.

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!