Memcached is a high-performance, distributed memory object caching system that helps speed up web applications by caching data in memory to reduce the load on databases. In this tutorial, you will learn how to install and configure Memcached on Ubuntu 22.04, integrate it with PHP, and secure it with SASL authentication.

What You Can Do with Memcached:

  • Cache Database Queries: Reduce database load by caching frequently accessed queries.
  • Speed Up Web Applications: Improve the performance of dynamic websites by storing data in memory.
  • Manage Sessions: Use Memcached to store and manage PHP session data.

Step 1: Create a Cloud Instance

Before installing Memcached, you need to create a cloud instance where it will run. Here’s how to set up your instance:

Step 1.1: Click on “Create”

  • From your hosting provider’s dashboard, click on the Create button and select Instances.
Shape.host Resources dashboard with Create > Instance highlighted.

Step 1.2: Choose a Location

  • Select the data center closest to your target audience (e.g., New York, London, Tokyo).
Shape.host instance form with the data center location selector highlighted.

Step 1.3: Choose an Instance Plan and OS

  • Choose an instance plan based on your needs (e.g., Standard, CPU-Optimized).
  • Select Ubuntu 22.04 as the operating system.
Shape.host instance form with the operating system image selector highlighted.

Step 1.4: Create the Instance

  • Fill in the necessary details, such as authentication (SSH keys or password), and choose a hostname. Click Create Instance.
Shape.host instance form with the Create Instance button highlighted.

Step 1.5: Find Your IP Address

  • Once the instance is created, go to the Resources section to find the public IP address of your instance. You’ll need this to connect to the instance remotely.
Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Install Memcached

Install Memcached and its tools by running the following command:

apt install memcached libmemcached-tools -y
Terminal showing APT package management commands and output.

Step 3: Start and Verify Memcached

Start Memcached and check its status to make sure it’s running:

systemctl start memcached
systemctl status memcached
Terminal showing systemd service management commands or output.
Terminal showing systemd service management commands or output.

Check the installed version of Memcached:

memcached -V
Terminal displaying the Memcached version.

To confirm that Memcached is listening on the default port (11211), run:

ss -plunt | grep memcache
Terminal showing ss commands or output during Start and Verify Memcached.

Step 4: Install Apache, PHP, and PHP-Memcached

To integrate Memcached with PHP, install Apache, PHP, and the necessary PHP extensions:

apt install apache2 php libapache2-mod-php php-memcached php-cli -y
Terminal showing APT package management commands and output.

Step 5: Test PHP and Memcached Integration

5.1: Create a PHP Info File

To verify PHP is working correctly, create a PHP file in /var/www/html:

nano /var/www/html/info.php
Terminal showing nano commands or output during Create a PHP Info File.

Add the following code:

<?php
   phpinfo();
?>

Save and close the file.


Step 6: Restart Apache

After creating the PHP info page, restart Apache to ensure everything is working correctly:

systemctl restart apache2
Terminal showing systemd service management commands or output.

Step 7: Test Memcached with a PHP Script

Next, create a PHP test script to verify Memcached is functioning correctly:

nano /var/www/html/test.php
Terminal showing systemd service management commands or output.

Add the following code to test Memcached:

<?php

try

{
$memcached = new Memcached();
$memcached->addServer("127.0.0.1", 11211); 
$response = $memcached->get("key_cache");

if($response==true) 
{
echo "Result coming from caching";
echo $response;
}

else

{
echo "Cache is not created yet, reload again to see changes";
$memcached->set("key_cache", "Hooray! Memcache is working now ...") ;
}
}
catch (exception $e)
{
echo $e->getMessage();
}
?>
Terminal showing the GNU nano editor and configuration text.

Save and close the file.

7.1: Test in the Browser

Open your web browser and go to http://<your-server-ip>/test.php. If everything is set up correctly, you should see output indicating whether the result is being fetched from the cache.

Browser displaying the first-run message that the Memcached test cache has been created.

Next, reload the page again. This time the page load from the cache memory as shown below:

Browser displaying the cached Memcached test result.

Step 8: Enable SASL Authentication for Memcached

8.1: Install SASL

To secure Memcached with authentication, install the SASL libraries:

apt install sasl2-bin
Terminal showing APT package management commands and output.

Create the necessary configuration directory:

mkdir -p /etc/sasl2

8.2: Configure SASL for Memcached

Create a SASL configuration file for Memcached:

nano /etc/sasl2/memcached.conf
Terminal showing nano commands or output during Configure SASL for Memcached.

Add the following lines to configure SASL:

log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2
Terminal showing the GNU nano editor and configuration text.

8.3: Set Up SASL Users

Create a user for Memcached:

saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 user1
Terminal showing SASL user creation prompts for Memcached.

Change ownership of the SASL database file:

chown memcache:memcache /etc/sasl2/memcached-sasldb2
Terminal showing file ownership or permission commands.

To check if Memcached is working properly, you can run the following command:

memcstat --servers="127.0.0.1"
Terminal displaying Memcached server statistics.

Step 9: Configure Memcached for SASL

Edit the Memcached configuration file to enable SASL and verbosity:

nano /etc/memcached.conf
Terminal displaying Memcached statistics and a configuration editing command.

Add the following lines:

-S
-vv
Terminal showing the GNU nano editor and configuration text.

Restart Memcached for the changes to take effect:

systemctl restart memcached
Terminal showing systemd service management commands or output.

Step 10: Verify SASL Authentication

Check the logs to ensure SASL is working:

journalctl -u memcached | grep SASL
Terminal showing journalctl commands or output during Verify SASL Authentication.

To verify that Memcached is using SASL, run:

memcstat --servers="127.0.0.1" --username=user1 --password=password
Terminal showing a Memcached SASL authentication test command.

If successful, you should see statistics indicating that Memcached is functioning properly with SASL authentication enabled.

You have successfully installed and configured Memcached on Ubuntu 22.04, integrated it with PHP, and secured it with SASL authentication. Memcached is now ready to cache your web application’s data and enhance its performance.

To ensure high performance and security for your web applications, consider using Shape.Host’s Linux SSD VPS services. Shape.Host offers SSD-powered VPS solutions that provide optimal performance for caching systems like Memcached, ensuring your applications run smoothly.