
Memcached is an open-source, distributed memory caching system that stores frequently requested data in memory (RAM) to speed up application processes. Memcached serves the cached data to avoid repeated queries to applications making it ideal for web applications, API responses, and database queries.
This article explains how to install Memcached on Rocky Linux 9 and secure the server to enable data caching for applications.
Before you begin:
Memcached is available in the default package repositories on Rocky Linux 9. Follow the steps below to install Memcached using the DNF package manager on your server.
Update the server's package index.
Install all required dependency packages such as the libmemcached library.
Install Memcached.
Enable the Memcached service to automatically start at system boot.
Start the Memcached service.
View the Memcached service status and verify that it's running.
Output:
Memcached uses the /etc/sysconfig/memcached main configuration file to run on your server. Follow the steps below to configure Memcached and adjust the default configuration values such as the listening port, cache size, and maximum connection limits.
Open the Memcached configuration file /etc/sysconfig/memcached using a text editor such as nano.
Add the -S value to the OPTIONS directive within the file to enable SASL authentication.
Modify the following Memcached configurations to match your needs.
Save and close the file.
Within the above configuration:
PORT: Sets the TCP port Memcached should use to listen for incoming connections on the server, port 11211 by default.USER: Sets the user profile to run the Memcached service.MAXCONN: Sets the maximum number of simultaneous connections Memcached can process on the server.CACHESIZE: Sets the amount of cached memory Memcached can store data (in MB).OPTIONS: Sets additional options to enable on the Memcached daemon such as the listening network address.Restart Memcached to apply the configuration changes.
Memcached runs without any authentication requirements for all users by default. Follow the steps below to enable the SASL (Simple Authentication and Security Layer) protocol and protect cached data by requiring authentication for all users on the server.
Install SASL dependency packages for Memcached.
Create a new /etc/sasl2/ directory to store the SASL configuration files.
Create a new memcached.conf file in the /etc/sasl2/ directory.
Add the following contents to the file.
Save and close the file.
The above configuration enables SASL to use the PLAIN authentication method and sets the logging level to 5 using the Memcached SASL database file etc/sasl2/memcached-sasldb2 for authentication.
Create a new user with a strong password. Replace memcached_user with your desired username and enter the user password when prompted.
Within the above command:
-a: Specifies the memcached authentication service name.-f: Sets the SASL database path /etc/sasl2/memcached-sasldb2.-c: Creates a new SASL user memcached_user.Grant the Memcached user memcached ownership privileges to the SASL database file.
List the SASL database file permissions and verify that the Memcached user has read and write privileges.
Output:
Restart the Memcached service to apply the configuration changes.
Use the memstat tool to test your SASL authentication credentials. Replace memcached_user and strong_password with your actual user details.
Your output should be similar to the one below when successful.
In case your receive the following error:
Temporarily disable SELinux.
Run the memstat command again.
Memcached is compatible with multiple programming languages and frameworks such as Python, Ruby, and Java. Follow the steps below to connect to Memcached using PHP.
Install the Extra Packages for Enterprise Linux (EPEL) repository.
Install PHP and the Memcached extension using the following command.
Create a new PHP application file.
Add the following contents to the file. Replace memcached_user and strong_password with the actual user credentials you created earlier.
Save and exit the file.
The above PHP application connects to Memcached and authenticates using SASL to ensure secure access on the server, stores the string Greetings from Vultr, and displays the key value when accessed.
Run the PHP script to test access to Memcached on your server.
Output:
You have installed Memcached on your Rocky Linux 9 server and configured SASL to require authentication for all users. Memcached works as a caching solution you can integrate with multiple applications on your server. For more information and configuration options, visit the Memcached Wiki.
0 Comments
Be the first to comment and share your perspective with the community.