Apr 112012
 

When I reviewed memcached previously, I got faster performance with wp-cache than memcached…..but that was about 16 months ago.  So I decided to give it another run for the money on a few sites.  The installation is pretty simple….especially since I scripted most of it for you…..

#!/bin/bash
 
# By Ed Wiget
 
# Use this version of Libevent
LIBEVENT_VERSION='2.0.18'
# Use this version of Memcached
MEMCACHED_VERSION='1.4.13'
# Use this version of Memcache - note issues with 3.0.6
MEMCACHE_VERSION='3.0.5'
# directory to download and compile stuff
DLDIR=/tmp
# set lib directory to 64 bit
LIBDIR='lib64'
# number of cpu cores
CPUS=`cat "/proc/cpuinfo" | grep "processor"|wc -l`
# number of threads to use when compiling
MAKETHREADS=" -j$CPUS"
 
echo "*************************************************"
echo "* Installing memcached"
echo "*************************************************"
echo "Downloading memcached..."
cd ${DLDIR}
 
if [ -s libevent-${LIBEVENT_VERSION}-stable.tar.gz ]; then
    echo "libevent-${LIBEVENT_VERSION} Archive found, skipping download..."
else
    wget -c http://www.monkey.org/~provos/libevent-${LIBEVENT_VERSION}-stable.tar.gz --tries=3
fi
 
if [ -s memcached-${MEMCACHED_VERSION}.tar.gz ]; then
    echo "memcached ${MEMCACHED_VERSION} Archive found, skipping download..."
else
    wget -c http://memcached.googlecode.com/files/memcached-${MEMCACHED_VERSION}.tar.gz --tries=3
fi
 
if [ -s memcache-${MEMCACHE_VERSION}.tgz ]; then
    echo "memcache ${MEMCACHE_VERSION} Archive found, skipping download..."
else
    wget -c http://pecl.php.net/get/memcache-${MEMCACHE_VERSION}.tgz --tries=3
fi
 
echo "Compiling libevent..."
 
cd ${DLDIR}
tar xfz libevent-${LIBEVENT_VERSION}-stable.tar.gz
cd libevent-${LIBEVENT_VERSION}-stable
./configure --prefix=/usr/${LIBDIR}
make$MAKETHREADS && make install
 
echo "/usr/${LIBDIR}/lib/" > /etc/ld.so.conf.d/libevent-i386.conf
ldconfig
 
echo "Compiling memcached..."
 
cd ${DLDIR}
tar xzf memcached-${MEMCACHED_VERSION}.tar.gz
cd memcached-${MEMCACHED_VERSION}
./configure --with-libevent=/usr/${LIBDIR}
make$MAKETHREADS && make install
cp ${DLDIR}/memcached-${MEMCACHED_VERSION}/scripts/memcached-tool /usr/local/bin
 
echo "Compiling memcached..."
 
cd ${DLDIR}
cat > /etc/init.d/memcached << EOF
#!/bin/sh
# chkconfig: - 80 12
# description:  The memcached daemon is a network memory cache service.
# processname: memcached
BIN=/usr/local/bin/memcached
USER=nobody
CON=2048
THREADS=4
MINSP=48
CHUNKF=1.05
PORT1=11211
PORT2=11212
MEMSIZE=8
SERVERCOUNT=1
 
if [ -f /proc/user_beancounters ]; then
ulimit -s 256
fi
 
if [ "$SERVERCOUNT" == '2' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT2 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
elif  [ "$SERVERCOUNT" == '1' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
fi
 
case "$1" in
start)
 
if [ "$SERVERCOUNT" == '2' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT2 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
elif  [ "$SERVERCOUNT" == '1' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
fi
 
;;
stop) 
 
killall $BIN
 
;;
restart)
killall $BIN
 
if [ "$SERVERCOUNT" == '2' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT2 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
elif  [ "$SERVERCOUNT" == '1' ]; then
 
$BIN -d -m $MEMSIZE -l 127.0.0.1 -p $PORT1 -c $CON -t $THREADS -n $MINSP -f $CHUNKF -u $USER
 
fi
 
;;
esac
EOF
chmod +x /etc/init.d/memcached
chkconfig --add memcached
chkconfig --level 345 memcached on
service memcached start
 
echo "Compiling PHP memcache extension..."
 
cd ${DLDIR}
tar -xvf memcache-${MEMCACHE_VERSION}.tgz
cd memcache-${MEMCACHE_VERSION}
/usr/local/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/bin/php-config
make$MAKETHREADS && make install
echo "" >> /usr/local/lib/php.ini
echo "extension=/usr/local/lib/php/extensions/no-debug-non-zts-20090626/memcache.so" >> /usr/local/lib/php.ini
 
/etc/init.d/php-fpm restart
 
echo ""
echo "*************************************************"
echo "Setup memcached.php admin page ..."
echo "*************************************************"
 
cp -a memcache.php /usr/local/nginx/html
chown -R nginx:nginx /usr/local/nginx/html
chmod 644 /usr/local/nginx/html/memcache.php
 
sed -i "s/'ADMIN_USERNAME','memcache'/'ADMIN_USERNAME','memcacheuser'/g" /usr/local/nginx/html/memcache.php
sed -i "s/'ADMIN_PASSWORD','password'/'ADMIN_PASSWORD','memcachepass'/g" /usr/local/nginx/html/memcache.php
sed -i "s/mymemcache-server1:11211/localhost:11211/g" /usr/local/nginx/html/memcache.php
sed -i "s/mymemcache-server2:11211/localhost:11212/g" /usr/local/nginx/html/memcache.php
 
echo "Setup Memcached Server Admin Login Details for /usr/local/nginx/html/memcache.php"
echo -n "(Type username your want to set and press Enter): "
read memcacheduser
echo -n "(Type password your want to set and press Enter): "
read memcachedpassword
 
echo "current memcached username: `grep "'ADMIN_USERNAME','memcacheuser'" /usr/local/nginx/html/memcache.php | sed -e "s/define('ADMIN_USERNAME','//" | sed -e 's/\/\/ Admin Username//' | sed 's/^[ \t]*//;s/[ \t]*$//' | sed -e "s/');//"`"
echo "current memcached password: `grep "'ADMIN_PASSWORD','memcachepass'" /usr/local/nginx/html/memcache.php | sed -e "s/define('ADMIN_PASSWORD','//" | sed -e 's/\/\/ Admin Password//' | sed 's/^[ \t]*//;s/[ \t]*$//' | sed -e "s/');//"`"
 
sed -i "s/'ADMIN_USERNAME','memcacheuser'/'ADMIN_USERNAME','${memcacheduser}'/g" /usr/local/nginx/html/memcache.php
sed -i "s/'ADMIN_PASSWORD','memcachepass'/'ADMIN_PASSWORD','${memcachedpassword}'/g" /usr/local/nginx/html/memcache.php
 
echo "new memcached username: ${memcacheduser}"
echo "new memcached password: ${memcachedpassword}"
 
echo "*************************************************"
echo "* memcached installed"
echo "*************************************************"

After you have memcached installed, next is configuring wordpress.

Install the wordpress plugin Memcached

Copy the file wp-content/plugins/memcached/object-cache.php wp-content/object-cache.php

edit wp-content/object-cache.php if you are using multiple memcached servers or different ports to define each and set:

$port = 11211;
$buckets = array('127.0.0.1');

or you can also set the values in wp-config.php file at the very end….the default values are for localhost, if it is an external host, change 127.0.0.1 to the hosts ip address.

global $memcached_servers;
$memcached_servers = array(‘default’ => array(‘127.0.0.1:11211′));

If you don’t want to use the memcache.php page, there is another utility called memcached-top which you can grab from:

wget http://memcache-top.googlecode.com/files/memcache-top-v0.6
chmod +x memcache-top-v0.6
./memcache-top-v0.6

This site uses Akismet to reduce spam. Learn how your comment data is processed.