Memcached is a free, open-source, powerful distributed memory object caching system. It is generally used to speed up dynamic database-driven websites by caching data and objects in RAM on key-value basis. Today, we’ll show you how to install Memcached on Ubuntu & setup properly.
There are a few ways you can install Memcached. Depending on which system you’re using, the method will vary. If you are using Ubuntu or Debian then you can follow these steps.
Read our step by step guide on how to set up a cloud VPS on digitalocean & how to set up the LAMP server before installing Memcached. If you are using a low memory server then follow our guide on how to set up swap on Ubuntu.
How to Install Memcached on Ubuntu 18.04
Follow our step by step Memcached installation guide:
Step 1: Install Memcached
First of all, Update the package of your ubuntu server and then install Memcached by below command
sudo apt update && sudo apt install memcached
Step 2: Install Memcached PHP Module
To work with the Memcached server from your PHP scripts, you need to install the Memcached PHP module. We can also install libmemcached-tools a library that provides several tools to work with your Memcached server.
sudo apt install php-memcached
sudo apt install libmemcached-tools
Memcached is now installed as a service on your server. Now time to configure Memcached properly.
Step 3: Configure Memcached
/etc/memcached.conf file in edit mode and check
-l 127.0.0.1 line is available in this file. If not then add this single line of code in this file. It’ll help to prevent denial of service attacks from the network.
sudo nano /etc/memcached.conf
To inspect the interface setting, find the following line in the file:
It is also a good idea to disable UDP, as it is more likely to be exploited in denial of service attacks (DDoS). Add the following option to the bottom of this file to disable UDP (TCP will unaffected).
Step 4: Restart Memcached
After completing the installation, you must restart the Apache service & Memcached to take effect immediately.
sudo systemctl restart memcached
sudo service apache2 restart
Now verify that Memcached is currently bound to the local interface and listening only for TCP connections by below command
sudo netstat -tlpn
Check if the Memcache PHP extension is enabled and working properly. Create an info.php file.
sudo nano /var/www/html/info.php
and add the below codes into the file & save the file.
<?php phpinfo(); ?>
Now access info.php on any browser and search for Memcache, You will get the result like below.
In this guide, we have covered how to properly installed & set up Memcached database caching on Ubuntu. To learn more about Memcached, check out the Memcached project documentation.
If you like this article then, please share this article or comment below if you have any questions.