1、概述
memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问压力。
memcache是php的一个扩展模块,用于实现php连接memcached服务器进行交互式协作,从而实现php web程序对memcached缓冲服务器上数据的增删改查操作(一般情况下查的功能比较多)。
2、LNMP环境安装memcache模块
[root@fnw ~]# tar xf memcache-3.0.8-for-php5.5.9.tgz [root@fnw ~]# cd memcache-3.0.8 [root@fnw memcache-3.0.8]# /usr/local/php5.5.30/bin/phpize [root@fnw memcache-3.0.8]# ./configure --enable-memcache --with-php-config=/usr/local/php5.5.30/bin/php-config --with-zlib-dir [root@fnw memcache-3.0.8]# make && make install [root@fnw memcache-3.0.8]# cd .. [root@fnw ~]#
3、在php中配置memcache模块
#切换到php5.5.30的安装目录中
[root@fnw memcache-3.0.8]# cd /usr/local/php5.5.30/
#查找php的配置文件
[root@fnw php5.5.30]# find ./ -name php.ini ./lib/php.ini
#查找memcache模块的安装位置
[root@fnw php5.5.30]# find ./ -name memcache* ./lib/php/extensions/no-debug-non-zts-20121212/memcache.so
#用vim编辑器查找extension_dir并编辑插件的模块路劲
[root@fnw php5.5.30]# vim lib/php.ini extension_dir="/usr/local/php5.5.30/lib/php/extensions/no-debug-non-zts-20121212/" extension="memcache.so"
4、用php-fpm启动php进程
#检查php语法
[root@fnw php5.5.30]# /usr/local/php5.5.30/sbin/php-fpm -t -c /usr/local/php5.5.30/lib/php.ini [02-Nov-2015 13:50:28] NOTICE: configuration file /usr/local/php5.5.30/etc/php-fpm.conf test is successful
#启动php服务进程
[root@fnw php5.5.30]# /usr/local/php5.5.30/sbin/php-fpm -c /usr/local/php5.5.30/lib/php.ini [root@fnw php5.5.30]# netstat -tulnp | grep php tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 9903/php-fpm
5、启动nginx进程
#检查nginx语法
[root@fnw php5.5.30]# /usr/local/nginx1.8/nginx -t nginx: the configuration file /usr/local/nginx1.8/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.8/conf/nginx.conf test is successful
#启动nginx服务
[root@fnw php5.5.30]# /usr/local/nginx1.8/nginx [root@fnw php5.5.30]# netstat -tulnp |grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9922/nginx
6、测试nginx,php,memcache连接memcached缓存服务器
#配置phpinfo()函数,在浏览器输入web服务器的ip地址,测试php是否成功加载memcache模块
[root@fnw php5.5.30]# curl 192.168.1.167/phpinfo.php | grep memcache % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<h2><a name="module_memcache">memcache</a></h2> <tr class="h"><th>memcache support</th><th>enabled</th></tr> <tr><td class="e">memcache.allow_failover</td><td class="v">1</td><td class="v">1</td></tr> <tr><td class="e">memcache.chunk_size</td><td class="v">32768</td><td class="v">32768</td></tr> <tr><td class="e">memcache.compress_threshold</td><td class="v">20000</td><td class="v">20000</td></tr> <tr><td class="e">memcache.default_port</td><td class="v">11211</td><td class="v">11211</td></tr> <tr><td class="e">memcache.hash_function</td><td class="v">crc32</td><td class="v">crc32</td></tr> <tr><td class="e">memcache.hash_strategy</td><td class="v">consistent</td><td class="v">consistent</td></tr> <tr><td class="e">memcache.lock_timeout</td><td class="v">15</td><td class="v">15</td></tr> <tr><td class="e">memcache.max_failover_attempts</td><td class="v">20</td><td class="v">20</td></tr> <tr><td class="e">memcache.protocol</td><td class="v">ascii</td><td class="v">ascii</td></tr> <tr><td class="e">memcache.redundancy</td><td class="v">1</td><td class="v">1</td></tr> <tr><td class="e">memcache.session_redundancy</td><td class="v">2</td><td class="v">2</td></tr> <tr><td class="e">Registered save handlers </td><td class="v">files user memcache </td></tr> 100 67072 0 67072 0 0 1029k 0 --:--:-- --:--:-- --:--:-- 1091k
#用telnet工具测试memcached服务器是否开启
[root@fnw php5.5.30]# telnet 192.168.1.2 11211 Trying 192.168.1.2... Connected to 192.168.1.2. Escape character is '^]'. quit Connection closed by foreign host.
#编写php测试程序
[root@fnw php5.5.30]# cd ../nginx1.8/html [root@fnw ~]# vim index.php <?php $memcache = new Memcache; $memcache->connect('192.168.1.2', 11211) or die ("Could not connect"); $version = $memcache->getVersion(); echo "Server's version: ".$version."<br/>\n"; $memcache->set('mykey', "liangge", false, 0) or die ("Failed to save data at the server"); echo "Store data in the cache <br/>\n"; $myvalue = $memcache->get('mykey'); echo "Data from the cache: "; echo $myvalue; echo "\n"; ?>
#用curl测试memcache模块是否成功连接memcached服务器
[root@fnw ~]# curl 192.168.1.167/ Server's version: 1.4.24<br/> Store data in the cache (data will expire in 10 seconds);<br/> Data from the cache: liangge [root@fnw ~]# ip addr show | grep "inet " #LNMP的客户机的IP地址 inet 127.0.0.1/8 scope host lo inet 192.168.1.167/24 brd 192.168.1.255 scope global eth1
#在服务器端用shell获取memcached缓存中的数据信息
[root@mysqldb1 ~]# printf "get mykey\r\n" | nc 192.168.1.2 11211 VALUE mykey 0 7 liangge END [root@mysqldb1 ~]# ip addr show | grep "inet " #memcached缓存服务器的IP地址 inet 127.0.0.1/8 scope host lo inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0 到此为止LNMP+memcache的安装已经全部搞定,希望我的博文对大家有帮助。如果有什么不懂地方联系QQ:877306754,欢迎大家前来学习交流。