Memcached (Memcache Daemon) is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory. It is commonly used to speed up dynamic database-driven websites by caching data and objects in server memory to reduce the number of times the data source must be read. Memcached is free and open-source software, licensed under the Revised BSD license. Also, there are PHP extensions which allow you to work with memcached. There are two PHP memcache extensions available from the PHP Extension Community Library, PHP memcached and PHP memcache.
PHP Memcache vs PHP Memcached
These two PHP extensions are not identical. PHP Memcache is older, very stable but has a few limitations. The PHP memcache module utilizes the daemon directly while the PHP memcached module uses the libMemcached client library and also contains some added features.
Installing Memcache Daemon + PHP memcache or PHP memcached
Before selecting a PHP extension be sure to install the memcache daemon.
How-To: Install Memcache on CentOS (WHM/cPanel)
- Login into your WHM panel and using easyapache to enable Memcache
Go to Software – Module Installers – PHP Pecl. Search for memcache and then install both memcache and memcached
Restart apache:
# service httpd restart
Check your memcached server is running successfully: ps -eaf | grep memcached
Installing Memcache Daemon + PHP memcache or PHP memcached on CentOS or Ubuntu
Before selecting a PHP extension be sure to install the memcache daemon:
Centos / Red Hat:
# yum install memcached
Ubuntu/ Debian:
# apt-get update
# apt-get install memcached
After installing Memcached, open the configuration file for Memcached and make any changes:
Centos / Red Hat:
# nano /etc/sysconfig/memcached
Ubuntu / Debian:
# nano /etc/memcached.conf
Exit and save the configuration file, and then restart Memcached
# service memcached restart
Remember to set the memcache daemon to start on server boot.
Centos / Red Hat:
# chkconfig memcached on
Ubuntu / Debian:
# update-rc.d memcached enable
Install a PHP memcache extension
PHP memcache
You can browse the version here – https://pecl.php.net/package/memcache The current version as of this writing is 3.0.8
wget https://pecl.php.net/get/memcache-3.0.8.tgz
tar xvf memcache-3.0.8.tgz
cd memcache-3.0.8
phpize
./configure
make
make install
make test
Then add memcache.so to your php.ini file:
# nano /etc/php.ini
extension="memcache.so"
PHP memcached:
Remember to install libmemcached dependancy (Ubuntu/Debian):
yum install cyrus-sasl-devel zlib-devel gcc-c++
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz
tar -xvf libmemcached-1.0.16.tar.gz
cd libmemcached-1.0.16
./configure --disable-memcached-sasl
make
make install
Then install PHP Memcached:
wget https://pecl.php.net/get/memcached-3.0.8.tgz
tar xf memcached-3.0.8.tgz
cd memcached-3.0.8
phpize
./configure
make
make install
make test
Then add memcached.so to your php.ini file:
extension="memcached.so"
You will need to connect your PHP application to memcached. For example, using W3 Total Cache with WordPress, memcached module with Drupal, Magento config, etc.
Finally restart memcached, httpd (or apache, apache2 for Ubuntu/Debian)
If you would like to view stats of hit rate etc you can download memcachephp stats at GitHub – HERE. Which will look something like this: