Install Memcached On Debian 7 ‘Wheezy’

Memcached is a free, open-source, high-performance, distributed memory object caching system. It is mostly used to speed up the websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. It runs on Unix, Linux, Windows OS and Mac OS.

It is widely used by most popular websites such as YouTube, Reddit, Zynga, Facebook, Orange, Twitter and Wikipedia etc. In this article, let us setup Memcached on Debian 7 ‘Wheezy’.

Prerequisites

Before installing Memcached, make sure that your server is up-to-date.

Login as root user and perform the steps:

root@server:~# apt-get update
root@server:~# apt-get upgrade

In addition, make sure that you have installed the following packages in your server:

root@server:~# apt-get install mysql-server php5-mysql php5 php5-dev php-pear

Install Memcached

Execute the following command to install Memcached:

root@server:~# apt-get install memcached

Now test Memcached service is started and running using the following command:

root@server:~# ps -eaf | grep memcached
nobody    7148     1  0 15:09 ?        00:00:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
root      7186  3190  0 15:13 pts/0    00:00:00 grep memcached

And also check the stats of Memcached using the following command:

root@server:~#  echo "stats settings" | nc localhost 11211
STAT maxbytes 67108864
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove no
END

Configure Memcached

The default configuration file of Memcached in Debian is /etc/memcached.conf:

root@server:~# nano /etc/memcached.conf

Change the RAM (ex. 128) and other values as per your requirement:

[...]
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding t$
# memory

-m 128
# Default connection port is 11211
-p 11211
[...]

For a best configuration practices, please refer here.

Save and exit the config file. Restart Memcached service to take effect the saved changes:

root@server:~# /etc/init.d/memcached restart

Now check the configuration stats of Memcached again using the following command:

root@server:~#  echo "stats settings" | nc localhost 11211
STAT maxbytes 134217728
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
[...]

Install PHP-Memcached extension

Run the following command to start installing php-memcached extension:

root@server:~# pecl install memcache

You will see a output like this:

[...]
Installing shared extensions:     /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr/lib/php5/20100525+lfs/
running: find "/tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7" | xargs ls -dils
799913   4 drwxr-xr-x 3 root root   4096 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7
799917   4 drwxr-xr-x 3 root root   4096 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr
799918   4 drwxr-xr-x 3 root root   4096 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr/lib
799919   4 drwxr-xr-x 3 root root   4096 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr/lib/php5
799920   4 drwxr-xr-x 2 root root   4096 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr/lib/php5/20100525+lfs
798969 236 -rwxr-xr-x 1 root root 241444 Jul 27 16:20 /tmp/pear/temp/pear-build-rootGAjS9y/install-memcache-2.2.7/usr/lib/php5/20100525+lfs/memcache.so

Build process completed successfully
Installing '/usr/lib/php5/20100525+lfs/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini

Restart the Apache server using the following command:

root@server:~# /etc/init.d/apache2 restart

Now test the Memcached extension:

root@server:~# php -i | grep memcache
/etc/php5/cli/conf.d/20-memcached.ini,
memcached
memcached support => enabled
libmemcached version => 1.0.8
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
memcached.sess_binary => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => 1 => 1
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
Registered save handlers => files user memcached 

That’s it.