Memcached is a free, distributed memory caching system that stores frequently accessed data and objects—such as database queries, API responses, session information, or computation results—in RAM. By reducing repetitive operations like database lookups, it helps improve application performance and lower server load.
This guide explains how to install Memcached on Ubuntu 20.04 and configure it to use SASL (Simple Authentication and Security Layer) for secure client connections.
Prerequisites
- An Ubuntu 20.04 server
- SSH access as a non-root user with sudo privileges
- Ensure the server is updated
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Install Memcached
Memcached is available in Ubuntu 20.04’s default package repositories and can be installed using APT. You can also compile a specific version from source if needed. The following steps describe installing Memcached and enabling it to run automatically on your server.
Update the package index
$ sudo apt updateInstall Memcached and required tools
$ sudo apt install memcached libmemcached-tools -yCheck the installed Memcached version
$ memcached --versionExample output:
memcached 1.5.22Enable Memcached to start at boot
$ sudo systemctl enable memcachedStart the Memcached service
$ sudo systemctl start memcachedConfigure Memcached
The configuration file for Memcached is /etc/memcached.conf. Here you can define options such as port number, memory size, connection limits, and the IP address that Memcached listens on. Follow these steps to adjust the settings and confirm that Memcached is running properly.
Edit the configuration file
$ sudo nano /etc/memcached.confEnable SASL authentication
Add -S at the end of the file after -P /var/run/memcached/memcached.pid to enable SASL:
...
-P /var/run/memcached/memcached.pid
-SEnable verbose logging
-vLimit simultaneous connections
-c 1024Check the Memcached port
# Default connection port is 11211
-p 11211Restrict or allow remote access
Ensure the -l directive is set to 127.0.0.1 to limit access to local connections. To allow remote access, update it to your server’s public IP:
-l 127.0.0.1
-l ::1Apply configuration changes
$ sudo systemctl restart memcachedSecure Memcached with SASL
By default, Memcached does not enforce authentication. Enabling SASL ensures that only authenticated users can access it. Follow these steps to add this security layer.
Install the SASL package
$ sudo apt install sasl2-bin -yCreate a directory for SASL credentials
$ sudo mkdir /etc/sasl2Create a SASL configuration file
$ sudo nano /etc/sasl2/memcached.confAdd the following content:
log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2This configuration:
- log_level: Enables detailed logging at level 5.
- mech_list: Specifies authentication method;
plainallows simple username and password authentication. - sasldb_path: Indicates the location of the Memcached SASL database for authentication.
Create a SASL user
Replace example-user with your desired username:
$ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example-userSet a strong password when prompted.
Assign correct ownership to the SASL database
$ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2Restart Memcached to apply changes
$ sudo systemctl restart memcachedCheck the Memcached service status
$ sudo systemctl status memcachedList all SASL users
$ sudo sasldblistusers2 -f /etc/sasl2/memcached-sasldb2Example output:
example-user@ubuntu20: userPasswordTest the new user credentials
Replace example-user@ubuntu20 and strong-password with your actual details:
$ memcstat --binary --servers="127.0.0.1" --username='example-user@ubuntu20' --password=strong-passwordIf the configuration is correct, you should see output similar to this:
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: 8Connect to Memcached
Memcached works with many application frameworks such as PHP, Perl, Python, Ruby, and Java. To test connectivity with PHP, follow these steps.
Install PHP and the Memcached module
$ sudo apt install php php-memcached -yCreate a sample PHP script
$ nano memcached.phpAdd the following PHP code. Replace example-user@ubuntu20 and strong-password with your own credentials:
<!--?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 centron!');
echo $memcached->get('example');
?>This script connects to Memcached using the specified user credentials and binary protocol. It creates a new key named example with the value “Greetings from centron!” and then retrieves it from memory.
Run the PHP script
$ php memcached.phpExample output:
Greetings from centron!The output confirms that the PHP script successfully connected to Memcached, stored the key example, and retrieved the value “Greetings from centron!”.
Conclusion
You have installed Memcached on Ubuntu 20.04 and configured it to securely integrate with application frameworks such as PHP. By caching frequently used data like database queries in memory, Memcached helps improve server performance and reduce load.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.