Install Memcached PHP Extension in CentOS


Please shareShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditDigg thisShare on StumbleUpon

Memcached is a general-purpose distributed memory caching system that was originally developed by Danga Interactive for LiveJournal, but is now used by many other sites. It is often used to speed up dynamic database-driven 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. Memcached runs on Unix, Linux, Windows and Mac OS X and is distributed under a permissive free software license.

Memcached’s APIs provide a giant hash table distributed across multiple machines. When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order. Applications using Memcached typically layer requests and additions into RAM before falling back on a slower backing store, such as a database.

The system is used by sites including YouTube, Reddit, Zynga, Facebook, Orange, Twitter and Wikipedia. Engine Yard is using Memcached as the part of their platform as a service technology stack and Heroku offers a managed Memcached service built on Couchbase Server as part of their platform as a service. Google App Engine, AppScale, Windows Azure and Amazon Web Services also offer a Memcached service through an API.


Install Memcached daemon:

The Memcached software doesn’t exist in the standard CentOS repository. You can download from EPEL repository.

Update the repositories:

yum update -y

Start the installation:

yum install memcached

Configure Memcached:

vi   /etc/sysconfig/memcached

Start Memcached:

chkconfig --levels 235 memcached on

/etc/init.d/memcached start

Check and verify Memcached

root@ns5001089 [~]# netstat -an | grep 11211
tcp 0 0* LISTEN
tcp 0 0 TIME_WAIT
tcp 0 0 :::11211 :::* LISTEN
udp 0 0*
udp 0 0 :::11211 :::*
root@ns5001089 [~]# memcached-tool localhost:11211 stats
#localhost:11211 Field Value
 accepting_conns 1
 auth_cmds 0
 auth_errors 0
 bytes 44808145
 bytes_read 45373990
 bytes_written 71278002
 cas_badval 0
 cas_hits 0
 cas_misses 0
 cmd_flush 0
 cmd_get 4073
 cmd_set 546
 conn_yields 0
 connection_structures 16
 curr_connections 10
 curr_items 531
 decr_hits 0
 decr_misses 0
 delete_hits 0
 delete_misses 0
 evictions 0
 get_hits 744
 get_misses 3329
 incr_hits 0
 incr_misses 0
 limit_maxbytes 2147483648
 listen_disabled_num 0
 pid 14356
 pointer_size 64
 rusage_system 2.628164
 rusage_user 2.664166
 threads 4
 time 1366212222
 total_connections 3004
 total_items 546
 uptime 237459
 version 1.4.4
root@ns5001089 [~]#


Memcached PHP Extension

Install Pecl too:

yum install php-pear
yum install php-pecl-memcache

Then Start using pecl to fetch PHP Extensions:

pecl install memcache
 downloading memcache-2.2.7.tgz ...
 Starting to download memcache-2.2.7.tgz (36,459 bytes)
 ..........done: 36,459 bytes
 11 source files, building
 running: phpize
 Configuring for:
 PHP Api Version: 20090626
 Zend Module Api No: 20090626
 Zend Extension Api No: 220090626
 Enable memcache session handler support? [yes] :
 building in /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7
 running: /root/tmp/pear/memcache/configure --enable-memcache-session=yes
 checking for grep that handles long lines and -e... /bin/grep
 checking for egrep... /bin/grep -E
 checking for a sed that does not truncate output... /bin/sed
 checking for cc... cc
 checking for C compiler default output file name... a.out
 checking whether the C compiler works... yes
 checking whether we are cross compiling... no
 checking for suffix of executables...
 checking for suffix of object files... o
 checking whether we are using the GNU C compiler... yes
 checking whether cc accepts -g... yes
 checking for cc option to accept ISO C89... none needed
 checking how to run the C preprocessor... cc -E
 checking for icc... no
 checking for suncc... no
 checking whether cc understands -c and -o together... yes
 checking for system library directory... lib
 checking if compiler supports -R... no
 checking if compiler supports -Wl,-rpath,... yes
 checking build system type... x86_64-unknown-linux-gnu
 checking host system type... x86_64-unknown-linux-gnu
 checking target system type... x86_64-unknown-linux-gnu
 checking for PHP prefix... /usr/local
 checking for PHP includes... -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib
 checking for PHP extension directory... /usr/local/lib/php/extensions/no-debug-non-zts-20090626
 checking for PHP installed headers prefix... /usr/local/include/php
 checking if debug is enabled... no
 checking if zts is enabled... no
 checking for re2c... re2c
 checking for re2c version... 0.13.5 (ok)
 checking for gawk... gawk
 checking whether to enable memcache support... yes, shared
 checking whether to enable memcache session handler support... yes
 checking for the location of ZLIB... no
 checking for the location of zlib... /usr
 checking for session includes... /usr/local/include/php
 checking for memcache session support... enabled
 checking for ld used by cc... /usr/bin/ld
 checking if the linker (/usr/bin/ld) is GNU ld... yes
 checking for /usr/bin/ld option to reload object files... -r
 checking for BSD-compatible nm... /usr/bin/nm -B
 checking whether ln -s works... yes
 checking how to recognize dependent libraries... pass_all
 checking for ANSI C header files... yes
 checking for sys/types.h... yes
 checking for sys/stat.h... yes
 checking for stdlib.h... yes
 checking for string.h... yes
 checking for memory.h... yes
 checking for strings.h... yes
 checking for inttypes.h... yes
 checking for stdint.h... yes
 checking for unistd.h... yes
 checking dlfcn.h usability... yes
 checking dlfcn.h presence... yes
 checking for dlfcn.h... yes
 checking the maximum length of command line arguments... 1572864
 checking command to parse /usr/bin/nm -B output from cc object... ok
 checking for objdir... .libs
 checking for ar... ar
 checking for ranlib... ranlib
 checking for strip... strip
 checking if cc supports -fno-rtti -fno-exceptions... no
 checking for cc option to produce PIC... -fPIC
 checking if cc PIC flag -fPIC works... yes
 checking if cc static flag -static works... yes
 checking if cc supports -c -o file.o... yes
 checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
 checking whether -lc should be explicitly linked in... no
 checking dynamic linker characteristics... GNU/Linux
 checking how to hardcode library paths into programs... immediate
 checking whether stripping libraries is possible... yes
 checking if libtool supports shared libraries... yes
 checking whether to build shared libraries... yes
 checking whether to build static libraries... no
creating libtool
 appending configuration tag "CXX" to libtool
 configure: creating ./config.status
 config.status: creating config.h
 running: make
cc -shared .libs/memcache.o .libs/memcache_queue.o .libs/memcache_standard_hash.o .libs/memcache_consistent_hash.o .libs/memcache_session.o -Wl,-soname -Wl, -o .libs/
 (cd .libs && rm -f && ln -s ../
 /bin/sh /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7/libtool --mode=install cp ./ /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7/modules
 cp ./.libs/ /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7/modules/
 cp ./.libs/memcache.lai /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7/modules/
 PATH="$PATH:/sbin" ldconfig -n /root/tmp/pear/pear-build-rootkFeKkf/memcache-2.2.7/modules
 Libraries have been installed in:
If you ever happen to want to link against installed libraries
 in a given directory, LIBDIR, you must either use libtool, and
 specify the full pathname of the library, or use the `-LLIBDIR'
 flag during linking and do at least one of the following:
 - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
 during execution
 - add LIBDIR to the `LD_RUN_PATH' environment variable
 during linking
 - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
 - have your system administrator add LIBDIR to `/etc/'
See any operating system documentation about shared libraries for
 more information, such as the ld(1) and manual pages.
Build complete.
 Don't forget to run 'make test'.
running: make INSTALL_ROOT="/root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7" install
 Installing shared extensions: /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib/php/extensions/no-debug-non-zts-20090626/
 running: find "/root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7" | xargs ls -dils
 1199741 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7
 1199770 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr
 1199771 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local
 1199772 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib
 1199773 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib/php
 1199774 4 drwxr-xr-x 3 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib/php/extensions
 1199775 4 drwxr-xr-x 2 root root 4096 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib/php/extensions/no-debug-non-zts-20090626
 1199769 248 -rwxr-xr-x 1 root root 246440 Apr 17 10:32 /root/tmp/pear/pear-build-rootkFeKkf/install-memcache-2.2.7/usr/local/lib/php/extensions/no-debug-non-zts-20090626/
 Build process completed successfully
 Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/'
 install ok: channel://
 Extension memcache enabled in php.ini

Restart Apache:

/etc/init.d/httpd   restart

Check Memcached plugin in PHP:

php -i | grep memcach
memcache support => enabled
memcache.allow_failover => 1 => 1
memcache.chunk_size => 8192 => 8192
memcache.default_port => 11211 => 11211
memcache.default_timeout_ms => 1000 => 1000
memcache.hash_function => crc32 => crc32
memcache.hash_strategy => standard => standard
memcache.max_failover_attempts => 20 => 20
Registered save handlers => files user sqlite memcache