Tuesday, June 18, 2024

Install Memcached Server on Ubuntu

Assalamu Alaikum.
Welcome to Uddoyon, in this article we know about How to Installing Memcached Server and PHP Memcache for Nginx Environment on Ubuntu System so keep reading…

Memcached is a high-performance, distributed memory caching system used to speed up dynamic web applications by alleviating database load. Integrating Memcached with PHP and Nginx can significantly enhance the overall performance of your web applications. In this article, we’ll guide you through the steps to install Memcached Server and the PHP Memcache extension in an Nginx environment on Ubuntu.

- Advertisement -

Prerequisites:
Before you begin, make sure you have the following prerequisites:

  1. A server with Nginx installed.
  2. PHP installed on your server.
  3. Administrative access to your server.

Step 1: Install Memcached Server:
To install Memcached Server, follow these steps:

- Advertisement -
sudo apt update
sudo apt install memcached -y

After installation, start the Memcached service and enable it to start on boot:

sudo systemctl start memcached
sudo systemctl enable memcached

Step 2: Install PHP Memcache Extension:
Now, let’s install the PHP Memcache extension. This extension allows PHP to communicate with the Memcached server. (for Multi-php: replace php-memcache to php7.x/8.x-memcache)

- Advertisement -
sudo apt install php-memcache

After the installation, restart the PHP and Nginx services to apply the changes: (for Multi-php: replace php-fpm to php7.x/8.x-fpm)

sudo systemctl restart php-fpm
sudo systemctl restart nginx

Step 3: Configure PHP to Use Memcache (Optional):
Open the PHP configuration file using a text editor of your choice. In this example, we’ll use nano:

sudo nano /etc/php/7.x/fpm/php.ini

Add the following line to enable the Memcache extension:

extension=memcache.so

Save the file and exit the editor. Restart the PHP service to apply the changes:

sudo systemctl restart php-fpm

Step 4: Test Memcache Integration:
Create a simple PHP script to test the Memcache integration. For example, create a file named test-memcache.php in your Nginx web root:

<?php
$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");

echo 'Connected to Memcached Server';

$key = 'example_key';
$data = 'Hello, Memcache!';

$memcache->set($key, $data, 0, 60);

$value = $memcache->get($key);

echo 'Value from Memcached: ' . $value;
?>

Access this script in your web browser (e.g., http://yourdomain.com/test-memcache.php) to verify that PHP can connect to Memcached and retrieve data.

Congratulations! You’ve successfully installed and configured Memcached Server with the PHP Memcache extension in an Nginx environment on Ubuntu. Utilizing Memcached for caching can significantly improve the performance and responsiveness of your web applications.

- Advertisement -
- Advertisement -
Related Articles
- Advertisement -

Popular Articles

- Advertisement -
error: Content is protected !!