为LNMP安装memcached

准备工作

先用yum安装必要的library

1
yum install libevent libevent-devel

下载memcached和php的扩展memcache

1
2
wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
wget http://pecl.php.net/get/memcache-2.2.6.tgz

安装Memcached

要令php使用memcache,需要先安装memcached服务。

1
2
3
4
5
tar -zxvf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/usr/local/webserver/memcached
make
make install

安装完成后,要添加运行script令其开机自动启动,输入vi /etc/init.d/memcached,然后输入以下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Init file for memcached
#
# chkconfig: - 80 12
# description: Distributed memory caching daemon
#
# processname: memcached
# config: /etc/sysconfig/memcached
# config: /etc/memcached.conf
source /etc/rc.d/init.d/functions
### Default variables
PORT='11211'
USER='nobody'
MAXCONN='1024'
CACHESIZE='64'
OPTIONS=''
#please note the location
PIDLOCATE='/usr/local/webserver/memcached'
### Read configuration
#[ -r '$SYSCONFIG' ] && source '$SYSCONFIG'
RETVAL=0
#prog='memcached'
#please note the location
prog='/usr/local/webserver/memcached/bin/memcached'
desc='Distributed memory caching'
start() {
echo -n $'Starting $desc ($prog): '
daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE -P $PIDLOCATE/memcached.pid
RETVAL=$?
echo
#[ $RETVAL -eq 0 ] && touch $SYSCONFIG/memcached.pid
return $RETVAL
}
stop() {
echo -n $'Shutting down $desc ($prog): '
killproc $prog
RETVAL=$?
echo
#[ $RETVAL -eq 0 ] && rm -f $SYSCONFIG/memcached.pid
return $RETVAL
}
restart() {
stop
start
}
reload() {
echo -n $'Reloading $desc ($prog): '
killproc $prog -HUP
RETVAL=$?
echo
return $RETVAL
}
case '$1' in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
#[ -e /var/lock/subsys/$prog ] && restart
restart
RETVAL=$?
;;
reload)
reload
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $'Usage: $0 {start|stop|restart|condrestart|status}'
RETVAL=1
esac
exit $RETVA

注意在此脚本修改安装memcached后的目录。保存后,运行以下命令添加自动运行,并运行memcached

1
2
chkconfig memcached on
service memcached start

为PHP安装memcache

进入源代码目录后,解压安装memcache extension

1
2
3
4
5
6
tar vxzf memcache-2.2.6.tgz
cd memcache-2.2.6
/usr/local/webserver/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/webserver/php/bin/php-config --with-zlib-dir
make
make install

然后就是修改 /usr/local/webserver/php/etc/ 目录下的php.ini,添加memcache模块

extension = "memcache.so"