
Memcached is an open-source distributed caching system that stores data like database queries, API calls, and session data in RAM. It helps reduce frequent operations, improving application response time and server resource usage.
In this article, you are to install Memcached on Ubuntu 22.04 and configure it to enable secure connections using the SASL (Simple Authentication and Security Layer) mechanism.
Before you begin:
Have an Ubuntu 22.04 server.
Access the server using SSH as a non-root user with sudo privileges.
Memcached is available in the default Ubuntu 22.04 repositories and can be installed via the APT package manager. Alternatively, you can compile and install a specific version from the source code. Follow the steps below to install and enable Memcached 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 configurations, including the port, connection limits, memory, and IP address to listen for incoming connections. Follow the steps below to configure Memcached and verify its process details 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 listen directive and ensure it is set to the server's loopback address 127.0.0.1 to accept only local Memcached connections. If using Memcached on a standalone server, change the address to your public IP or Vultr VPC address 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. A value of 5 sets high-detail logging.mech_list: Sets the authentication mechanism. plain enables username and password authentication.sasldb_path: Specifies the path to the SASL database file for Memcached authentication.Create a user using the saslpasswd2 utility. Replace example-user with your actual username to add it to the Memcached SASL 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@ubuntu22 and strong-password with your actual user details.
Your output should look like the one below when successful.
Memcached integrates with application frameworks such as PHP, Perl, Python, Ruby, and Java. Follow the steps below to connect to Memcached and test it 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@ubuntu22 and strong-password with your actual user credentials.
Save and close the file.
The above PHP script connects to Memcached using your example user and the binary protocol. It sets a new key example with the value Greetings from Vultr! and retrieves it using the $memcached variable.
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.
In this article, you installed and secured Memcached on an Ubuntu 22.04 server. Memcached improves server performance by caching repeated operations like database queries and integrates easily with dynamic web applications.
0 Comments
Be the first to comment and share your perspective with the community.