Cloud Service >> Knowledgebase >> cPanel >> How to Setup Memcached on Your Website?
submit query

Cut Hosting Costs! Submit Query Today!

How to Setup Memcached on Your Website?

Website speed is critical in today’s digital landscape. Visitors anticipate swift page loads, and delays can drive them away, often permanently. To ensure your site performs at its peak, employing an advanced caching solution like Memcached is essential.

 

Memcached is a high-performance, distributed memory caching system designed to boost website speed by storing frequently requested data in memory. This approach minimizes the need for repetitive database queries, significantly enhancing your site's responsiveness.

Prerequisites

To set up Memcached, you need:

A server or hosting environment where you have administrative access.

Basic knowledge of the command line interface.

A web application that you wish to accelerate using Memcached.

Installing Memcached

Installation is dependent on the operating system installed on the server. Here are the general procedures for common platforms.

On Ubuntu/Debian

 

Update the Package List:

sudo apt update

Install Memcached:

sudo apt install memcached

Verify Installation:

memcached -h

On CentOS/RHEL

Install EPEL Repository (if not already installed):

sudo yum install epel-release

Install Memcached:

sudo yum install memcached

Verify Installation:

memcached -h

On Windows

You can download the Memcached binary from the official website or use a package manager like Chocolatey for Windows.

Install Memcached Using Chocolatey:

choco install memcached

Verify Installation:

Open Command Prompt and run:

memcached -h

Configuring Memcached

Configure Memcached to suit your needs. The default configuration file is usually located at

 /etc/memcached.conf (Linux) 

C:\memcached\memcached.conf (Windows).

Key Configuration Options

Memory Usage: Define how much memory Memcached can use.

-m 64

This sets Memcached to use 64MB of memory. Adjust based on your server's capacity and needs.

Port: The port on which Memcached will listen (default is 11211).

-p 11211

Binding Address: Set the IP address to which Memcached will be bound.

-l 127.0.0.1

Max Connections: Determine the highest simultaneous connections.

-c 1024

After editing the configuration file, restart Memcached to apply the changes.

sudo systemctl restart memcached

Integrating Memcached with Your Web Application

PHP

Install PHP Extension:

sudo apt install php-memcached

Verify Installation:

Create a PHP file with

phpinfo();

?>

Open it in a browser and look for the Memcached section.

Using Memcached in PHP:

$memcached = new Memcached();

$memcached->addServer('127.0.0.1', 11211);

// Set a value

$memcached->set('key', 'value', 60); // 60 seconds expiry

// Get a value

$value = $memcached->get('key');

echo $value;

Python

For Python applications, you can use the pymemcache library.

Install pymemcache:

pip install pymemcache

Using Memcached in Python:

from pymemcache.client import base

client = base.Client(('127.0.0.1', 11211))

# Set a value

client.set('key', 'value', expire=60) # 60 seconds expiry

# Get a value

value = client.get('key')

print(value.decode('utf-8'))

Node.js

For Node.js applications, you can use the memcached module.

Install memcached module:

npm install memcached

Using Memcached in Node.js

const Memcached = require('memcached');

const memcached = new Memcached('127.0.0.1:11211');

// Set a value

memcached.set('key', 'value', 60, function(err) {

  if (err) console.error(err);

});

// Get a value

memcached.get('key', function(err, data) {

  if (err) console.error(err);

  console.log(data);

});

Monitoring Memcached

Monitoring is crucial to ensuring it is running optimally. You can use tools or integrate with monitoring systems like

Nagios 

Zabbix.

Using memcached-tool

Install the tool (if not already installed):

sudo apt install memcached-tools

Check the status:

memcached-tool 127.0.0.1:11211 stats

Managing Memcached via cPanel

cPanel provides an easy interface to administer various aspects of your web hosting. While Memcached itself isn't managed directly through it, you can use it to control: 

Server

Databases

File configurations

It indirectly helps in managing Memcached.

To manage your Memcached settings:

If your hosting provider allows, use the Terminal feature in cPanel to execute commands directly on your server.

To manage PHP extensions related to Memcached, use the "PHP PEAR Packages" or "PHP Extensions and Applications Package" features in cPanel.

To Sum it Up!

Setting up Memcached on your website involves:

 

Installing the software.

Configuring it to suit your needs.

Integrating it with your web application.
Proper configuration and monitoring are required to get the most out of your choice. 

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!