构建LNMP+memcached服务

通过PHP页面实现对memcached服务器的数据操作,实现以下目标:

- 为PHP安装memcache扩展

- 创建PHP页面,并编写PHP代码,实现对memcached的数据操作

环境:部署LNMP+memcached网站平台参考之前的随笔,这里不做描述

方案:如果希望使用PHP来操作memcached,必须要为PHP安装memcache扩展(php-pecl-memcache),否则PHP无法解析连接memcached的指令。另外,客户端测试时需要提前安装telnet远程工具.

拓扑图:

1. 在后端服务器web1上创建PHP首页文档mem.php ,用于测试

   [root@web1 ~]# vim /usr/local/nginx/html/mem.php

   <?php

   $memcache=new Memcache;    #创建memcache对象

   $memcache->connect('192.168.2.5',11211) or die ('could not connect!!');

   $memcache->set('key','test');    #定义变量

   $get_values=$memcache->get('key');    #获取变量值

   echo $get_values;

   ?>

2. 客户端浏览器访问服务器PHP首页文档

    [root@client ~]# yum -y install telnet   #安装远程连接工具

    [root@client ~]# firefox http://192.168.2.100/mem.php     #这里访问会失败, 因为没有给PHP安装扩展包,默认PHP无法连接memcached数据库,需要给PHP安装扩展模块

    才可以连接memcached数据库.

3. 为PHP添加memcache扩展

   [root@web1 ~]# yum -y install php-pecl-memcache

   [root@web1 ~]# systemctl restart php-fpm

4. 客户端再次测试

   [root@client ~]# firefox http://192.168.2.100/mem.php   #会成功显示PHP首页文档

结束.

 

posted @ 2019-07-06 20:22  巴州夜雨  阅读(207)  评论(0编辑  收藏  举报