Memcached is useful for speeding up php sites like WordPress or interfaces like ownCloud. Memcached can be installed with libmemached support which is a C and C++ compiler which greatly speeds up processing for the cache. This guide assumes you are using php5-fpm with nginx or apache2 on Debian or Ubuntu/Lubuntu. This guide was tested on a Raspberry Pi 2, Banana Pi and Orange Pi. I made this tutorial because I noticed ownCloud guides using nginx recommend installing memcached but it is never actually enabled. It is also part of my VPS migration plans when I migrate my DigitalOcean 512MB droplet to a 1GB droplet to host the new forum.
Install memcached with libmemcached
Install some dependencies like memcached, php5-dev tools and php-pear for installing extensions, libsasl2-dev is optional and may be important for shared hosting users. I haven't tested SASL because I use a DigitalOcean VPS for hosting this site.
Install these packages to get memcached up and running quickly, for those who want the latest version there are instructions for building libmemcached and the latest memcached.
sudo apt-get update sudo apt-get install libmemcached-tools memcached libsasl2-dev php5-dev php-pear pkg-config build-essential -y
If you are on php 7 install pear manually
wget http://pear.php.net/go-pear.phar php go-pear.phar
If you do not want the latest libmemcache, you can now skip down to adding the correct extension lines to the php5-fpm php.ini file.
You can build the absolute latest libmemcached by following these steps.
Download the libmemcached source file (there may be an updated version on the official page), unpack it and enter the directory
mkdir ~/libmemcacheinstall wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz tar -xvf libmemcached-1.0.18.tar.gz --strip-components 1 -C ~/libmemcacheinstall cd ~/libmemcacheinstall
If you do want SASL support use this command to configure libmemcached
If you don't need SASL support configure libmemcached with this syntax (source)
./configure --prefix=/usr --disable-memcached-sasl
Get the number of cores you are working with on your system so you can build libmemcached with all your CPU cores
The virtual machine I am running uses 2 cores so add this after the -j to compile libmemcached with both cores and then make install to install it
make -j2 make install
Install memcached pecl extension
Using php-pear's installer to install memcached, run this command
pecl install pecl_http-2.5.5 memcached
You will be prompted for the location of libmemcached, just press Enter and it should find it automatically, if not enter /usr/local
Now you need to add a line to enable memcached support in your php.ini.
This command appends the line to the bottom of the php.ini file for php5-fpm
echo "extension=memcached.so" >> /etc/php5/fpm/php.ini
service php5-fpm restart
If you ever need to uninstall memcached
pecl uninstall memcached
You can remove the libmemcached build folder from your home directory
rm -R ~/libmemcacheinstall
Test memcached with libmemcached is Enabled
Time to test that memcached with libmemcached is installed for php5-fpm on nginx or apach2. I will assume you already have WordPress or some other frontend that uses php.
For nginx or apach2 create an info.php file in your root directory for the virtual host that serves php
Paste this code
<?php phpinfo(); ?>
Ctrl+X, Y and Enter to save
Browse to the info.php file at http://ip.address/info.php
You will see a memcached section like this if you scroll down
If you want you can do a quick memcached benchmark
memslap --server=127.0.0.1 --concurrency=2 --execute-number=50
You may see memslap command not found in which case use
memcslap --server=127.0.0.1 --concurrency=2 --execute-number=50
Threads connecting to servers 2 Took 0.015 seconds to load data
That concludes the install memcached with libmemcached for php5-fpm for nginx or Apache web servers.