
Memcached is an open-source, high-performance distributed memory caching system that stores frequently accessed data such as database query results, API responses, and session tokens in RAM. It reduces the load on backend databases by serving repeated requests directly from memory, improving application response times.
This article explains how to install Memcached on an Ubuntu 26.04 server, configure core settings, enable SASL authentication for secure access, and test the connection using a PHP script.
Before you begin, you need to:
The default Ubuntu 26.04 APT repositories include the Memcached package. The following steps install Memcached along with its management tools and start the service.
Update the APT package index.
Install Memcached and the client utilities.
Confirm the installed Memcached version.
Your output should be similar to the one below:
Enable Memcached to start automatically at boot time.
Start the Memcached service.
Verify that Memcached is active and running.
The output should display active (running), confirming that the Memcached service is operational.
Memcached stores its configuration in /etc/memcached.conf. The following directives control the listening address, port, connection limits, and logging behavior.
Open the Memcached configuration file.
Find the bind directive and verify that Memcached listens on the localhost address only.
Find the port directive and verify the default connection port.
Uncomment the -v directive to enable verbose logging.
Uncomment the -c directive to set the maximum number of simultaneous connections.
Add -S at the end of the file after -P /var/run/memcached/memcached.pid to enable SASL authentication.
Save and close the file.
Memcached does not require authentication by default, allowing unrestricted access from any local process. The SASL (Simple Authentication and Security Layer) protocol adds username and password verification to restrict access. The following steps install the SASL package, create an authentication database, and test the secured connection.
Install the SASL authentication package.
Create the SASL configuration directory.
Create the SASL configuration file for Memcached.
Add the following configuration to the file.
Save and close the file.
Within the configuration:
log_level: Sets the logging detail level. The value 5 enables detailed diagnostic logs.mech_list: Defines the authentication mechanism. The value plain uses plain-text username and password.sasldb_path: Specifies the path to the SASL user database file.Create a SASL user for Memcached authentication. Replace example_user with your desired username.
Enter and confirm a strong password when prompted.
Grant the Memcached system user ownership of the SASL database file.
Restart Memcached to activate SASL authentication.
Verify that Memcached is active after the restart.
The output should display active (running), confirming that SASL authentication is enabled.
List all users in the SASL authentication database.
Your output should be similar to the one below:
Test the authenticated connection using memcstat. Replace example_user@ubuntu2604 and strong_password with your actual credentials from the output above.
A successful connection displays server statistics including the version, uptime, and connection count.
Memcached integrates with application frameworks such as PHP, Python, Ruby, and Java. The following steps install the PHP Memcached extension and verify the connection using a sample script.
Install PHP and the Memcached extension.
Create a sample PHP script.
Add the following code to the file. Replace example_user@ubuntu2604 and strong_password with your actual SASL credentials.
Save and close the file.
Run the script using the PHP CLI.
A successful connection returns:
You have installed and secured Memcached on an Ubuntu 26.04 server with SASL authentication and verified the connection using PHP. Memcached integrates with dynamic web applications as a caching layer to reduce database load and improve response times. For more information, refer to the Memcached wiki.
0 Comments
Be the first to comment and share your perspective with the community.