
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.
Before you begin:
Have an Ubuntu 20.04 server.
Access the server using SSH as a non-root user with sudo privileges.
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.
Install Memcached and required add-on tools.
View the installed Memcached version on your server.
Output:
Enable the Memcached service to automatically start at boot time.
Start the Memcached service.
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.
Add -S at the end of the file after -P /var/run/memcached/memcached.pid to enable SASL authentication on your server.
Find and uncomment the -v directive to enable verbose logging to the /var/log/memcache file.
Uncomment the -c 1024 directive to limit the number of simultaneous Memcached connections. Replace 1024 with your desired number of connections.
Find the following Memcached port directive to verify the application connection port on your server.
Locate the -l directive and ensure it is set to 127.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.
Save and close the file.
Restart Memcached to apply the configuration changes.
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.
Create a new directory to store your SASL authentication credentials.
Create a new SASL configuration file to use with Memcached. For example, memcached.conf.
Add the following contents to the file.
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 value 5 enabling detailed logs.mech_list: Defines the authentication method, where plain 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. Replace example-user with your actual username to enable it in your Memcached database.
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.
Restart Memcached to apply the configuration changes.
Check the status of Memcached server.
List all the created user in SASL authentication database.
Output:
Connect to Memcached to test your new user credentials. Replace example-user@ubuntu20 and strong-password with your actual user details.
Your output should look like the one below when successful:
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.
Create a new sample PHP script to connect to Memcached.
Add the following contents to the file. Replace example-user@ubuntu20 and strong-password with your actual user credentials.
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 value Greetings 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.
Output:
Based on the above output, the PHP script successfully connects to Memcached, creates the example key and writes a new value Greetings from Vultr to retrieve from memory.
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.
0 Comments
Be the first to comment and share your perspective with the community.