Install & Configure memcached on EC2 Amazon Linux

Install Memcached on EC2 Amazon Linux

sudo yum install libevent-devel
cd /usr/local/src
sudo wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
sudo tar zxpfv memcached*
cd memcached*
sudo ./configure --enable-64bit
sudo make
sudo make install clean
sudo rm /usr/local/src/memcached-1.4.5.tar.gz

Memcached Auto Start

sudo vi /etc/init.d/memcached
#!/bin/bash
#
# memcached     This shell script takes care of starting and stopping
#               the MemcacheD service (memcached).
#
# chkconfig: - 60 30
# description:  MemcacheD server.
# processname: memcached
# pidfile: /var/run/memcached/memcached.pid

# Source function library.
. /etc/rc.d/init.d/functions

prog=/usr/local/bin/memcached
username=memcached # Avoid running as root
memory_max=256 # Default 64 MB
port=11211
host=127.0.0.1

# Values based on CentOS 5.x
PID=/var/run/memcached/memcached.pid
LOCK=/var/lock/subsys/memcached

start(){
        $prog -d -l $host -u $username -m $memory_max -P $PID > /dev/null 2>&1 &
        ret=$?
        if [ $ret -eq 0 ] && touch $LOCK;
        then
                action $"Starting $prog: " /bin/true
        else
                ret=1
                action $"Startng $prog: " /bin/false
        fi
        return $ret
}


stop(){
        MEMCACHEDPID=$(cat "$PID"  2>/dev/null)
        if [ -n "$MEMCACHEDPID" ]; then
            /bin/kill "$MEMCACHEDPID" > /dev/null 2>&1
            ret=$?
            if [ $ret -eq 0 ]; then
                STOPTIMEOUT=60
                while [ $STOPTIMEOUT -gt 0 ]; do
                    /bin/kill -0 "$MEMCACHEDPID" > /dev/null 2>&1 || break
                    sleep 1
                    let STOPTIMEOUT=${STOPTIMEOUT}-1
                done
                if [ $STOPTIMEOUT -eq 0 ]; then
                    echo "Timeout error occurred trying to stop MemcacheD Daemon."
                    ret=1
                    action $"Stopping $prog: " /bin/false
                else
                    rm -f $LOCK
                    action $"Stopping $prog: " /bin/true
                fi
            else
                action $"Stopping $prog: " /bin/false
            fi
        else
            ret=1
            action $"Stopping $prog: " /bin/false
        fi
        return $ret

}

restart(){
        start
        stop
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status memcached
    ;;
  restart)
    restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit $?
sudo adduser -M memcached -s /sbin/nologin
sudo mkdir /var/run/memcached/ && sudo chown memcached:memcached /var/run/memcached/
sudo chmod +x /etc/init.d/memcached
sudo chkconfig --level 345 memcached on

To start/stop memcached

sudo service memcached start
sudo service memcached stop

Perl Memcache

sudo yum install openssl-devel
sudo pecl install memcache

Enable extenstion

sudo vi /etc/php.d/memcache.ini
; Enable memcache extension module
extension=memcache.so