“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