Speed and performance matter whenever operating a dynamic website or web application. That is where Memcached comes into role. Memcached is a caching system that keeps frequently accessed data in memory for making things run much faster by reducing the load on your server’s database.
In this guide, we will walk you through the steps of how to set up a Memcached PHP cache on a cPanel server.
Table of Contents
What is Memcached?
Memcached is an open-source caching function that enhances the performance of PHP applications or websites. Instead of continued queries in a database, it stores the data in RAM. The Memcached gets this data directly from memory, much faster than other ways.
Web applications can take benefit from low latency and quick access to caching due to Memcached. This function is being used by well-known businesses like Slack and Facebook The main purpose of the Memcached is to cache database query results.
Benefits of Using Memcached
- It Speeds Up Website: It boosts the speed of the website by storing data in memory. Memcached reduces the loading time for a website.
- Reduces Load on Database: This decreases the pressure from the database and prevents slowdowns, especially during heavy traffic.
- Scalability: Memcached can handle a high volume of data and connections, hence ideal for growing sites.
- Easy to use: It is pretty straightforward to set up and integrate with PHP applications.
- Free and Open-Source: Its availability is quite widespread and supported on multiple platforms, free of cost.
Why Caching PHP Applications is Important?
Caching reduces similar database queries that consume most of the performance of a PHP application. It reduces frequent queries to the database by retrieving from Memcached, saving a lot of time and server resources. This will result in faster loading of pages and an improved user experience.
How Does Memcached Work?
The database usually rests on your hard drive. Memcached caches key-value pairs in RAM. When your PHP application needs that data, Memcached will first check whether the data is already cached within the memory. If so, it will immediately serve the data right there. That’s considerably faster than hitting the database. If it’s not cached, it retrieves the data from the database and serves it, placing the data into memory for further requests. When the Memcache cache is full, it will automatically remove the least-used results from the cache. But it’s also going to set a time limit on cache results just to make sure all stale data is removed.
Prerequisites
Before we dive into the installation, here’s what you need:
- Root Access to the Server: You need root or administrator access to install software on your server.
- cPanel Server with WHM Access: You’ll use WHM or Web Host Manager to manage settings related to your server.
- PHP and Apache/Nginx Setup: Memcached works with PHP, so ensure that PHP and your web server – whether it be Apache or Nginx – are already set up.
Step-by-Step Guide to Install Memcached PHP Cache on cPanel Servers
Step 1: Install Memcached on Your Server
- Log in via SSH and update your server: Log in to your server as root using an SSH client such as PuTTY. Update your server to have the latest packages installed.
ssh root@your-server-ip
yum update -y
- Install Memcached: Install Memcached with the following command.
yum install memcached -y
- Start and Enable Memcached: Start the Memcached service and configure it to start at boot.
systemctl start memcached
systemctl enable memcached
- Check the Installation: Ensure that Memcached is running properly.
systemctl status memcached
Step 2: Install the Memcached PHP Extension
- Use EasyApache 4 in WHM: Log into WHM and go to Software > EasyApache 4.
- Install the PHP Extension: In EasyApache 4, find and install the
memcached
PHP extension for your PHP version. - Restart Apache: After installing the extension, restart Apache to apply the changes.
systemctl restart httpd
- Verify the Installation: To check if the extension is installed, create a
info.php
file in your web root with the following code and check it in your browser:
<?php
phpinfo();
?>
Look for “memcached” in the output.
Step 3: Configure Memcached
- Edit the Config File: Open Memcached’s configuration file to make changes.
nano /etc/sysconfig/memcached
- Set Memory Usage: You can specify how much RAM Memcached should use. For example, to use 128MB:
OPTIONS="-m 128"
- Set Listening IP and Port: Memcached usually listens on
127.0.0.1:11211
. You can adjust this if needed.
- Restart Memcached: Restart the service to apply your changes.
systemctl restart memcached
Step 4: Test Memcached Functionality
- Create a Test PHP Script: Make a file named
memcached_test.php
in your web root.
- Add Test Code: Use the following code to check if Memcached is working:
<?php
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);
// Set a value
$memcached->set('key', 'Hello, Memcached!', 60);
// Retrieve the value
$value = $memcached->get('key');
echo $value ? $value : 'Failed to get value from Memcached';
?>
Visit this file in your browser to see if Memcached is running correctly.
Benefits of Choosing Supportfly for cPanel Server Management
- Team of Professionals: The experienced team will always keep your server at its best.
- 24/7 Monitoring: Continuous monitoring helps them spot and repair impending issues before they happen.
- Optimization: They fine-tune the settings on your server to help get faster load times.
- Enhanced Security: Keep servers safe from threats by performing regular security updates.
- Customized Solution: They also offer customized plans to suit your needs and provide full performance on your server.
Conclusion
Memcached is a powerful tool to help your PHP applications run faster by taking some of the data out of memory, rather than going to databases for that information. Here’s how to install Memcached on a cPanel server: Of course, with ease you will have everything set and started to see the results. Monitor regularly and do your tweaks as required for best results.
For professional handling in your server management, join hands with SupportFly. cPanel server management support assures seamless and perfect server smoothness. So, when can you optimize your server?