第十八周

“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命令(獲?。┛傉埱髷?/span>
STAT cmd_set 72        set命令(獲?。┛傉埱髷?/span>
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的內存大?。ㄗ止潱?/span>
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.www58058.com/85199

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

相關推薦

  • N26-第四周-孫逸

    1、  復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 cp –r /etc/skel /home/tuser1 chmod –R 700 /home/tuser1 2、  編輯/etc/group文件,添加組hadoop。 group文件的內容格式: &…

    2017-03-10
  • 查找find及壓縮與解壓

    find  根據時間戳:以“天”為單位;              -atime [+|-]#,                #: [#,#+1)   2 那就是大于等于二天小于…

    2017-08-12
  • Linux運維學習歷程-第六天-Linux重定向和管道

    Linux運維學習歷程-第六天-Linux重定向和管道 2 本章內容我們將學習linux中的重定向和管道兩大用法   I/O輸入與輸出設備   重定向   管道   tee命令與tr命令 一、I/O設備   1、什么是I/O設備   管理和控制計算機的所有輸入/輸出(I/O)設備是操作系統…

    Linux干貨 2016-08-03
  • 馬哥教育網絡班20期+第3周課程練習

     1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 法一: who | cut -d ‘ ‘ -f 1 | tail -u 法二: who | cut -d ’’-f&…

    Linux干貨 2016-06-26
  • kickstart文件及引導鏡像的制作流程

    kickstart文件及引導鏡像的制作流程 kickstart文件制作流程 安裝制作kickstart文件的圖形界面工具 [root@slave1 ~]# yum install system-config-kickstart 開啟工具 [root@slave1 ~]# system-config-kickstart 在打開的圖形界面中通過file菜單選項選…

    Linux干貨 2017-09-04
  • 01Linux的發展歷史

    1、1965年時,貝爾實驗室(Bell Labs)加入一項由通用電氣(General Electric)和麻省理工學院(MIT)合作的項目;該項目要建立一套多使用者、多任務、多層次(multi-user、multi-task、multi-level)的MULTICS操作系統。但是由于整個目標過于龐大,糅合了太多的特性,Multics雖然發布了一些產品,但是性…

    Linux干貨 2016-10-14
欧美性久久久久