Memcached is a high-performance distributed memory caching system that reduces database load by storing frequently accessed data in RAM. This guide installs Memcached on Ubuntu 26.04, configures SASL authentication to secure access, and verifies the setup with a PHP script that stores and retrieves a cached value. By the end, you'll have a secured Memcached instance ready for application use.
Install Memcached
Memcached is available in Ubuntu 26.04's default APT repository.
1. Update the APT package index:
$ sudo apt update
2. Install Memcached and the management tools:
$ sudo apt install memcached libmemcached-tools -y
What you just installed:
-
memcached: the caching server daemon -
libmemcached-tools: CLI utilities for inspecting and managing Memcached
3. Verify the installed version:
$ memcached --version
Manage the Memcached Service
Enable Memcached to start automatically when the server boots.
1. Enable and start the service:
$ sudo systemctl enable memcached
$ sudo systemctl start memcached
2. Check the service status:
$ sudo systemctl status memcached
3. Stop or restart the service when needed:
$ sudo systemctl stop memcached
$ sudo systemctl restart memcached
Configure Memcached
1. Open the configuration file:
$ sudo nano /etc/memcached.conf
Verify the following settings are present:
-l 127.0.0.1
-l ::1
-p 11211
-c 1024
-S
These settings bind Memcached to localhost only, set the port to 11211, cap connections at 1024, and enable SASL support.
2. Restart Memcached to apply changes:
$ sudo systemctl restart memcached
Enable SASL Authentication
1. Install the SASL utilities:
$ sudo apt install sasl2-bin -y
2. Create the SASL configuration file:
$ sudo nano /etc/sasl2/memcached.conf
log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2
3. Create a Memcached user:
$ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example_user
4. Set the correct ownership on the credential database:
$ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2
5. Restart Memcached:
$ sudo systemctl restart memcached
Test with PHP
1. Install PHP and the Memcached extension:
$ sudo apt install php php-memcached -y
2. Create a test script:
$ nano memcached_test.php
<?php
$memcached = new Memcached();
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->addServer('127.0.0.1', 11211);
$memcached->setSaslAuthData('example_user', 'your_password');
$memcached->set('test_key', 'Hello from Memcached on Ubuntu 26.04');
echo $memcached->get('test_key');
?>
3. Run the script:
$ php memcached_test.php
The output Hello from Memcached on Ubuntu 26.04 confirms SASL authentication and caching are working correctly.
Next Steps
Memcached is running and secured with SASL authentication. From here you can:
- Integrate Memcached with a PHP application using the
php-memcachedextension - Use
memstatto monitor cache hit rates and memory usage - Deploy Memcached as a session storage backend for high-traffic web applications
For the full guide with additional tips, visit the original article on Vultr Docs.
Top comments (0)