LNMP环境添加Memcache模块
1.安装依赖库(libevent)
[root@localhost tong]# tar xvf libevent-1.4.13-stable.tar.gz
[root@localhost tong]# cd libevent-1.4.13-stable [root@localhost libevent-1.4.13-stable]# ./configure --prefix=/usr/local/test/libevent
[root@localhost libevent-1.4.13-stable]# make
[root@localhost libevent-1.4.13-stable]# make install
[root@localhost libevent-1.4.13-stable]# echo $? 0 [root@localhost libevent-1.4.13-stable]#
2.安装memcached服务端
[root@localhost libevent-1.4.13-stable]# cd .. [root@localhost tong]# tar xvf memcached-1.4.20.tar.tar
[root@localhost tong]# cd memcached-1.4.20 [root@localhost memcached-1.4.20]# ./configure --prefix=/usr/local/test/memcached --with-libevent=/usr/local/test/libevent/
[root@localhost memcached-1.4.20]# make
[root@localhost memcached-1.4.20]# make install
[root@localhost memcached-1.4.20]# echo $? 0 [root@localhost memcached-1.4.20]#
3.安装memcache客户端
[root@localhost memcached-1.4.20]# cd .. [root@localhost tong]# tar xvf memcache-3.0.6.tgz
[root@localhost memcache-3.0.6]# ./configure --enable-memcache --with-php-config=/usr/local/test/php/bin/php-config --with-zlib-dir
[root@localhost memcache-3.0.6]# make
[root@localhost memcache-3.0.6]# make install
Installing shared extensions: /usr/local/test/php/lib/php/extensions/debug-non-zts-20090626/ --记下这个路径
[root@localhost memcache-3.0.6]# echo $? 0 [root@localhost memcache-3.0.6]# vim /usr/local/test/php/lib/php.ini --进入php.ini配置文件添加如下两行
extension_dir = "/usr/local/test/php/lib/php/extensions/debug-non-zts-20090626/" extension=memcache.so
4.启动memcached服务
memcached -d -m 10 -u root -l 202.207.177.177 -p 11211 -c 256 -P /tmp/memcached.pid 参数说明: -d选项是启动一个守护进程, -m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB, -u是运行Memcache的用户,我这里是root, -l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址202.207.177.177, -p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口, -c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定, -P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid.
5.查看是否启动
[root@localhost memcache-3.0.6]# netstat -anp |grep mem tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 9565/memcached tcp 0 0 :::11211 :::* LISTEN 9565/memcached udp 0 0 0.0.0.0:11211 0.0.0.0:* 9565/memcached udp 0 0 :::11211 :::*
6.测试
[root@localhost memcache-3.0.6]# cd /usr/local/test/nginx/html/ [root@localhost html]# vim test.php
<?php
$mem = new Memcache;
$mem->connect("202.207.177.177", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>