Author: nathacof
Published: Monday 1st of June 2009
The first thing to do is install the libevent libraries from your distributions repositories:
[root@dev ~]# yum list libevent* Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * base: ftp.lug.udel.edu * updates: centos.aol.com * addons: centos.mirror.nac.net * extras: centos.mirror.nac.net Installed Packages libevent.i386 1.1a-3.2.1 installed libevent.x86_64 1.1a-3.2.1 installed libevent-devel.i386 1.1a-3.2.1 installed libevent-devel.x86_64 1.1a-3.2.1 installed
Next we need to install memcached! Grab the latest source from the memcached website.
[nathacof@dev src]$ wget http://memcached.googlecode.com/files/memcached-1.2.8.tar.gz [nathacof@dev src]$ tar xzf memcached-1.2.8.tar.gz [nathacof@dev src]$ cd memcached-1.2.8 [nathacof@dev memcached-1.2.8]$ ./configure --enable-64bit --enable-threads && make && sudo make install
Next we need a startup script for memcached . Memcached comes with a number of startup scripts in the scripts/ directory but I opted to write my own based off the standard CentOS service control scripts. :P
#!/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=128 # Default 64 MB
port=11211 # Change this, memcached has no security
# Values based on CentOS 5.x
PID=/var/run/memcached/memcached.pid
LOCK=/var/lock/subsys/memcached
start(){
$prog -d -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 $?
We'll copy this to the /etc/init.d directory. We also need to create a user, and a directory.
[nathacof@dev src]$ sudo cp memcached.sh /etc/init.d/memcached [nathacof@dev src]$ sudo /usr/sbin/adduser -M memcached -s /sbin/nologin -u 10000 [nathacof@dev src]$ sudo mkdir /var/run/memcached/ && sudo chown memcached:memcached /var/run/memcached/
Next we need to startup the service, and ensure that our server will start this service on the next reboot.
[nathacof@dev src]$ sudo /sbin/chkconfig --level 345 memcached on [nathacof@dev src]$ sudo /sbin/service memcached start Starting /usr/local/bin/memcached: [ OK ]
Let's check that the server is running.
[nathacof@dev src]$ sudo /sbin/service memcached status memcached (pid 9516) is running... [nathacof@dev src]$ sudo /usr/sbin/lsof -ai 4 -p 9516 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME memcached 9516 memcached 7u IPv4 209971 TCP *:11211 (LISTEN) memcached 9516 memcached 9u IPv4 209976 UDP *:11211
Great! We've got all the confirmation we need to ensure that Memcached is running! That's all there is to it. See the Memcached wiki for more information.
