How To Install Memcached on AlmaLinux 10
Memcached stands as one of the most powerful and widely-adopted distributed memory caching systems available today. This high-performance, open-source solution significantly enhances the speed of dynamic database-driven web applications by caching frequently accessed data directly in RAM. For AlmaLinux 10 users, implementing Memcached can dramatically reduce database load and improve application response times.
AlmaLinux 10, as a Red Hat Enterprise Linux (RHEL) compatible distribution, provides excellent support for Memcached installation and configuration. This comprehensive guide will walk you through multiple installation methods, from the straightforward DNF package manager approach to advanced source compilation techniques. Whether you’re managing WordPress sites, Joomla applications, or custom PHP-based solutions, Memcached integration can transform your application’s performance characteristics.
The benefits of Memcached extend beyond simple database query caching. It excels at session management, API response caching, and storing frequently accessed objects like images, files, and metadata. This tutorial covers everything from basic installation to advanced security configurations, ensuring you can implement Memcached effectively in both development and production environments.
Understanding Memcached
Memcached operates as a distributed memory object caching system that stores data in key-value pairs within system RAM. Unlike traditional disk-based caching solutions, Memcached provides lightning-fast data retrieval by eliminating the need for database queries or file system access. This architecture makes it particularly valuable for high-traffic web applications requiring consistent performance under load.
The system’s distributed nature allows multiple servers to share cached data, creating a unified caching layer across your infrastructure. When an application requests data, Memcached first checks its memory store before falling back to the original data source. This approach can reduce database queries by up to 95% in well-optimized applications.
Key features include automatic data expiration, built-in redundancy handling, and support for multiple programming languages. Memcached integrates seamlessly with popular web frameworks and content management systems, making it an ideal choice for WordPress, Joomla, and custom applications. The system’s simplicity and reliability have made it a standard component in modern web application architectures.
Prerequisites and System Requirements
Before beginning the Memcached installation process, ensure your AlmaLinux 10 server meets the necessary requirements. You’ll need root access or a user account with sudo privileges to install packages and modify system configurations. A fresh AlmaLinux 10 installation provides the cleanest environment for this setup.
Network connectivity is essential for downloading packages and dependencies. Your server should have at least 512MB of available RAM, though 1GB or more is recommended for production environments. Consider your application’s caching needs when planning memory allocation, as Memcached’s effectiveness directly correlates with available memory.
Basic firewall configuration knowledge will help secure your installation. While not mandatory, familiarity with systemd service management and basic Linux command-line operations will streamline the installation process. Ensure your system’s package repositories are accessible and up-to-date before proceeding with any installation method.
Method 1: Installing Memcached via DNF Package Manager
The DNF package manager provides the most straightforward approach to installing Memcached on AlmaLinux 10. This method leverages the distribution’s official repositories, ensuring compatibility and automatic dependency resolution.
Updating System Packages
Begin by updating your system’s package repository information and installed packages. This step ensures you have the latest security patches and package versions:
sudo dnf update -y
The update process may take several minutes depending on your system’s current state and internet connection speed.
Installing Memcached and Dependencies
Install Memcached and its required libraries using the following command:
sudo dnf install memcached libmemcached -y
This command installs both the Memcached daemon and the libmemcached client library, which provides essential tools for managing your cache server. The installation process automatically handles dependency resolution and downloads approximately 5-10MB of packages.
Verifying Installation
Confirm your installation was successful by checking the installed package information:
rpm -qi memcached
You can also verify the Memcached version:
memcached --version
This command should display the installed version number and compilation details.
Starting and Enabling Service
Start the Memcached service and enable it to launch automatically at boot:
sudo systemctl start memcached
sudo systemctl enable memcached
Verify the service status:
sudo systemctl status memcached
You should see output indicating the service is active and running.
Method 2: Installing from Source
Source installation provides maximum control over Memcached features and optimization settings. This method is ideal for production environments requiring specific configurations or the latest features not yet available in distribution packages.
Installing Build Dependencies
Install the necessary development tools and libraries:
sudo dnf groupinstall "Development Tools" -y
sudo dnf install libevent-devel gcc make -y
These packages provide the compilation environment required for building Memcached from source code.
Downloading and Compiling Source Code
Download the latest stable Memcached source code:
wget https://www.memcached.org/files/memcached-1.6.38.tar.gz
tar -zxf memcached-1.6.38.tar.gz
cd memcached-1.6.38
Configure the build process with optimization flags:
./configure --prefix=/usr/local/memcached --enable-sasl
The --enable-sasl
flag adds authentication support, while --prefix
specifies the installation directory.
Installation and Setup
Compile and install Memcached:
make
sudo make install
Create a symbolic link for easy access:
sudo ln -s /usr/local/memcached/bin/memcached /usr/local/bin/memcached
Set up proper system integration by creating a systemd service file.
Method 3: Installing via Remi Repository
The Remi repository provides newer versions of PHP-related packages, including updated Memcached builds optimized for modern PHP applications.
Setting up EPEL Repository
Enable the Extra Packages for Enterprise Linux (EPEL) repository:
sudo dnf install epel-release -y
sudo dnf config-manager --set-enabled crb
EPEL provides additional packages not included in the base AlmaLinux repositories.
Adding Remi Repository
Install the Remi repository configuration:
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-10.rpm -y
Verify the repository installation:
sudo dnf repolist | grep remi
Installing Latest Memcached Version
Install Memcached from the Remi repository:
sudo dnf --enablerepo=remi install memcached -y
This method often provides more recent versions than the default repositories, particularly beneficial for compatibility with newer PHP versions.
Configuring Memcached
Proper configuration ensures optimal performance and security for your Memcached installation. The primary configuration file is located at /etc/sysconfig/memcached
.
Understanding Configuration Files
Open the configuration file using your preferred text editor:
sudo nano /etc/sysconfig/memcached
The file contains key parameters that control Memcached behavior and resource allocation.
Key Configuration Parameters
Configure the following essential parameters:
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="128"
OPTIONS="-l 127.0.0.1,::1"
PORT specifies the listening port (default 11211). USER defines the system user for running the service. MAXCONN sets the maximum simultaneous connections. CACHESIZE determines memory allocation in megabytes. OPTIONS provides additional command-line parameters.
Security Configuration
For enhanced security, bind Memcached to localhost only:
OPTIONS="-l 127.0.0.1 -U 0"
The -U 0
option disables UDP support, reducing potential attack vectors. For production environments, consider implementing SASL authentication and encryption.
After configuration changes, restart the service:
sudo systemctl restart memcached
Firewall Configuration
Proper firewall configuration balances security with accessibility. AlmaLinux 10 uses firewalld by default, providing flexible zone-based management.
Understanding Firewall Requirements
Memcached uses port 11211 by default. Local applications can access this port without firewall modifications, but remote connections require explicit rules.
Configuring Firewalld
Allow Memcached traffic through the firewall:
sudo firewall-cmd --add-port=11211/tcp --zone=public --permanent
sudo firewall-cmd --reload
Verify the configuration:
sudo firewall-cmd --list-ports
Advanced Firewall Security
For enhanced security, create a custom zone for Memcached:
sudo firewall-cmd --permanent --new-zone=memcached
sudo firewall-cmd --permanent --zone=memcached --add-port=11211/tcp
sudo firewall-cmd --permanent --zone=memcached --add-source=192.168.1.0/24
sudo firewall-cmd --reload
This configuration restricts access to specific IP ranges, improving security in production environments.
PHP Integration
PHP integration enables web applications to utilize Memcached for caching database queries, session data, and application objects.
Installing PHP Extensions
Install the PHP Memcached extension:
sudo dnf install php-pecl-memcached -y
For legacy applications, you might also need:
sudo dnf install php-pecl-memcache -y
These extensions provide different APIs for interacting with Memcached.
Testing PHP Integration
Create a test script to verify PHP integration:
<?php
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
$memcached->set('test_key', 'Hello, Memcached!');
$result = $memcached->get('test_key');
echo $result;
?>
This script demonstrates basic Memcached operations from PHP.
Testing and Verification
Comprehensive testing ensures your Memcached installation functions correctly and performs optimally under various conditions.
Service Status Verification
Check the Memcached service status:
sudo systemctl status memcached
Monitor the service logs for any errors:
sudo journalctl -u memcached -f
Connection Testing
Test connectivity using telnet:
telnet localhost 11211
Once connected, test basic operations:
set test_key 0 0 5
hello
get test_key
quit
Performance Testing
Use the memcached statistics interface to monitor performance:
echo "stats" | nc localhost 11211
This command provides detailed statistics about cache hits, misses, and memory usage.
Troubleshooting Common Issues
Understanding common Memcached issues helps maintain stable operations and quickly resolve problems.
Service Start Problems
If Memcached fails to start, check the system logs:
sudo journalctl -u memcached --no-pager
Common issues include port conflicts, permission problems, or insufficient memory.
Connection Issues
For connection problems, verify the service is listening:
sudo netstat -tlnp | grep :11211
Check firewall rules and SELinux policies if connections fail.
Performance Issues
Monitor cache statistics to identify performance bottlenecks:
echo "stats" | nc localhost 11211 | grep -E "evictions|bytes|limit_maxbytes"
High eviction rates indicate insufficient memory allocation.
Security Best Practices
Implementing security best practices protects your Memcached installation from unauthorized access and potential attacks.
Restrict network access by binding to localhost only. Use firewalls to limit connections to trusted IP addresses. Consider implementing SASL authentication for production environments. Regularly update Memcached to patch security vulnerabilities.
Monitor access logs for suspicious activity. Implement proper user permissions and avoid running Memcached as root. Use encryption for sensitive data and consider VPN access for remote administration.
Maintenance and Monitoring
Regular maintenance ensures optimal performance and reliability. Monitor memory usage and cache hit rates regularly. Implement log rotation to prevent disk space issues. Schedule regular security updates and system maintenance windows.
Use monitoring tools to track performance metrics and set up alerts for unusual activity. Document configuration changes and maintain backup procedures for critical settings.
Congratulations! You have successfully installed Memcached. Thanks for using this tutorial for installing the Memcached distributed memory caching on your AlmaLinux OS 10 system. For additional help or useful information, we recommend you check the official Memcached website.