Computersnyou

How to Setup Memcached With PHP 7 On Linux

Posted on  4/20/2017

web architecture

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Free & open source, high-performance, distributed memory object caching system

For PHP there is php-memached Unfortunately installation of php-memecahced is is not that simple as apt-get install because latest php-memcached is not available via native package managers i.e apt get .

Follow these simple steps to install and setup PHP-Memcached with php 7 ( 7.1) .

 

All steps are tested on Ubuntu 16.04 LTS , but these steps are same for almost all major Linux distribution .
memcache and memcached serialize data differently, meaning that data written with one library can’t necessarily be read with the other library.

 

Prerequisites

php-dev package is required

sudo apt-get install php7.1-dev

Requirements

sudo apt-get update 
sudo apt-get install build-essential pkg-config make git g++ gcc

Setup

Now lets install required libraries

sudo apt-get install libmemcached-dev libmsgpack-dev libmsgpackc2

now clone and build php-memcached

git clone --depth 1 https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached
phpize
./configure
make
sudo mv modules/ /usr/local/memcached/

 

Enable

following command will create memcached.ini files under /etc/php/7.1/cli/conf.d/ and  /etc/php/7.1/fpm/conf.d/ directories

echo 'extension=/usr/local/memcached/memcached.so' | \
sudo tee /etc/php/7.1/cli/conf.d/memcached.ini

echo 'extension=/usr/local/memcached/memcached.so' | \
sudo tee /etc/php/7.1/fpm/conf.d/memcached.ini

Verify

“-m” command prints all the compiled modules. you can also use ” -i ” to see all loaded configuration or old phpinfo() function to print all info .

php -m | grep "memcached" && php-fpm7.1 -m | grep "memcached"

sample output:

Screenshot from 2017 04 20 22 45 31

Restart PHP-FPM

If you are using php-fpm with nginx or Apache restart php-fpm and your web-server to load changes

sudo systemctl status php7.1-fpm.service

Important links


  • Home
  • About