memcache installed on CentOS 64 PHP 5.X

How I had memcache installed on a CentOS 64 PHP 5.X server using bash .


-------------------------
cd /src
# Prerequisite Install(memcached dependency)
-------------------------
# Download & install libevent
wget http://monkey.org/~provos/libevent-2.0.12-stable.tar.gz
tar xfz libevent-2.0.12-stable.tar.gz
cd libevent-2.0.12-stable
./configure && make && make install

cd /src
# Download & install memcached
wget http://memcached.googlecode.com/files/memcached-1.4.6.tar.gz
tar xfz memcached-1.4.6.tar.gz
cd memcached-1.4.6
./configure && make && make install


It is not uncommon that libevent.so cannot be found when executing memcache. If so, a very useful command, LD_DEBUG, can help us to determine where libraries are being loaded from.


LD DEBUG=libs memcached - v 2>&1 > /dev/null I less
3224: find library=libevent-2.0.so.5 [0]; searching
3224: search cache=/etc/ld.so.cache
3224: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path) 3224: trying file=/lib64/tls/x86_64/libevent-2.0.so.5
3224: trying file=/lib64/tls/libevent-2.0.so.5
3224: trying file=/lib64/x86_64/libevent-2.0.so.5
3224: trying file=/lib64/libevent-2.0.so.5
3224: trying file=/usr/lib64/tls/x86_64/libevent-2.0.so.5
3224: trying file=/usr/lib64/tls/libevent-2.0.so.5
3224: trying file=/usr/lib64/x86_64/libevent-2.0.so.5
3224: trying file=/usr/lib64/libevent-2.0.so.5
3224: memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory.


With this information, now we can create a symbolic link to libevent, basically, we place the library where memcached will find it:


# Create a symlink to libevent

ln -s /usr/local/lib/libevent-2.0.so.5 /lib64/

# Run memcached as a daemon (d = daemon, m = memory, u = user, l = IP to listen to, p = port)

memcached -d -m 1024 -u root -l 127.0.0.1 -p 11211


Now, we need to install the package for PHP, ie, compile the client. There are two different ways to achieve that:


--------------------------------------------------
# PHP5-Memcache Install as a module using yum
--------------------------------------------------
yum install php-pecl-memcache.x86_64


Yum is the nice way to do it since it will check for dependencies and usally it will solve them, however, you may want to install a more recent version than the one in the reporsitory, there are other ways to have memcache installed as a PHP extension:


--------------------------------------------------
# PHP5-Memcache Install as an extension
--------------------------------------------------
# Download the extension module
cd /src
wget http://pecl.php.net/get/memcache-2.2.6.tgz
tar xfz memcache-2.2.6.tgz
cd memcache-2.2.6
phpize
./configure & make & make install


As for the last step, we need to instruct PHP to load the extension, if you used yum for install, chances are yum created an ini file for you, look for /etc/php.d/memcache.ini, otherwise, add the following line to your php.ini, usually at /etc/php.ini:


extension=memcache.so


You might need to edit /etc/php.d/memcache.ini (or /etc/php5/conf.d/memcache.ini ) and uncomment the following line by removing the semi-colon:

extension=memcache.so


Restart apache, usually you could use something like:

service httpd restart


And, of course, after all this hard work you may want to test if memcache is effectively working, a little PHP script should do the trick:



<?php
//memcached simple test
$memcache = new Memcache ;
$memcache -> connect ( 'localhost' , 11211 ) or die ( 'Could not connect to memcache server' ) ;
$key = md5 ( 'a_unique_key' ) ;
$time_queried_first_time = time () ;
for (
$k = 0 ; $k < 2 ; $k ++) {
$data = $memcache -> get ( $key ) ;
if (
$data == NULL ) {
$data = array () ;
echo
'Query at: ' . $time_queried_first_time . ' :: ' ;
for (
$i = 0 ; $i < 10 ; $i ++) {
for (
$j = 0 ; $j < 10 ; $j ++) {
$data [ $i ][ $j ] = mt_rand ( 0 , 99 ) ;
}
}
$memcache -> set ( $key , $data , 0 , 60 ) ;
}
else {
var_dump ( $data ) ;
}
}
?>