Memcached is a powerful open-source distributed memory caching system that can greatly enhance the performance of dynamic web applications by caching data in memory. It is particularly useful for caching database query results, session data, web pages, and API responses. In this tutorial, we will guide you through the process of installing Memcached on Alma Linux 8.
Prerequisites
Before we begin, make sure you have the following:
- A server running Alma Linux 8.
- Root access to the server.
Step 1: Installing Memcached
To install Memcached on Alma Linux 8, you can use the default package manager, DNF. Open a terminal and run the following command to install Memcached and its dependencies:
dnf install memcached libmemcached -y
Once the installation is complete, you can verify the installation by running the following command:
rpm -qi memcached
This will display information about the installed Memcached package.
Step 2: Configuring Memcached
After installing Memcached, you need to configure it to suit your needs. The main configuration file for Memcached is located at /etc/sysconfig/memcached
. Open the file using a text editor:
nano /etc/sysconfig/memcached
In this file, you can modify several parameters to customize the behavior of Memcached. Some key parameters include:
PORT
: The port on which Memcached listens. The default is 11211.USER
: The user under which Memcached runs. The default is “memcached”.MAXCONN
: The maximum number of concurrent connections Memcached can handle. The default is 1024.CACHESIZE
: The amount of memory allocated for caching. The default is 64MB.OPTIONS
: Additional options, such as the IP address to bind to. The default is “127.0.0.1,::1”.
Make the necessary changes to these parameters according to your requirements. Save the file and exit the text editor.
Step 3: Starting and Enabling Memcached
Now that Memcached is installed and configured, you can start the Memcached service and enable it to start automatically on system boot. Run the following command to start the service:
systemctl enable memcached --now
To check the status of the Memcached service, you can use the following command:
systemctl status memcached
If everything is configured correctly, you should see that the service is active and running.
Step 4: Configuring Firewalld
If you have firewalld enabled on your Alma Linux system, you need to allow incoming connections to the Memcached port. By default, Memcached listens on port 11211. Run the following commands to allow incoming connections on ports 11211 and 80:
firewall-cmd --add-port=11211/tcp --zone=public --permanent firewall-cmd --add-port=80/tcp --zone=public --permanent
After adding the firewall rules, reload the firewalld configuration:
firewall-cmd --reload
Step 5: Installing PHP with Memcached Support
To integrate Memcached with PHP-based applications, you need to install the Memcached extension for PHP. Alma Linux provides the EPEL and Remi repositories, which contain the necessary packages. Run the following commands to install the repositories:
dnf install epel-release -y dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm -y
Next, enable the PHP Remi repository:
dnf module enable php:remi-7.4 -y
Finally, install the Memcached extension for PHP:
dnf install php-pecl-memcache php-pecl-memcached -y
Step 6: Verifying Memcached for PHP
To verify that Memcached is working correctly with PHP, we will install the Nginx web server and create a sample PHP page. Run the following command to install Nginx and PHP packages:
dnf install nginx php php-cli -y
Next, create a PHP info page:
nano /var/www/html/info.php
Add the following lines to the file:
<?php phpinfo(); ?>
Save the file and exit the text editor. Create a symbolic link to the Nginx default web root directory:
ln -s /var/www/html/info.php /usr/share/nginx/html/
Start the Nginx service and enable it to start automatically:
systemctl start nginx systemctl enable nginx
Finally, open your web browser and access the URL http://your-server-ip/info.php. You should see the PHP info page, which will display information about the installed PHP version and the Memcached extension.
Conclusion
Congratulations! You have successfully installed Memcached on Alma Linux 8. With Memcached, you can now significantly improve the performance of your PHP-based website or applications by caching data in memory. This caching mechanism can greatly reduce the latency and load on your database, resulting in faster response times for your users.
If you have any questions or need further assistance, feel free to reach out to our team at Shape.host. We provide reliable and scalable cloud hosting solutions, including Cloud VPS, to help businesses optimize their web applications and ensure a smooth user experience. Contact us today to learn more about our services and how we can help your business thrive.