
Memcached is a free, distributed memory caching solution that temporarily stores data and objects—like frequent database queries, API responses, session data, or computations—in RAM. It integrates with modern applications to reduce repeated operations such as database access, helping boost response times and reduce server load.
This article walks you through installing Memcached on Ubuntu 20.04 and configuring it to use SASL (Simple Authentication and Security Layer) for secure client connections.
Prerequisites
Before you begin:
Have an Ubuntu 20.04 server.
Access the server using SSH as a non-root user with sudo privileges.
Install Memcached
Memcached is included in Ubuntu 20.04's default package repositories and can be installed using the APT package manager. Alternatively, you can compile a specific version from source if needed. The following instructions cover installing Memcached and enabling it to run on your server.
Update the server package index.
console$ sudo apt update
Install Memcached and required add-on tools.
console$ sudo apt install memcached libmemcached-tools -y
View the installed Memcached version on your server.
console$ memcached --version
Output:
memcached 1.5.22
Enable the Memcached service to automatically start at boot time.
console$ sudo systemctl enable memcached
Start the Memcached service.
console$ sudo systemctl start memcached
Configure Memcached
Memcached uses the /etc/memcached.conf
file for its configuration settings. Within this file, you can define options such as the port number, memory allocation, connection limits, and the IP address the service listens on. The following steps guide you through configuring these settings and confirming that Memcached is running correctly on your server.
Open the Memcached configuration file
/etc/memcached.conf
using a text editor such as Nano.console$ sudo nano /etc/memcached.conf
Add
-S
at the end of the file after-P /var/run/memcached/memcached.pid
to enable SASL authentication on your server.ini... -P /var/run/memcached/memcached.pid -S
Find and uncomment the
-v
directive to enable verbose logging to the/var/log/memcache
file.ini-v
Uncomment the
-c 1024
directive to limit the number of simultaneous Memcached connections. Replace1024
with your desired number of connections.ini-c 1024
Find the following Memcached port directive to verify the application connection port on your server.
ini# Default connection port is 11211 -p 11211
Locate the
-l
directive and ensure it is set to127.0.0.1
to restrict Memcached access to local connections only. If you intend to allow remote access, such as from a standalone Memcached server, update this value to your server’s public IP or Vultr VPC address.ini-l 127.0.0.1 -l ::1
Save and close the file.
Restart Memcached to apply the configuration changes.
console$ sudo systemctl restart memcached
Secure Memcached
By default, Memcached does not use any authentication, which leaves it vulnerable to unauthorized access. To secure it, you can enable the SASL protocol, which enforces user authentication before allowing access. The steps below guide you through installing the SASL package to add this layer of security to your Memcached server.
Install the SASL package on your server.
console$ sudo apt install sasl2-bin -y
Create a new directory to store your SASL authentication credentials.
console$ sudo mkdir /etc/sasl2
Create a new SASL configuration file to use with Memcached. For example,
memcached.conf
.console$ sudo nano /etc/sasl2/memcached.conf
Add the following contents to the file.
inilog_level: 5 mech_list: plain sasldb_path: /etc/sasl2/memcached-sasldb2
Save and close the file
The SASL configuration above sets up authentication for Memcached. Here's what each directive does:
log_level
: Activates logging, with the value5
enabling detailed logs.mech_list
: Defines the authentication method, whereplain
allows plain username and password authentication.sasldb_path
: Specifies the location of the Memcached SASL database used for authentication.
Create a user using the
saslpasswd
utility. Replaceexample-user
with your actual username to enable it in your Memcached database.console$ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example-user
Enter a strong password for the new user when prompted.
Grant the Memcached user
memcache
full privileges to the/etc/sasl2/memcached-sasldb2
database file.console$ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2
Restart Memcached to apply the configuration changes.
console$ sudo systemctl restart memcached
Check the status of Memcached server.
console$ sudo systemctl status memcached
List all the created user in SASL authentication database.
console$ sudo sasldblistusers2 -f /etc/sasl2/memcached-sasldb2
Output:
example-user@ubuntu20: userPassword
Connect to Memcached to test your new user credentials. Replace
example-user@ubuntu20
andstrong-password
with your actual user details.console$ memcstat --binary --servers="127.0.0.1" --username='example-user@ubuntu20' --password=strong-password
Your output should look like the one below when successful:
Server: 127.0.0.1 (11211) pid: 5090 uptime: 570 time: 1743878810 version: 1.5.22 libevent: 2.1.11-stable pointer_size: 64 rusage_user: 0.052240 rusage_system: 0.043533 max_connections: 1024 curr_connections: 2 total_connections: 10 rejected_connections: 8
Connect to Memcached
Memcached can be used with various application frameworks like PHP, Perl, Python, Ruby, and Java. To connect and test Memcached with PHP, follow these steps.
Install PHP and the Memcached module.
console$ sudo apt install php php-memcached -y
Create a new sample PHP script to connect to Memcached.
console$ nano memcached.php
Add the following contents to the file. Replace
example-user@ubuntu20
andstrong-password
with your actual user credentials.php<?php $memcached = new Memcached(); $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); $memcached->addServer('127.0.0.1', 11211); $memcached->setSaslAuthData('example-user@ubuntu20', 'strong-password'); // Set and retrieve a value to test the connection $memcached->set('example', 'Greetings from Vultr!'); echo $memcached->get('example'); ?>
Save and close the file.
The PHP script above connects to Memcached using the provided example user details and the binary protocol. It creates a new key,
example
, with the valueGreetings from Vultr!
, which is then stored in the Memcached memory and retrieved through the$memcached
variable in the application.Run the script using PHP to test the connection to Memcached.
console$ php memcached.php
Output:
Greetings from Vultr!
Based on the above output, the PHP script successfully connects to Memcached, creates the
example
key and writes a new valueGreetings from Vultr
to retrieve from memory.
Conclusion
You have successfully installed Memcached on an Ubuntu 20.04 server and configured it for secure integration with application frameworks like PHP. Memcached enhances server performance by storing frequently accessed data, such as database queries, in memory.
No comments yet.