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.
Step 1.2: Choose a Location
- Select the data center closest to your target audience (e.g., New York, London, Tokyo).
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.
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.
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.
Step 2: Install Memcached
Install Memcached and its tools by running the following command:
apt install memcached libmemcached-tools -y
Step 3: Start and Verify Memcached
Start Memcached and check its status to make sure it’s running:
systemctl start memcached
systemctl status memcached
Check the installed version of Memcached:
memcached -V
To confirm that Memcached is listening on the default port (11211), run:
ss -plunt | grep memcache
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
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
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
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
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();
}
?>
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.
Next, reload the page again. This time the page load from the cache memory as shown below:
Step 8: Enable SASL Authentication for Memcached
8.1: Install SASL
To secure Memcached with authentication, install the SASL libraries:
apt install sasl2-bin
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
Add the following lines to configure SASL:
log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2
8.3: Set Up SASL Users
Create a user for Memcached:
saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 user1
Change ownership of the SASL database file:
chown memcache:memcache /etc/sasl2/memcached-sasldb2
To check if Memcached is working properly, you can run the following command:
memcstat --servers="127.0.0.1"
Step 9: Configure Memcached for SASL
Edit the Memcached configuration file to enable SASL and verbosity:
nano /etc/memcached.conf
Add the following lines:
-S
-vv
Restart Memcached for the changes to take effect:
systemctl restart memcached
Step 10: Verify SASL Authentication
Check the logs to ensure SASL is working:
journalctl -u memcached | grep SASL
To verify that Memcached is using SASL, run:
memcstat --servers="127.0.0.1" --username=user1 --password=password
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.