第十八周

“1、为LNMP架构添加memcached支持,并完成对缓存效果的测试报告;
架构(3台centos7)
nginx与php 192.168.1.108    nginx,php-fpm,php-mysql php-pecl-memcache
mysql         192.168.1.110    mariadb
memcached 192.168.1.118   memcached

nginx的yum仓库
[name]
name=myrepo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
enabled=1
gpgcheck=0
搭建LNMP(关闭防火墙和selinux)
/]# iptables -F
]# setenforce 0
   nginx与php主机配置
/]# yum install nginx php-fpm php-mysql php-pecl-memcache
/]# vim /etc/nginx/conf.d/default.conf
location / {
        root   /www/html;
        index  index.php index.html index.htm;
    }
location ~ .php$ {
        root           /www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
/]# mkdir -p /www/html
/]# systemctl start nginx.service
/]# systemctl start php-fpm.service
/]# php -m | grep memcache   #查看加载memcache模块
memcache
 
在mysql主机
/]# yum install mariadb-server –y
~]# vim /etc/my.cnf
[mysqld]
datadir=/mydata/data
innodb_file_per_table=ON
skip_name_resolve=ON
/]# mkdir –p /mydata/data
/]#chown –R mysql.mysql /mydata/data
/]# myslq _install_db –user=mysl –datadir=/mydata/data
/]# systemctl start mariadb.service
/]# mysql
MariaDB [(none)]> grant all on *.* to root@”192.168.1.%” identified by “mageedu”;
MariaDB [(none)]> flush privileges;

在niginx的主机的/www/html目录下创建php与mysql的联动文件,以及memcached文件
/]# vim index.php
<?php
phpinfo();
?>
/]# vim mysql.php
<?php
   $conn=mysql_connect (“192.168.1.110”,’root’,’mageedu’);
    if ($conn)
      echo “OK !!!”;
    else
       echo “FAILURE”;
?>
/]# vim mem.php
<?php
$memcache = new Memcache;
$memcache->connect(‘192.168.1.118’,11211) or die (“Could not connect”);
$memcache->set(‘key’,’test’);
$get_value = $memcache->get(‘key’);
echo $get_value;
?>

  配置memcached服务
~]# yum install memcached -y
~]# systemctl start memcached
[root@localhost ~]# telnet 192.168.1.118 11211
Trying 192.168.1.118…
Connected to 192.168.1.118.
Escape character is ‘^]’.
stats              查看memcached状态命令
STAT pid 2857       进程ID
STAT uptime 44564        运行时间
STAT time 1503095847     当前unix时间戳
STAT version 1.4.15       版本号
STAT libevent 2.0.21-stable  libvent版本
STAT pointer_size 64      当前操作系统的指针大小
STAT rusage_user 2.165423   进程的累积用户时间
STAT rusage_system 2.919051  累积系统时间
STAT curr_connections 10     当前存储的items数量
STAT total_connections 67    启动以后存储的items的总数量
STAT connection_structures 36  服务器分配的连接构造数
STAT reserved_fds 20          
STAT cmd_get 387     get命令(获取)总请求数
STAT cmd_set 72        set命令(获取)总请求数
STAT cmd_flush 0    flush命令请求数
STAT cmd_touch 0    touch命令请求次数
STAT get_hits 318    总命中次数
STAT get_misses 69   未命中次数
STAT delete_misses 0   delete命令未命中次数
STAT delete_hits 1     delete命中次数
STAT incr_misses 0
STAT incr_hits 2
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0    使用擦拭次数
STAT touch_hits 0  
STAT touch_misses 0
STAT auth_cmds 0   认证命令处理次数
STAT auth_errors 0  认证失败数
STAT bytes_read 49415   总读取字节数(请求字节数)
STAT bytes_written 290744  总发送字节数(结果字节数)
STAT limit_maxbytes 67108864 分配给memcached的内存大小(字节)
STAT accepting_conns 1    服务器是否达到最大连接(0/1)
STAT listen_disabled_num 0  失效的监听数
STAT threads 4           当前线程数
STAT conn_yields 0       连接操作主动放弃数
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT bytes 29484         当前存储占用的字节数
STAT curr_items 41       当前存储的数据总数
STAT total_items 74         启动以来存储的数据总数
STAT expired_unfetched  
STAT evicted_unfetched 0
STAT evictions 0 为获取空闲内存而删除的items数(分配给memcached的空间用满后需要删除旧的items来得到空间分配给新的ite
STAT reclaimed 0 已过期的数据条目来存储新数据的数目
END

 在nginx主机安装wordpress
前提在mysql主机授权
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on workpress.* to wpuser@”192.168.1.%” identified by “mageedu”;

]# unzip wordpress-4.7.4-zh_CN.zip –C /www/html
]# cd /www/html/wordpress/
]# cp wp-config-sample.php wp-config.php
]# vim  wp-config.php

/** WordPress数据库的名称 */
define(‘DB_NAME’, ‘wordpress’);
/** MySQL数据库用户名 */
define(‘DB_USER’, ‘wpuser’);
/** MySQL数据库密码 */
define(‘DB_PASSWORD’, ‘mageedu’);
/** MySQL主机 */
define(‘DB_HOST’, ‘192.168.1.110’);

而后通过网络安装192.168.1.108/wordpress//wpd-admin/install.php

为wordpress配置memcached缓存,首先下载wordpress插件,下载 WordPress Memcached插件(http://wordpress.org/plugins/memcached/),解压后,将 object-cache.php 上传到 wp-content 目录。
[root@localhost ~]# cp object-cache.php /www/html/wordpress/wp-content/
[root@localhost ~]# vim object-cache.php  (在418行)
修改地址:
$buckets = array(‘192.168.1.118:11211’);  memcached服务器地址

测压工具为siege.
/]#wget http://soft.vpser.net/test/siege/siege-2.67.tar.gz
]#tar xf  siege-2.67.tar.gz
]#cd  siege-2.67
]#/configure 
]# make -j 4 && make install
在未开启memcached服务
]# /usr/local/bin/siege -r 5 -c 300 192.168.1.108:/index.php
ransactions:        1500 hits  次数
Availability:      100.00 %   命中率
Elapsed time:       17.98 secs  总时长
Data transferred:       68.27 MB  总共传输数据大小
Response time:        2.20 secs  相对用时
Transaction rate:       83.43 trans/sec  服务器处理速度
Throughput:        3.80 MB/sec   每秒传输数据大小
Concurrency:      183.91    最高并发数
Successful transactions:        1500  成功次数
Failed transactions:           0   失败次数
Longest transaction:       13.73
Shortest transaction:        0.00

开启
ransactions:        1500 hits
Availability:      100.00 %
Elapsed time:        7.77 secs
Data transferred:       68.27 MB
Response time:        0.59 secs
Transaction rate:      193.05 trans/sec
Throughput:        8.79 MB/sec
Concurrency:      113.39
Successful transactions:        1500
Failed transactions:           0
Longest transaction:        3.14
Shortest transaction:        0.01

2、部署配置haproxy,能够实现将来自用户的80端口的http请求转发至后端8000上的server服务,写出其配置过程。
haproxy主机:192.168.1.118
web1主机:192.168.1.108  nginx端口808
web2主机:192.168.1.110   nginx端口8080

在2台web主机下载nginx,修改配置文件,创建网页文件
]#.f yum insatll nginx –y
]# vim /etc/nginx/conf.d/default.conf
server {
    listen       808;
server_name  localhost;
location / {
        root   /www/html;

]vim /www/html/index.html
server 192.168.1.108 !

]# systemctl start nginx

进行测试,确保正常访问
 
第十八周
 

第十八周

haproxy主机下载haproxy,修改配置

/]# yum install haproxy

/]# vim /etc/haproxy/haproxy.cfg

listen my_haproxy 192.168.1.118:80
cookie SERVERID rewrite
balance roundrobin
server web1 192.168.1.108:808 cookie app1inst1 check inter 2000 rise 2 fall 5
server web2 192.168.1.110:8080 cookie app1inst2 check inter 2000 rise 2 fall 5

/]# systemctl start haproy.service
测试

 第十八周

 

第十八周

3、阐述varnish的功能及其应用场景,并通过实际的应用案例来描述配置、测试、调试过程。”

Web缓存是指一个Web资源(html,js,css,images…)存在于Web服务器和客户端(浏览器),缓存会根据进来的请求报文做出响应,后缓存一份到本地的缓存中;当下一个请求到来的时候,如果是相同的URL,缓存会根据缓存机制决定是直接使用从缓存中响应访问请求还是向后端服务器再次发送请求,取决于缓存是否过期及其请求的内容是否发生改变。有效的缓存能减少后端主机的压力,实现快速响应用户的请求,提高用户体验。

varnish就是一种实现上述web缓存功能(通常针对于静态资源提供页面缓存)的一款开源工具,通常它也被称为http或web加速器,同时它也可以做为http反向代理工具,实现负载均衡和动静分离的功能。此外,据官网介绍,Varnish的设计不仅仅是定位于反向代理服务器,根据使用方式的不同,Varnish可扮演的角色也丰富多样,其它可实现的功能如下:

1)WEB应用防火墙;
2)DDoS攻击防护;
3)网站防盗链;
4)负载均衡;
5)integration point;
6)单点登录网关;
7)认证和认证授权;
8)后端主机快速修复;
9)HTTP路由

但在实际生产环境中varnish更多是在HAProxy、Nginx等七层负载均衡器后充当静态资源缓存的反向代理,下图是一个简化的varnish应用场景,主要用来实现静态资源缓存和动静分离
nginx+php-fpm     192.168.0.108
mariadb           192.168.0.110
nginx             192.168.1.118
varnish           192.168.1.119

[root@localhost varnish]# vim varnish.params   端口
VARNISH_LISTEN_PORT=80

[root@localhost varnish]# vim default.vcl
backend default {        #默认后台主机
    .host = “192.168.1.118”;   #地址
    .port = “80”;              #端口
    .probe = {                 #健康检查
        .url = “/”;            
        .interval = 2s;
        .window = 5;
        .threshold = 4;
     }
}
backend web1 {                #默认后台主机      
    .host = “192.168.1.108”;
    .port = “80”;
    .probe = {
         .url = “/”;
         .interval = 2s;
         .window = 5;
         .threshold = 4;
     }
}
acl purgers {                  #Purge—ACL控制
     “127.0.0.1”;
     “192.168.1.0”/24;
}
sub vcl_purge {                 #定义purge操作
  return(synth(200,”Purged”));
}

sub vcl_recv {                     #动静分离设置
    if (req.url ~ “(?i).php$”) {
       set req.backend_hint = web1; #动态资源后台主机
    } else {
       set req.backend_hint = default;    #静态资源后台主机
    }
    if (req.method == “PURGE”) {
       if (!client.ip ~ purgers) {
           return(synth(405,”Purging not allowed for” + client.ip));
           return(purge);
        }
     }
}
sub vcl_deliver {          记录缓存命中状态
    if (obj.hits>0) {
        set resp.http.X-Cache=”HIT”;
      } else {
        set resp.http.X-Cache=”MISS”;
     }
}

/]# systemctl start varnish.service

在varnish CLI命令接口下创建并启用vcl
[root@localhost varnish]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
200        
—————————–
Varnish Cache CLI 1.0
—————————–
Linux,3.10.0-514.26.2.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-4.0.5 revision 07eff4c29
加载默认vcl配置文件,并命名为test1.
varnish> vcl.load test1 default.vcl
200        
VCL compiled.
激活test1
varnish> vcl.use test1
200        
VCL ‘test1’ now active

varnish> backend.list
200        
Backend name                   Refs   Admin      Probe
default(192.168.1.118,,80)     2      probe      Healthy 5/5
web1(192.168.1.108,,80)        2      probe      Healthy 5/5

静态资源测试
[root@localhost varnish]# curl -I 192.168.1.119/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Sun, 20 Aug 2017 06:19:16 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sun, 20 Aug 2017 05:15:44 GMT
ETag: “59991b00-e”
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Connection: keep-alive

[root@localhost varnish]# curl -I 192.168.1.119/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Sun, 20 Aug 2017 06:19:16 GMT
Content-Type: text/html
Content-Length: 14
Last-Modified: Sun, 20 Aug 2017 05:15:44 GMT
ETag: “59991b00-e”
X-Varnish: 5 3
Age: 89
Via: 1.1 varnish-v4
X-Cache: HIT
Connection: keep-alive

动态测试
[root@localhost varnish]# curl -I 192.168.1.119/index.php
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 20 Aug 2017 06:21:23 GMT
Content-Type: text/html
X-Powered-By: PHP/5.4.16
X-Varnish: 7
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Content-Length: 6
Connection: keep-alive

[root@localhost varnish]# curl -I 192.168.1.119/index.php
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Sun, 20 Aug 2017 06:21:23 GMT
Content-Type: text/html
X-Powered-By: PHP/5.4.16
X-Varnish: 32770 8
Age: 18
Via: 1.1 varnish-v4
X-Cache: HIT
Content-Length: 6
Connection: keep-alive

动静分离,后端主机测试
[root@localhost varnish]# curl  192.168.1.119/index.php
OK !!!
[root@localhost varnish]# curl  192.168.1.119/index.html
Server 110 !!

原创文章,作者:ning407631632,如若转载,请注明出处:http://www.178linux.com/85199

(0)
上一篇 2017-08-21 09:14
下一篇 2017-08-21 10:03

相关推荐

  • Linux文件管理命令详解–cp,mv,rm

    在Linux系统里平时需要对目录文件做一些管理操作,其中最基本的有cp,mv,rm等命令: cp命令:copy 复制   其中包含源文件,目标文件: copy分为单源复制和多源复制,详解如下:               单源复制:cp [option]…[-T] S…

    Linux干货 2016-11-06
  • Python 课堂笔记

    第二天

    Linux干货 2018-03-21
  • vim编辑器的使用

    vim          vim是一个类似于vi的文本编辑器,不过在vi的基础上增加了很多功能,在早起vim和Emacs编辑器打的很火热,在1999年被linuxwork文本编辑分类的优胜者,而vim屈居第二,但2000年vim赢得了salashdot beanie的最佳开放源代…

    系统运维 2016-08-09
  • 磁盘管理2——文件系统挂载和swap文件系统以及磁盘管理工具

    文件系统的使用: 首先要“挂载”:mount命令和umount命令 根文件系统之外的其他文件系统要想能够被访问,都必须通过“关联”至根文件系统上的某个目录来实现,此管理操作即为“挂载”,此目录为“挂载点” 挂载点:MOUNT_POINT,用于作为另一个文件系统的访问入口     (1)事先存在   …

    Linux干货 2016-08-29
  • bash脚本编程基础知识

    shell脚本语言编程之bash shell简介 什么是shell:     shell是Linux的用户界面,提供了用户与内核进行交互的接口,他接收了用户的指令,并将指令送入内核去执行     shell即是一种高级程序语言,也是一种命令解析语言   &nb…

    Linux干货 2016-08-15
  • 文件相关命令

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp:copy 源文件:目标文件 单源复制:cp [OPTION]… [-T] SOURCE DEST 如果DEST不存在,则事先创建此文件,并复制源文件的数据流至DEST中 如果DEST存在: 如果DEST是非目录文件:则覆盖目标文件 如果DEST是目录文件:则先在D…

    Linux干货 2016-09-23