OPERATION DAY04
案例1:构建memcached服务
案例2:LNMP+memcached
案例3:PHP的本地Session信息
案例4:PHP实现session共享
1 案例1:构建memcached服务
1.1 问题
本案例要求先快速搭建好一台memcached服务器,并对memcached进行简单的增、删、改、查操作:
- 安装memcached软件,并启动服务
- 使用telnet测试memcached服务
- 对memcached进行增、删、改、查等操作
1.2 方案
使用1台RHEL7虚拟机作为memcached服务器(192.168.4.5)。
在RHEL7系统光盘中包含有memcached,因此需要提前配置yum源,即可直接使用yum安装,客户端测试时需要提前安装telnet远程工具。
验证时需要客户端主机安装telnet,远程memcached来验证服务器的功能:
- add name 0 180 10 //变量不存在则添加
- set name 0 180 10 //添加或替换变量
- replace name 0 180 10 //替换
- get name //读取变量
- append name 0 180 10 //向变量中追加数据
- delete name //删除变量
- stats //查看状态
- flush_all //清空所有
- 提示:0表示不压缩,180为数据缓存时间,10为需要存储的数据字节数量。
1.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:构建memcached服务
1)使用yum安装软件包memcached
2) memcached配置文件
-
[[email protected] ~]# vim /usr/lib/systemd/system/memcached.service
- ExecStart=/usr/bin/memcached -u USER<spanclass="shsymbol"style="color:rgb(255,0,128);font−weight:bold;">−</span>pPORT -m CACHESIZE<spanclass="shsymbol"style="color:rgb(255,0,128);font−weight:bold;">−</span>cMAXCONN OPTIONS</li><li style="list-style:none outside;"></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># <span class="sh_normal">vim </span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">etc</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">sysconfig</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>memcached</li><li style="list-style:none outside;">PORT<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_string" style="color:rgb(166,133,0);">"11211"</span></li><li style="list-style:none outside;">USER<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_string" style="color:rgb(166,133,0);">"memcached"</span></li><li style="list-style:none outside;">MAXCONN<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_string" style="color:rgb(166,133,0);">"1024"</span></li><li style="list-style:none outside;">CACHESIZE<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_string" style="color:rgb(166,133,0);">"64"</span></li><li style="list-style:none outside;">OPTIONS<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_string" style="color:rgb(166,133,0);">""</span></li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">3)启动服务并查看网络连接状态验证是否开启成功:</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># systemctl start memcached</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># systemctl status memcached</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># netstat <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>anptu <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">|</span> grep memcached</li><li style="list-style:none outside;">tcp <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0.0.0.0</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:</span><span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">11211</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0.0.0.0</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:*</span> LISTEN <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">2839</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>memcached </li><li style="list-style:none outside;">tcp <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:::</span><span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">11211</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:::*</span> LISTEN <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">2839</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>memcached </li><li style="list-style:none outside;">udp <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0.0.0.0</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:</span><span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">11211</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0.0.0.0</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:*</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">2839</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>memcached </li><li style="list-style:none outside;">udp <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:::</span><span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">11211</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:::*</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">2839</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>memcached</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># setenforce <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># firewall<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>cmd <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">--</span>set<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span><span class="sh_keyword" style="color:rgb(187,121,119);font-weight:bold;">default</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>zone<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span>trusted</li></ol></div></div><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤二:使用telnet访问memcached服务器</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)使用yum安装telnet</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install telnet</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">2)使用telnet连接服务器测试memcached服务器功能,包括增、删、改、查等操作。</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># telnet <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">192.168.4.5</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">11211</span></li><li style="list-style:none outside;">Trying <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">192.168.4.5</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">...</span></li><li style="list-style:none outside;">……</li><li style="list-style:none outside;">set name <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">180</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">3</span> <span class="sh_comment" style="color:rgb(255,128,0);">//定义变量,变量名称为name</span></li><li style="list-style:none outside;">plj <span class="sh_comment" style="color:rgb(255,128,0);">//输入变量的值,值为plj </span></li><li style="list-style:none outside;">STORED</li><li style="list-style:none outside;">get name <span class="sh_comment" style="color:rgb(255,128,0);">//获取变量的值</span></li><li style="list-style:none outside;">VALUE name <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">3</span> <span class="sh_comment" style="color:rgb(255,128,0);">//输出结果</span></li><li style="list-style:none outside;">plj</li><li style="list-style:none outside;">END</li><li style="list-style:none outside;">add myname <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">180</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">10</span> <span class="sh_comment" style="color:rgb(255,128,0);">//新建,myname不存在则添加,存在则报错</span></li><li style="list-style:none outside;">set myname <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">180</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">10</span> <span class="sh_comment" style="color:rgb(255,128,0);">//添加或替换变量</span></li><li style="list-style:none outside;">replace myname <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">180</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">10</span> <span class="sh_comment" style="color:rgb(255,128,0);">//替换,如果myname不存在则报错</span></li><li style="list-style:none outside;">get myname <span class="sh_comment" style="color:rgb(255,128,0);">//读取变量</span></li><li style="list-style:none outside;">append myname <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">0</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">180</span> <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">10</span> <span class="sh_comment" style="color:rgb(255,128,0);">//向变量中追加数据</span></li><li style="list-style:none outside;"><span class="sh_keyword" style="color:rgb(187,121,119);font-weight:bold;">delete</span> myname <span class="sh_comment" style="color:rgb(255,128,0);">//删除变量</span></li><li style="list-style:none outside;">stats <span class="sh_comment" style="color:rgb(255,128,0);">//查看状态</span></li><li style="list-style:none outside;">flush_all <span class="sh_comment" style="color:rgb(255,128,0);">//清空所有</span></li><li style="list-style:none outside;">quit <span class="sh_comment" style="color:rgb(255,128,0);">//退出登录 </span></li></ol></div></div><a style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;" target="_blank"></a><span style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"></span><h2 style="font-size:20px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t6"></a>2 案例2:LNMP+memcached</h2><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t7"></a>2.1 问题</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">沿用练习一,部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作,实现以下目标:</p><ol class="list" style="margin-left:30px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><li>部署LNMP实现PHP动态网站架构</li><li>为PHP安装memcache扩展</li><li>创建PHP页面,并编写PHP代码,实现对memcached的数据操作</li></ol><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t8"></a>2.2 方案</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">使用2台RHEL7虚拟机,其中一台作为memcached及LNMP服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。</p><div class="image" style="text-align:center;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;">]<img src="https://img-blog.****.net/20180626090744178?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NDQxMDI3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt=""><p style="text-indent:21pt;font-size:12px;text-align:center;">图-1</p></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">在RHEL7系统光盘中包含有我们需要的MariaDB、PHP,我们需要使用源码安装Nginx,使用RPM包安装FPM。另外如果希望使用PHP来操作memcached,注意必须要为PHP安装memcache扩展(php-pecl-memcache),否则PHP无法解析连接memcached的指令。客户端测试时需要提前安装telnet远程工具。</p><h3 style="font-size:18px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';"><a name="t9"></a>2.3 步骤</h3><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">实现此案例需要按照如下步骤进行。</p><p class="number" style="text-indent:21pt;font-size:15px;font-weight:bold;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">步骤一:部署LNMP环境(如果环境中已经存在LNMP环境本步骤可以忽略)</p><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">1)使用yum安装基础依赖包</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install gcc openssl<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>devel pcre<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>devel zlib<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>devel</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">..</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">..</span></li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">2)源码安装Nginx</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># tar <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>xf nginx<span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">-1.12.2</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>tar<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>gz</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># cd nginx<span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">-1.12.2</span></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] nginx<span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">-1.12.2</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">]</span># <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">./</span>configure <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">\</span></li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">></span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">--</span><span class="sh_keyword" style="color:rgb(187,121,119);font-weight:bold;">with</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>http_ssl_module </li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] nginx<span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">-1.12.2</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">]</span># make <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">&&</span> make install</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">3)安装MariaDB数据库</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install mariadb mariadb<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>server mariadb<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>devel</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">4)安装PHP</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install php php<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>mysql</li><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install php<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>fpm<span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">-5.4.16-42</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>el7<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>x86_64<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>rpm</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">5)为PHP添加memcache扩展</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># yum <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>y install php<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>pecl<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-</span>memcache</li></ol></div></div><p style="text-indent:21pt;font-size:15px;color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';">6)修改Nginx配置文件</p><div class="snippet-container" style="color:rgb(0,0,0);font-family:'微软雅黑', 'Times New Roman';font-size:14px;"><div class="sh_acid snippet-wrap" style="background-image:none;border:0px none;"><ol class="snippet-num" style="margin-left:1em;"><li style="list-style:none outside;"><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">[</span>[email protected] <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~]</span># <span class="sh_normal">vim </span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">usr</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">local</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">nginx</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span><span class="sh_normal">conf</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span>nginx<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>conf</li><li style="list-style:none outside;"><span class="sh_normal">location </span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">/</span> <span class="sh_cbracket" style="color:rgb(255,0,128);font-weight:bold;">{</span></li><li style="list-style:none outside;"> root html<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span></li><li style="list-style:none outside;"> index index<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>php index<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>html index<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>htm<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span></li><li style="list-style:none outside;"> <span class="sh_cbracket" style="color:rgb(255,0,128);font-weight:bold;">}</span></li><li style="list-style:none outside;"> location <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">~</span> <span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">\.</span>php {
- root html;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME documentrootfastcgi_script_name;
- include fastcgi.conf;
- }
步骤二:启动服务
1)启动Nginx服务
这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。
2)启动MySQL服务
3)启动PHP-FPM服务
4)关闭SELinux、防火墙
步骤三:创建PHP页面,使用PHP语言测试memcached服务
创建PHP首页文档/usr/local/nginx/html/index.php,测试页面可以参考lnmp_soft/php_scripts/mem.php:
- [[email protected] ~]# vim /usr/local/nginx/html/test.php
-
<?php
-
memcache<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">=</span><span class="sh_keyword" style="color:rgb(187,121,119);font-weight:bold;">new</span> Memcache<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span> <span class="sh_comment" style="color:rgb(255,128,0);">//创建memcache对象</span></li><li style="list-style:none outside;">memcache->connect(‘localhost’,11211) or die (‘could not connect!!’);
-
memcache<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-></span><span class="sh_function" style="color:rgb(0,68,102);">set</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">(</span><span class="sh_string" style="color:rgb(166,133,0);">'key'</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">,</span><span class="sh_string" style="color:rgb(166,133,0);">'test'</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">);</span> <span class="sh_comment" style="color:rgb(255,128,0);">//定义变量</span></li><li style="list-style:none outside;">get_values=memcache<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">-></span><span class="sh_function" style="color:rgb(0,68,102);">get</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">(</span><span class="sh_string" style="color:rgb(166,133,0);">'key'</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">);</span> <span class="sh_comment" style="color:rgb(255,128,0);">//获取变量值</span></li><li style="list-style:none outside;">echoget_values;
- ?>
步骤四:客户端测试
客户端使用浏览器访问服务器PHP首页文档,检验对memcached的操作是否成功:
3 案例3:PHP的本地Session信息
3.1 问题
通过Nginx调度器负载后端两台Web服务器,实现以下目标:
- 部署Nginx为前台调度服务器
- 调度算法设置为轮询
- 后端为两台LNMP服务器
- 部署测试页面,查看PHP本地的Session信息
3.2 方案
使用4台RHEL7虚拟机,其中一台作为Nginx前端调度器服务器(eth0:192.168.4.5,eth1:172.16.2.5)、两台虚拟机部署为LNMP服务器,分别为Web1服务器(192.168.2.100)和Web2服务器(192.168.2.200),另外一台作为测试用的Linux客户机(172.16.4.100),拓扑如图-2所示。

图-2
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:部署后端LNMP服务器相关软件
注意:以下部署LNMP服务器的操作,需要在两台后端服务器做相同的操作,下面我们以一台Web1服务器(192.168.2.100)为例,对Web2服务器执行相同操作即可。
1)使用yum安装基础依赖包
2)源码安装Nginx
3)安装MariaDB数据库
4)安装PHP
5)修改Nginx配置文件(修改默认首页与动静分离)
-
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
-
location / {
- root html;
- index index.php index.html index.htm;
- }
- location ~ \.php<span class="sh_cbracket" style="color:rgb(255,0,128);font-weight:bold;">{</span></li><li style="list-style:none outside;"> root html<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span></li><li style="list-style:none outside;"> fastcgi_pass <span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">127.0.0.1</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">:</span><span class="sh_number" style="color:rgb(128,0,128);font-weight:bold;">9000</span><span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span></li><li style="list-style:none outside;"> fastcgi_index index<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">.</span>php<span class="sh_symbol" style="color:rgb(255,0,128);font-weight:bold;">;</span></li><li style="list-style:none outside;"> # fastcgi_param SCRIPT_FILENAMEdocument_root$fastcgi_script_name;
- include fastcgi.conf;
- }
步骤二:启动LNMP服务器相关的服务
1)启动Nginx服务
这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。
2)启动MySQL服务
3)启动PHP-FPM服务
4)关闭SELinux、防火墙
步骤三:部署前端Nginx调度服务器
1)使用源码安装nginx软件(如果Nginx软件包已存在可以忽略此步骤)
2)修改Nginx配置文件
Nginx配置文件中,通过upstream定义后端服务器地址池,默认调度策略为轮询,使用proxy_pass调用upstream定义的服务器地址池:
-
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
-
.. ..
- upstream webs {
- server 192.168.2.100:80;
- server 192.168.2.200:80;
- }
- server {
- listen 80;
- server_name localhost;
- location / {
- proxy_pass http://webs;
- root html;
- index index.html index.htm;
- }
- }
3)重新加载配置文件
4)关闭SELinux、防火墙
步骤四:测试环境是否配置成功
1)浏览器访问测试页面验证。
步骤五:部署测试页面
1)部署测试页面(Web1服务器)。
测试页面可以参考lnmp_soft/php_scripts/php-memcached-demo.tar.gz。
2)浏览器直接访问后端服务器的测试页面(Web1服务器)。
注意:可用修改index.html和home.html两个文件的内容,添加页面颜色属性,以区别后端两台不同的服务器:<body bgcolor=blue>。
3)部署测试页面(Web2服务器)。
测试页面可以参考lnmp_soft/php_scripts/php-memcached-demo.tar.gz。
4)浏览器直接访问后端服务器的测试页面(Web2服务器)。
注意:可用修改index.html和home.html两个文件的内容,添加页面颜色属性,以区别后端两台不同的服务器:<body bgcolor=green>。
5)浏览器访问前端调度器测试(不同后端服务器Session不一致)。
-
[[email protected] ~]# firefox http://192.168.4.5
- //填写注册信息后,刷新,还需要再次注册,说明两台计算机使用的是本地Session
- //第二台主机并不知道你再第一台主机已经登录,第一台主机的登录信息也没有传递给第二台主机
4 案例4:PHP实现session共享
4.1 问题
沿用练习三,通过修改PHP-FPM配置文件,实现session会话共享,本案例需要在练习三的基础上实现:
- 配置PHP使用memcached服务器共享Session信息
- 客户端访问两台不同的后端Web服务器时,Session 信息一致
4.2 方案
在练习三拓扑的基础上,Nginx服务器除了承担调度器外,还需要担任memcached数据库的角色,并在两台后端LNMP服务器上实现PHP的session会话共享。拓扑结构如图-4所示。

图-4
4.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:构建memcached服务
1)安装Memcached服务(如果192.168.4.5上已经有本软件包,此步骤可以忽略)
5)为PHP添加memcache扩展
2)启动服务并查看网络连接状态验证是否开启成功:
-
[[email protected] ~]# systemctl restart memcached
-
[[email protected] ~]# netstat -anptu | grep memcached
- tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 2839/memcached
- tcp 0 0 :::11211 :::* LISTEN 2839/memcached
- udp 0 0 0.0.0.0:11211 0.0.0.0:* 2839/memcached
- udp 0 0 :::11211 :::* 2839/memcached
3)关闭SELinux、防火墙
步骤二:在后端LNMP服务器上部署Session共享
注意:这些操作在两台后端Web服务器上均需要执行,以下操作以Web1(192.168.2.100)服务器为例。
1)修改PHP-FPM配置文件,并重启服务
-
[[email protected] ~]# vim /etc/php-fpm.d/www.conf //修改该配置文件的两个参数
- //文件的最后2行
- 修改前效果如下:
- php_value[session.save_handler] = files
- php_value[session.save_path] = /var/lib/php/session
- //原始文件,默认定义Sessoin会话信息本地计算机(默认在/var/lib/php/session)
- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- 修改后效果如下:
- php_value[session.save_handler] = memcache
- php_value[session.save_path] = “tcp://192.168.2.5:11211”
- //定义Session信息存储在公共的memcached服务器上,主机参数中为memcache(没有d)
- //通过path参数定义公共的memcached服务器在哪(服务器的IP和端口)
-
[[email protected] ~]# systemctl restart php-fpm
步骤三:客户端测试
客户端使用浏览器访问两台不同的Web服务器。
操作步骤与练习三一致,最终可以获得相关的Session ID信息。