《云计算》-Nginx高级-搭建memcached服务器-搭建LNMP+memcached环境

构建memcached服务
1.1 问题

本案例要求先快速搭建好一台memcached服务器,并对memcached进行简单的增、删、改、查操作:

安装memcached软件,并启动服务
使用telnet测试memcached服务
对memcached进行增、删、改、查等操作

    
    
  • 1
  • 2
  • 3

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:构建memcached服务

1)使用yum安装软件包memcached

[root@proxy ~]# yum -y  install   memcached
[root@proxy ~]# rpm -qa memcached
memcached-1.4.15-10.el7_3.1.x86_64

    
    
  • 1
  • 2
  • 3
  1. memcached配置文件

    [root@proxy ~]# vim /usr/lib/systemd/system/memcached.service
    ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS
    [root@proxy ~]# vim /etc/sysconfig/memcached
    PORT=“11211”
    USER=“memcached”
    MAXCONN=“1024”
    CACHESIZE=“64”
    OPTIONS=""

3)启动服务并查看网络连接状态验证是否开启成功:

[root@proxy ~]# systemctl  start  memcached
[root@proxy ~]# systemctl  status  memcached
[root@proxy ~]# 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
[root@proxy ~]# setenforce 0
[root@proxy ~]# firewall-cmd --set-default-zone=trusted

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

步骤二:使用telnet访问memcached服务器

1)使用yum安装telnet

[root@proxy ~]# yum -y install telnet

    
    
  • 1

2)使用telnet连接服务器测试memcached服务器功能,包括增、删、改、查等操作。

[root@proxy ~]# telnet  192.168.4.5  11211
Trying 192.168.4.5...
……
set name 0 180 3                //定义变量,变量名称为name
plj                            //输入变量的值,值为plj                
STORED
get name                        //获取变量的值
VALUE name 0 3                 //输出结果
plj
END
add myname 0 180 10            //新建,myname不存在则添加,存在则报错
set myname 0 180 10            //添加或替换变量
replace myname 0 180 10        //替换,如果myname不存在则报错
get myname                    //读取变量
append myname 0 180 10        //向变量中追加数据
delete myname                    //删除变量
stats                        //查看状态
flush_all                        //清空所有
quit                            //退出登录                                  

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

2 案例2:LNMP+memcached
2.1 问题

沿用练习一,部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作,实现以下目标:

部署LNMP实现PHP动态网站架构
为PHP安装memcache扩展
创建PHP页面,并编写PHP代码,实现对memcached的数据操作

    
    
  • 1
  • 2
  • 3

2.2 方案

使用2台RHEL7虚拟机,其中一台作为memcached及LNMP服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。

图-1

在RHEL7系统光盘中包含有我们需要的MariaDB、PHP,我们需要使用源码安装Nginx,使用RPM包安装FPM。另外如果希望使用PHP来操作memcached,注意必须要为PHP安装memcache扩展(php-pecl-memcache),否则PHP无法解析连接memcached的指令。客户端测试时需要提前安装telnet远程工具。
2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署LNMP环境(如果环境中已经存在LNMP环境本步骤可以忽略)

1)使用yum安装基础依赖包

[root@proxy ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel
.. ..

    
    
  • 1
  • 2

2)源码安装Nginx

[root@proxy ~]# tar -xf nginx-1.12.2.tar.gz
[root@proxy ~]# cd nginx-1.12.2
[root@proxy nginx-1.12.2]#  ./configure   \
> --with-http_ssl_module 
[root@proxy nginx-1.12.2]# make && make install

    
    
  • 1
  • 2
  • 3
  • 4
  • 5

3)安装MariaDB数据库

[root@proxy ~]# yum -y install  mariadb  mariadb-server  mariadb-devel

    
    
  • 1

4)安装PHP

[root@proxy ~]# yum -y install  php  php-mysql
[root@proxy ~]# yum -y install  php-fpm-5.4.16-42.el7.x86_64.rpm

    
    
  • 1
  • 2

5)为PHP添加memcache扩展

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

    
    
  • 1

6)修改Nginx配置文件

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            index  index.php  index.html   index.htm;
        }
 location  ~  \.php$  {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
           # fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi.conf;
        }

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

步骤二:启动服务

1)启动Nginx服务

这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。

[root@proxy ~]# systemctl stop  httpd                //如果该服务存在,则关闭该服务
[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# netstat -utnlp | grep :80
tcp    0    0 0.0.0.0:80        0.0.0.0:*        LISTEN        32428/nginx         

    
    
  • 1
  • 2
  • 3
  • 4

2)启动MySQL服务

[root@proxy ~]# systemctl start mariadb
[root@proxy ~]# systemctl status mariadb

    
    
  • 1
  • 2

3)启动PHP-FPM服务

[root@proxy ~]# systemctl start php-fpm
[root@proxy ~]# systemctl status php-fpm

    
    
  • 1
  • 2

4)关闭SELinux、防火墙

[root@proxy ~]# setenforce 0
[root@proxy ~]# firewall-cmd --set-default-zone=trusted

    
    
  • 1
  • 2

步骤三:创建PHP页面,使用PHP语言测试memcached服务

创建PHP首页文档/usr/local/nginx/html/index.php,测试页面可以参考lnmp_soft/php_scripts/mem.php:

 [root@proxy ~]# vim /usr/local/nginx/html/test.php
<?php
$memcache=new Memcache;                //创建memcache对象
$memcache->connect('localhost',11211) or die ('could not connect!!');
$memcache->set('key','test');             //定义变量
$get_values=$memcache->get('key');        //获取变量值
echo $get_values;
?>

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

步骤四:客户端测试

客户端使用浏览器访问服务器PHP首页文档,检验对memcached的操作是否成功:

[root@client ~]# firefox http://192.168.4.5/test.php

    
    
  • 1
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了317 篇原创文章</span> · <span>获赞 48</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值