
Memcached is an open-source distributed memory caching system that stores data and objects such as frequent database queries, API calls, expensive computations, session data, or temporary tokens in RAM. Memcached integrates with modern applications to reduce the number of frequent operations such as Database calls to improve the application response time and server resource usage.
This article explains how to install Memcached on Ubuntu 24.04 and configure the application to enable secure connections using the SASL (Simple Authentication and Security Layer) mechanism.
Before you begin:
Memcached is available in the default Ubuntu 24.04 repositories and you can install the package using the APT package manager. In addition, you can compile and install a specific version using the application source code. In the following steps, install Memcached and enable it to run on your server.
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 configurations in the /etc/memcached.conf file to run on your server. You can set up the Memcached port, and interface configurations such as the default port, connection limits, memory and IP address to listen for incoming connections on your server. In the following steps, configure Memcached and verify the process details to run 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.
-v directive to enable verbose logging to the /var/log/memcache file.-c 1024 directive to limit the number of simultaneous Memcached connections. Replace 1024 with your desired number of connections.127.0.0.1 to only accept local Memcached connections. Change the address to your public IP or Vultr VPC address when using Memcached on a standalone server to allow external connections.Save and close the file.
Restart Memcached to apply the configuration changes.
Memcached does not run with any authentication protocol unless enabled in your configuration making it insecure and open to all requests. The SASL protocol enables authentication and requires a valid user to access Memcached. Follow the steps below to install the SASL package and secure Memcached on your 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 above SASL configuration enables authentication using the Memcached database. Within the configuration:
log_level: Enables logging. The value 5 enables high-detail logs.mech_list: Sets the authentication mechanism. The value plain enables plain username and password usage.sasldb_path: Specifies the Memcached SASL database file to use 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.
Connect to Memcached to test your new user credentials. Replace example-user and strong-password with your actual user details.
Your output should look like the one below when successful.
Memcached is compatible with multiple application frameworks such as PHP, Perl, Python, Ruby, and Java. Follow the steps below to connect to Memcached and test the application using PHP.
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 and strong-password with your actual user credentials.
Save and close the file.
The above PHP script connects to Memcached using your example user details and the binary protocol. A new key example with the value Greetings from Vultr is added to the Memcached memory and retrieved using 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 installed Memcached on an Ubuntu 22.04 server and secured the application to integrate with application frameworks such as PHP. Memcached improves your server performance and integrates with dynamic web applications to store repeated queries such as database calls in memory. For more information, visit the Memcached wiki repository.
0 Comments
Be the first to comment and share your perspective with the community.