Installing Memcached using PECL on Linux
Memcached is a high-performance, distributed memory object caching system that can be used to speed up dynamic web applications by alleviating database load. PECL (PHP Extension Community Library) is a repository for PHP extensions, including the Memcached extension. This article will guide you through the process of installing Memcached using PECL on a Linux system.
Prerequisites
Before you begin, make sure that your Linux system has the following prerequisites installed:
- PHP 5.2.0 or later
- libmemcached development package
You can install the required packages using the following command:
sudo apt-get update && sudo apt-get install -y memcached libmemcached-dev
Installing PECL
PECL is not included by default on most Linux distributions. You can install it using the following command:
sudo apt-get install -y php-pear
Once PECL is installed, you can check the version using the following command:
pecl version
Installing Memcached using PECL
Now that you have PECL installed, you can install the Memcached extension using the following command:
sudo pecl install memcached
This will download and compile the Memcached extension. Once the installation is complete, you can verify that the extension was installed correctly by checking the output of the following command:
php -m | grep memcached
If the Memcached extension is installed correctly, you should see it listed in the output.
Configuring Memcached
Once the Memcached extension is installed, you need to configure it to connect to the Memcached server. You can do this by adding the following lines to your php.ini file:
[memcached]
memcached.sess_prefix = "session:"
memcached.sess_persistent = 1
memcached.sess_unique_id = 1
memcached.sess_number = "2"
memcached.sess_server[0] = "localhost:11211"
memcached.sess_server_failure_limit = "1"
memcached.sess_remove_failed = 1
These settings configure Memcached to use the local Memcached server and to store session data in Memcached.
Testing Memcached
To test that Memcached is working correctly, you can create a simple PHP script that sets and retrieves data from Memcached. Here is an example script:
<?php
$memcached = new Memcached();
$memcached->addServer('localhost', 11211);
$memcached->set('key', 'value', 0, 60);
$value = $memcached->get('key');
echo $value;
?>
This script creates a new Memcached object, adds the local Memcached server, sets a key-value pair in Memcached, and retrieves the value for the key. If Memcached is working correctly, you should see the value "value" printed to the screen.
Dockerfile Getting Error
If you are trying to build a Docker container and are getting an error when installing Memcached using PECL, make sure that you have the required dependencies installed. Here is an example Dockerfile that installs Memcached using PECL:
FROM php:7.4-apache
RUN docker-php-ext-install memcached && docker-php-ext-enable memcached
This Dockerfile uses the official PHP 7.4 image and installs the Memcached extension using the docker-php-ext-install script. Make sure that your Dockerfile includes the required dependencies before installing Memcached.
In this article, we covered the process of installing Memcached using PECL on a Linux system. We discussed the prerequisites for installing Memcached, how to install PECL, how to install Memcached using PECL, how to configure Memcached, how to test Memcached, and how to troubleshoot Dockerfile errors when installing Memcached. By following the steps in this article, you should be able to install and configure Memcached on your Linux system.