php有两个memcache模块,一个是php-memcache,一个是php-memcached,php-memcached是最新的,也是比较稳定的,网上的太多的资料都是关于php-memcache的,太少提到php-memcached,而php-memcached的配置跟php-memcache是有所不同的。我的环境是多台webServer,全安装了nginx+php,3台memcached 服务器。
webserver_IP:192.168.10.9----22
memcachedserver_IP:192.168.10.23--25
一、先安装好web服务器,LNMP一键安装,此处就不多加强调了。
二、安装memcached服务器。
三、在所有的webServer上安装php扩展,
安装包:
memcached 服务器端所需:
libevent-2.0.21-stable.tar.gz
memcached-1.4.24.tar.gz
支持PHP扩展所需:
libmemcached-1.0.18.tar.gz
memcached-2.2.0.tgz
在memcached服务器端:
1、安装libevent-2.0.21-stable
wget http://nchc.dl.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz
tar xzvf libevent-1.4.9-stable.tar.gz
cd libevent-1.4.10-stable
./configure --prefix=/usr
make && make install
2、安装memcached-1.4.24
wget http://www.memcached.org/files//memcached-1.4.24.tar.gz
cd /software
tar xzvf memcached-1.4.24.tar.gz
cd memcached-1.4.24
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/
make && make install
3、启动memcached
/usr/local/memcached/bin/memcached -d -m 10000 -p 11211 -u root -c 4096 -P /usr/local/memcached/logs/memcached.pid
执行后,远程登录任意一台memcached服务器,可以用以下办法查看执行状态
telnet 192.168.10.23 11211
stats
STAT pid 11568
STAT uptime 5910
STAT time 1241876672
STAT version 1.2.8
STAT pointer_size 32
STAT rusage_user 0.751885
STAT rusage_system 1.584759
STAT curr_items 2615
STAT total_items 36753
STAT bytes 244835
STAT curr_connections 3
STAT total_connections 18427
STAT connection_structures 36
STAT cmd_flush 0
STAT cmd_get 18388
STAT cmd_set 38334
STAT get_hits 15717
STAT get_misses 2671
STAT evictions 0
STAT bytes_read 4924864
STAT bytes_written 1992526
STAT limit_maxbytes 134217728
STAT threads 2
STAT accepting_conns 1
STAT listen_disabled_num 0
END
有以上信息出现,表明启动成功!
4、加入开机启动
vim /etc/rc.local
#最后一行加入:
/usr/local/memcached/bin/memcached -d -m 10000 -p 11211 -u root -c 4096 -P /usr/local/memcached/logs/memcached.pid
在webserver上安装,实现扩展
1、安装zlib
yum install zlib
2、安装libmemcached
wget https://launchpadlibrarian.net/165454254/libmemcached-1.0.18.tar.gz
tar zxvf libmemcached-0.26.tar.gz
cd libmemcached-0.26
./configure --prefix=/usr/local/libmemcached/
make && make install
3、安装php-memcached模块
wget http://pecl.php.net/get/memcached-2.2.0.tgz
2.2.0 | stable | 2014-04-01 | memcached-2.2.0.tgz (68.8kB) |
---|
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
phpize
./configure --with-php-config=/usr/bin/php-config --enable-memcache --with-libmemcached-dir=/usr/local/libmemcached
make
make install
make install 结束后:
[root@host23 memcached-2.2.0]# make install
Installing shared extensions: /Data/app/php/lib/php/extensions/no-debug-non-zts-20131226/
4、编辑php.ini
vim /etc/php.ini
修改extension_dir的路径为你的
默认的是:
extension_dir= "./"
可以修改为:
extension_dir= "./"
在文件最后加入:
extension = "/Data/app/php/lib/php/extensions/no-debug-non-zts-20131226/"
也可以不修改extension的路径,直接在最后添加:
extension = "/Data/app/php/lib/php/extensions/no-debug-non-zts-20131226/memcached.so"
修改php.ini
session.save_handler = files -> session.save_handler = memcached
session.save_path = "/var/lib/php/session" -> session.save_path = "192.168.10.23:11211;/opt/"
9、重启apache
service httpd restart
测试:
vim test.php
<?php
$memcache_obj = new Memcached();
$memcache_obj->addServer('192.168.1.23', 11211);
$memcache_obj->set('key','memcache 测试!',false, 10) or die ("Failed to save data at the server");
echo $memcache_obj->get('key');
?>
:wq
# /usr/local/php/bin/php test.php
[root@host9 myscripts]# /Data/app/php5.6.11/bin/php testmem2.php
memcache 测试!
[root@host9 myscripts]#
[root@host9 myscripts]# vim testmem2.php 将192.168.1.23改成一错的,192.168.1.231,保存再测试。
[root@host9 myscripts]# /Data/app/php5.6.11/bin/php testmem2.php
Failed to save data at the server
[root@host9 myscripts]#
1、安装libmemcached需要先安装zlib
2、php.ini文件的配置,php-memcached模块跟php-memcache不同,要设成是memcached,这个在phpinfo页面的Registered save handlers可以看到。如果设置不正确,会有"Cannot find save handler memcache"的错误提示。
3、session.save_path几乎所有的资料都是session.save_path = "tcp://127.0.0.1:11211",但这可能在memcaceh是这样用的,我没有验证,我一开始也是这样设置,但一直出现"Failed to write session data (memcached)"的错误,找了很入也没找到是什么原因,后来在php.net上找到
session.save_path string
Defines a comma separated of hostname:port entries to use for session server pool, for example"sess1:11211, sess2:11211".
这段说明,于是去掉tcp://再试,才终于成功!