第十八周博客作業

1、為LNMP架構添加memcached支持,并完成對緩存效果的測試報告;

操作系統: CentOS 7.2
10.0.0.51 nginx+php+mysql
10.0.0.52 memcached

一. 環境準備:

搭建LNMP編譯安裝環境

1. 配置163的yum源和阿里云的epel源

[root@localhost ~]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@localhost ~]# wget -O /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2. 安裝Nginx

#yum install nginx           #配置文件處于/etc/nginx
#systemctl start nginx     #啟動nginx
#systemctl enable nginx.service   # 設置為開機啟動
# rpm -q nginx
nginx-1.10.2-1.el7.x86_64

測試: http://10.0.0.51是否可以打開

3. 安裝Mysql

#rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
#yum repolist enabled | grep “mysql.*-community.*”
#yum -y install mysql-community-server
# systemctl start mysqld
# systemctl enable mysqld
# mysql_secure_installation
查看版本: 
# rpm -q mysql-community-server
mysql-community-server-5.6.36-2.el7.x86_64

4. 安裝php

#yum install php-fpm php-mysql
#systemctl start php-fpm               # 啟動php-fpm
#systemctl enable php-fpm           # 設置開機啟動

修改Nginx的配置文件:
]# vim /etc/nginx/conf.d/wordpress.conf
server {
        listen 8000;
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        location ~ \.php$ {
        root /usr/www;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        }
}
#systemctl reload nginx

在/usr/www 目錄中創建 phpinfo.php
# mkdir /usr/www
# vim /usr/www/phpinfo.php
<?php
        phpinfo();
?>

測試訪問:
mark

5. 安裝wordpress

# wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
tar xf wordpress-4.7.4-zh_CN.tar.gz  -C /usr/www

# mysql -uroot -p
mysql> create database wordpress;
mysql> GRANT ALL ON wordpress.* TO wpuser@'10.%.%.%' IDENTIFIED BY 'wppass';
mysql> flush privileges;

# cp wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wordpress');

/** MySQL數據庫用戶名 */
define('DB_USER', 'wpuser');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', 'wppass');

/** MySQL主機 */
define('DB_HOST', '10.0.0.51');

通過頁面http://10.0.0.51:8000/wordpress/wp-admin/install.php安裝wordpress:
mark
mark
mark
問題: 登錄后,提示
mark
需手動加上index.php才能打開, 這個問題有時間再研究。

二. 安裝memcached

[root@localhost ~]# yum -y install memcached
[root@localhost ~]# systemctl start memcached.service

三. 查看memcached狀態

[root@localhost ~]# telnet 10.0.0.52 11211
stats             #stats命令用來查看memcahced狀態
STAT pid 15706    #memcache服務器的進程ID  
STAT uptime 130   #服務器已經運行的秒數
STAT time 1499479581           #服務器當前的unix時間戳
STAT version 1.4.15            #memcache版本
STAT libevent 2.0.21-stable    #libevent版本
STAT pointer_size 64           #當前操作系統的指針大小(32位系統一般是32bit,64就是64位操作系統)
STAT rusage_user 0.002243      #進程的累計用戶時間
STAT rusage_system 0.013459    #進程的累計系統時間
STAT curr_connections 10       #服務器當前存儲的items數量
STAT total_connections 11      #從服務器啟動以后存儲的items總數量
STAT connection_structures 11  #服務器分配的連接構造數
STAT reserved_fds 20
STAT cmd_get 0       #get命令(獲?。┛傉埱蟠螖?等于 get_hits + get_misses 
STAT cmd_set 0       #set命令(保存)總請求次數 
STAT cmd_flush 0     #flush命令請求次數
STAT cmd_touch 0     #touch命令請求次數
STAT get_hits 0      #總命中次數
STAT get_misses 0    #總未命中次數
STAT delete_misses 0    #delete命令未命中次數
STAT delete_hits 0      #delete命令命中次數
STAT incr_misses 0      #incr命令未命中次數
STAT incr_hits 0        #incr命令命中次數
STAT decr_misses 0      #decr命令未命中次數
STAT decr_hits 0        #decr命令命中次數
STAT cas_misses 0       #cas命令未命中次數
STAT cas_hits 0         #cas命令命中次數
STAT cas_badval 0       #使用擦拭次數
STAT touch_hits 0       #touch命令未命中次數
STAT touch_misses 0      #touch命令命中次數
STAT auth_cmds 0        #認證命令處理的次數
STAT auth_errors 0      #認證失敗數目
STAT bytes_read 7       #總讀取字節數(請求字節數)
STAT bytes_written 0    #總發送字節數(結果字節數)
STAT limit_maxbytes 67108864    #分配給memcache的內存大?。ㄗ止潱?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 0                   #當前存儲占用的字節數
STAT curr_items 0              #當前存儲的數據總數
STAT total_items 0             #啟動以來存儲的數據總數
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0   #為獲取空閑內存而刪除的items數(分配給memcache的空間用滿后需要刪除舊的items來得到空間分配給新的items)
STAT reclaimed 0   #已過期的數據條目來存儲新數據的數目
END

三.安裝PHP的Memcached的擴展

php連接memcached服務的模塊有兩個,php-pecl-memcache和php-pecl-memc>ached.若要安裝php-pecl-memcached需要依賴libmemcached程序包,可以提>供相應操作查看memcached的工具。在這里為方便演示就直接使用php-pecl->memcache擴展模塊。

[root@localhost ~]# yum -y install php-pecl-memcache
[root@localhost ~]# php -m|grep memcache
memcache

打開測試頁面查看是否已支持memcache
mark

四. 測試memcached緩存

[root@localhost ~]# cd /usr/www
[root@localhost www]# vim index.php
<?php
$memcache = new Memcache;             #創建一個memcache對象
$memcache->connect('10.0.0.52',11211) or die ("Could not connect"); #連接Memcached服務器
$memcache->set('key','test');        #設置一個變量到內存中,名稱是key 值是test
$get_value = $memcache->get('key');   #從內存中取出key的值
echo $get_value;
?>

mark

問題: 出現無法緩存的問題, 重啟php所在服務器后解決

五. 為wordpress配置memcache

下載 WordPress Memcached插件(http://wordpress.org/plugins/memcached/),解壓后,將 object-cache.php 上傳到 wp-content 目錄(不是 wp-content/plugins/),這樣 WordPress 會自動檢查在 wp-content 目錄下是否有 object-cache.php 文件,如果有,直接調用它作為 WordPress 對象緩存機制。

[root@localhost ~]# cp object-cache.php /usr/www/wordpress/wp-content/
[root@localhost ~]# vim object-cache.php +418
修改地址:
$buckets = array('10.0.0.52:11211');

登錄wordpress進行一些操作看, 查看緩存情況

[root@localhost wp-content]# telnet 10.0.0.52 11211
Trying 10.0.0.52...
Connected to 10.0.0.52.
Escape character is '^]'.
stats
STAT pid 11262
STAT uptime 99763
STAT time 1499708968
STAT version 1.4.15
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 2.200638
STAT rusage_system 1.379317
STAT curr_connections 22
STAT total_connections 58
STAT connection_structures 23
STAT reserved_fds 20
STAT cmd_get 712       #總共獲取數據的次數(等于 get_hits + get_misses )
STAT cmd_set 117       #總共設置數據的次數
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 614  #命中了多少次數據,也就是從 Memcached 緩存中成功獲取數據的次數
STAT get_misses 98 #沒有命中的次數
STAT delete_misses 0
STAT delete_hits 20
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 159540
STAT bytes_written 483317
STAT limit_maxbytes 67108864  #總的存儲大小,默認為 64M
STAT accepting_conns 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 23219       #當前所用存儲大小
STAT curr_items 43
STAT total_items 116
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 1
END

數據命中率: 614/712=86.2%

2、部署配置haproxy,能夠實現將來自用戶的80端口的http請求轉發至后端8000上的server服務,寫出其配置過程。

環境準備:
增加一臺haproxy服務,CentOS 7.2系統
10.0.0.50 haproxy
10.0.0.51 nginx+php+mysql (借用第一題的環境)

一. 安裝haproxy

]# yum -y install haproxy

二. 配置haporxy

]# vim /etc/haproxy/haproxy.cfg
...
listen websrvs        #定義一個代理服務器websrvs
    bind        *:80  #指定代理監聽端口為80
    server      websrv 10.0.0.51:8000 check    #定義一個后端websrv,注意:后端服務端口如果與監聽端口不一致,需要在地址后指明端口號
...
檢測配置文件
]# haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid

三. 啟動服務

]# systemctl start haproxy
]# systemctl enable haproxy

四. 訪問測試

直接訪問后端服務器
mark
通過haproxy訪問
mark

3、闡述varnish的功能及其應用場景,并通過實際的應用案例來描述配置、測試、調試過程。

系統: CentOS 7.2
varnish: 10.0.0.50
nginx+php-fpm+mariadb : 10.0.0.51
nginx : 10.0.0.52

一. 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應用場景,主要用來實現靜態資源緩存和動靜分離。

二. 部署web服務

####在10.0.0.51部署動態web服務  
]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
~]# yum install nginx php-fpm php-mysql php-mbstring php-gd php-xml -y
~]# mkdir -p /data/www
~]# vim /etc/nginx/nginx.conf
location / {
                root /data/www;
                index   index.php index.html index.htm;
        }

        location ~ \.php$ {
            root           /data/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# systemctl enable  nginx.service
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl enable php-fpm  

部署mariadb
~]# yum install mariadb-server -y
~]# vim /etc/my.cnf
[mysqld]
...
innodb_file_per_table = ON
skip_name_resolve = ON

~]# systemctl start mariadb.service
~]# mysql
> grant all on *.* to root@'10.%.%.%' identified by 'magedu';
> flush privileges;

####在10.0.0.52上配置靜態服務
]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
~]# yum install nginx -y
~]# mkdir -p /data/www
~]# vim /etc/nginx/nginx.conf
location / {
                root    /data/www;
                index   index.html index.htm;
        }
~]# systemctl start nginx.service
~]# systemctl enable nginx

三. 部署varnish

~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
~]# yum install varnish -y
######將varnish的監聽端口設置為80######
~]# vim /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80            
######配置varnish參數文件######
~]# vim /etc/varnish/default.vcl
######設置默認的后端靜態服務器ip和端口######
backend default {
    .host = "10.0.0.51";
    .port = "80";
######配置健康狀態監測######
    .probe = {
        .url = "/";
        .interval = 2s;
        .window = 5;
        .threshold = 4;
    }

}
######配置后端動態web服務器######
backend appsrv {
    .host = "10.0.0.52";
    .port = "80";
######配置健康狀態監測######
    .probe = {
        .url = "/";
        .interval = 2s;
        .window = 5;
        .threshold = 4;
    }

}
######定義Purge-ACL控制######
acl purgers {
    "127.0.0.1";
    "10.0.0.0"/24;
}
######定義purge操作######
sub vcl_purge {
    return(synth(200,"Purged"));
}

sub vcl_recv {
######動靜分離######
        if (req.url ~ "(?i)\.php$") {
             set req.backend_hint = appsrv;
         } else {
             set req.backend_hint = default;
         }
######如果請求方法為PURGE,且客戶端IP滿足acl,則執行purge操作,否則返回405頁面并提示######
        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
~]# systemctl enable varnish.service

四.創建檢測頁

######在10.0.0.51上創建動態探測頁######
~]# vim /data/www/index.php <?php
        $conn = mysql_connect('10.0.0.51','root','magedu');
        if ($conn)
                echo "Dynamic webserver to Mariadb is OK!";
        else
                echo "Failure";
?>
######在10.0.0.52上創建靜態探測頁######
~]# vim /data/www/index.html
<h1>I'm Static Server!</h1>

五.緩存效果測試

######在varnish CLI命令接口下創建并啟用vcl######
~]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
######加載默認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(10.0.0.52,,80)         2      probe      Sick 0/5
appsrv(10.0.0.51,,80)          2      probe      Sick 0/5

######第一次訪問靜態頁面,MISS######
~]# curl -I 10.0.0.50/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 05 Jul 2017 14:38:28 GMT
Content-Type: text/html
Content-Length: 28
Last-Modified: Wed, 05 Jul 2017 14:21:54 GMT
ETag: "595cf602-1c"
X-Varnish: 8
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Connection: keep-alive

######第二次訪問靜態頁面,HIT!######
~]# curl -I 10.0.0.50/index.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 05 Jul 2017 14:38:28 GMT
Content-Type: text/html
Content-Length: 28
Last-Modified: Wed, 05 Jul 2017 14:21:54 GMT
ETag: "595cf602-1c"
X-Varnish: 32770 9
Age: 2
Via: 1.1 varnish-v4
X-Cache: HIT
Connection: keep-alive

######第一次訪問動態頁面,MISS######
~]# curl -I 10.0.0.50/index.php
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 05 Jul 2017 14:53:11 GMT
Content-Type: text/html
X-Powered-By: PHP/5.4.16
X-Varnish: 11
Age: 0
Via: 1.1 varnish-v4
X-Cache: MISS
Content-Length: 35
Connection: keep-alive

######第二次訪問動態頁面,HIT!######
~]# curl -I 10.0.0.50/index.php
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Wed, 05 Jul 2017 14:53:11 GMT
Content-Type: text/html
X-Powered-By: PHP/5.4.16
X-Varnish: 32772 12
Age: 23
Via: 1.1 varnish-v4
X-Cache: HIT
Content-Length: 35
Connection: keep-alive

######分別訪問動靜資源均正常,說明動靜分離實現成功######
[root@localhost ~]# curl  10.0.0.50/index.html
<h1>I'm Static Server!</h1>
[root@localhost ~]# curl  10.0.0.50/index.php
Dynamic webserver to Mariadb is OK!

原創文章,作者:hansj,如若轉載,請注明出處:http://www.www58058.com/80106

(0)
hansjhansj
上一篇 2017-07-11
下一篇 2017-07-12

相關推薦

  • Tomcat基礎及常見使用入門

    Tomcat的簡單介紹,安裝,以及簡單的配置運用

    2017-09-11
  • Nginx之ngx_http_fastcgi_module模塊詳解

    一、ngx_http_fastcgi_module模塊:       nginx支持FastCGI模式       CGI:Common GateWay Interface 公共網管接口,可以理解其為HTTP服務器與其他主機上運行的程序進行通信的接口。       CG…

    2017-06-25
  • 用NFS搭建wordpress讓兩臺httpd服務同時服務

    一、博客實踐作業: (1) nfs server導出/data/application/web,在目錄中提供wordpress;  (2) nfs client掛載nfs server導出的文件系統至/var/www/html; (3) 客戶端(lamp)部署wordpress,并讓其正常訪問;要確保能正常發文章,上傳圖片; (4) 客戶端2(la…

    2017-06-07
  • Linux簡單的磁盤分區、格式化和掛載

     一.分區: 1.查看分區fdisk -l 通過fdisk -l 查看機器所掛硬盤個數及分區情況   2.通過上面的信息,我們知道此機器中掛載兩個硬盤分別是:/dev/sda和/dev/sdb, 其中sda有兩個主分區/dev/sda1和sda2; 其中sdb有1個擴展類型/dev/sdb3和3個邏輯分區sd…

    Linux干貨 2016-02-28
  • Centos6搭建vsftpd手冊

    1、開啟防火墻ftp端口vi /etc/sysconfig/iptables     #編輯防火墻配置文件 1 -A INPUT -m state –state NEW -m tcp -p tcp –dport 21 -j ACCEPT(允許21端口通過防火墻) 2 -A INPUT -m st…

    Linux干貨 2017-04-26
  • linux基礎之bash特性知識點

    1、 Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。     Linux 一切皆文件。個人理解 在linux下的命令操作都算是對文件操作     那么文件管理命令類命令可以分為下面幾類     目錄操作: &nbs…

    Linux干貨 2016-09-24
欧美性久久久久