用Varnish和Memcached缓存给WordPress网站提速
Varnish是一款高性能的缓存加速器,Varnish把数据存放在服务器的内存中,利用内存可以极大的提高PHP页面执行速度,可以设置0~60秒的***缓存时间,32位的机器支持的缓存文件***为2 GB。
Varnish采用VCL的配置,而且具有强大的管理功能,如top、stat、admin、lis,管理方式比较灵活。Varnish的状态机设计不仅巧妙,结构也很清晰,利用二叉堆管理缓存文件,即可达到随时删除的目的。
Memcached是一个高性能的分布式内存对象缓存系统,通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached对于减少MysqL数据查询压力非常有帮助。
由于Varnish采用了Visual Page Cache技术,所有缓存的数据都直接从内存读取,而Squid从硬盘读取缓存的数据,所以Varnish在访问速度方面会更快一些。但是Varnish在高并发状态下,CPU、I/O和内存等资源的开销高于Squid。
目前Varnish3.0版本解决了服务器重启后Varnish缓存消失的问题,性能优化上有了更大的提升。本篇文章就来分享一下利用Varnish和Memcached缓存来给Wordpress加速,因为要用到内存,所以比较适合那些大内存的服务器。
如果你正遭遇着大流量给Wordpress服务器带来的压力的苦恼,可以来尝试这些Wordpress提速方法:
1、Redis加速:用Redis缓存来给WordPress站点加速-适用于Apache和Nginx
2、负载均衡:免费空间+便宜VPS和OpenResty,Ngx_lua,Redis搭建系统负载均衡环境
3、CDN加速:DNSPOD分布式解析+安全宝和Incapsula对搜索引擎分别CDN加速
用Varnish和Memcached缓存给Wordpress网站提速-内存级加速
1、Varnish官网:
1、官方网站:https://www.varnish-cache.org/
2、对于Centos 5的,可以执行以下命令来安装:
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release/varnish-release-3.0-1.el5.centos.noarch.rpmyum install varnish
3、对于是Centos 6的,可以执行以下命令来安装:
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpmyum install varnish
4、如果版本搞错,就会出现如下提示错误:
error: Failed dependencies: rpmlib(FileDigests) <= 4.6.0-1 is needed by varnish-release-3.0-1.el6.noarch rpmlib(PayloadIsXz) <= 5.2-1 is needed by varnish-release-3.0-1.el6.noarchError: Missing Dependency: libedit.so.0 is needed by package varnish-3.0.5-1.el5.centos.i386 (varnish-3.0)
5、Centos 5安装时还会提示有依赖关系不能解决,解决的办法就是添加扩展的YUM 源,执行以下命令:
rpm -ivh http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.5.1-1.el5.rf.i386.rpm (32位)rpm -ivh http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm (64位)yum clean allyum update
6、对于是Debian系统,可以执行以下命令来安装:
curl http://repo.varnish-cache.org/debian/GPG-key.txt | apt-key add -echo "deb http://repo.varnish-cache.org/debian/ wheezy varnish-3.0" >> /etc/apt/sources.listapt-get updateapt-get install varnish
6、对于是Ubuntu系统,可以执行以下命令来安装:
curl http://repo.varnish-cache.org/debian/GPG-key.txt | sudo apt-key add -echo "deb http://repo.varnish-cache.org/ubuntu/ precise varnish-3.0" | sudo tee -a /etc/apt/sources.listsudo apt-get updatesudo apt-get install varnish
7、设置Varnish开机启动,执行:chkconfig varnish on和chkconfig varnishncsa on
8、启动Varnish的命令是:service varnish start和service varnishncsa start
9、这里还有一个Linux+Nginx+MySQL+PHP+Varnish一键安装包,自动安装完成:Varnish高性能开源HTTP加速器:Varnish Nginx和Varnish Apache搭建配置。
1、设置好Varnish缓存规则。默认是/etc/varnish/default.vcl,大家可以下载这个Varnish WordPress配置文件覆盖原来的,地址:http://centos.googlecode.com/files/default.vcl。源码内容:
# This is a basic VCL configuration file for varnish. See the vcl(7)# man page for details on VCL syntax and semantics. # Default backend definition. Set this to point to your content# server.backend default { .host = "127.0.0.1"; .port = "8080";}acl purge { "localhost"; "127.0.0.1";}# Below is a commented-out copy of the default VCL logic. If you# redefine any of these subroutines, the built-in logic will be# appended to your code.sub vcl_recv { # Only cache the following site if (req.http.host ~ "(amhg.freehao123.info)") { set req.backend = default; } else { return (pass); }if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } return (lookup); } if (req.restarts == 0) { if (req.http.x-forwarded-for) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { /* Non-RFC2616 or CONNECT which is weird. */ return (pipe); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.http.Authorization || req.http.Cookie ~ "wordpress_logged" || req.http.Cookie ~ "comment_") { /* Not cacheable by default */ return (pass); } return (lookup);}sub vcl_pipe { # Note that only the first request to the backend will have # X-Forwarded-For set. If you use X-Forwarded-For and want to # have it set for all requests, make sure to have: # set bereq.http.connection = "close"; # here. It is not set by default as it might break some broken web # applications, like IIS with NTLM authentication. return (pipe);}sub vcl_pass { return (pass);}sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } return (hash);}sub vcl_hit {if (req.request == "PURGE") { purge; error 200 "Purged."; } return (deliver);}sub vcl_miss {if (req.request == "PURGE") { purge; error 200 "Purged."; } return (fetch);}sub vcl_fetch { if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { /* * Mark as "Hit-For-Pass" for the next 2 minutes */ set beresp.ttl = 120 s; return (hit_for_pass); }set beresp.ttl = 1d; return (deliver);}sub vcl_deliver { return (deliver);}sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; set obj.http.Retry-After = "5"; synthetic {"<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html> <head> <title>"} + obj.status + " " + obj.response + {"</title> </head> <body> <h1>Error "} + obj.status + " " + obj.response + {"</h1> <p>"} + obj.response + {"</p> <h3>Guru Meditation:</h3> <p>XID: "} + req.xid + {"</p> <hr> <p>Varnish cache server</p> </body></html>"}; return (deliver);}sub vcl_init { return (ok);}sub vcl_fini { return (ok);}
2、下载下来的default.vcl你需要调整的地方有一处,就是将域名更改为自己要使用Varnish缓存的域名。
3、另外default.vcl还设置缓存时间,单位是s(秒),h(小时),d(天)。
4、配置Varnish的访问端口。Varnish配置默认的访问端口不是80端口,因此需要修改/etc/sysconfig/varnish配置文件,把端口设置为80。
5、在/etc/sysconfig/varnish这个文件中还可以设置Varnish缓存大小,默认是1GB。
1、Varnish的端口改为了80后,我们需要将Apache和Nginx改为非80端口,一般地进入到Apache和Nginx的配置文件修改即可。
2、特别注意:安装了虚拟主机控制面板,除了要修改nginx.conf、nginx-host.conf或者httpd.conf外,还要修改虚拟主机的配置文件,你需要根据自己的VPS主机面板来调整。
3、端口号一般是在配置文件的上方或者最下方。
1、上面操作完成后,执行重启命令:service varnish restart,必要时可以重启PHP、Apache和Nginx等。
2、Varnish清除缓存的格式是:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.url XXXX,其中6082是管理端口后,你可以自己在 /etc/sysconfig/varnish中定义。
3、如果你想清除所有的Varnish缓存,就执行:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.url ^.*$
4、如果你是想清除某一个页面的缓存,就执行:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.url /a/2014.html
5、如果你是想清除/a/b/s*.html这样的页面,执行:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.url ^/a/b/s.*$
6、如果你是想清除/a/b/*.html 这样的页面,执行:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.url ^/a/b.*$
7、总之,Varnish清除缓存遵循正则表达式,ban.url 是相对URL,查看最近的操作记录:varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban.list
8、Varnish HTTP Purge插件自动清除缓存。当你的Wordpress发表文章、编辑文章、修改主题、评论时,Varnish HTTP Purge会自动发出清除该页面缓存请求,无需自己手动清除。官网:https://wordpress.org/plugins/varnish-http-purge/
9、Varnish HTTP Purge也支持手动强制清除Varnish全部缓存。
1、Varnish缓存生效后,打开浏览器查看Http的Header头部信息,可以看到有网页是从Varnish缓存中读取。
2、执行:varnishstat 可以看到Varnish缓存状态。(点击放大)
3、Client connections accepted是成功发送HTTP请求的总数量,Client requests received到现在为止发送HTTP请求的累计次数,Cache hits在缓存区中查找并且命中缓存的次数,Cache misses非命中数,N struct object表示当前被缓存的数量。
4、查看Varnish内存使用量:varnishstat -1|grep SMA。
5、SMA.s0.g_space 表示当前可以应用的内存量, SMA.s0.g_bytes 表示的是当前cache实际的内存大小( SMA.s0.c_bytes= SMA.s0.g_bytes +SMA.s0.c_freed ),SMA.s0.c_bytes自动调节的范围,
6、从上图中可以看出我的一个256MB内存已经被分配光了,但是Varnish的命中率还是挺低的。(最主要的原因是这是一台测试VPS,几乎没有访问量,自然谈不上有多少访问缓存数据)
7、实际使用过程中,大家可以随时观察和分析自己的Varnish缓存命中率,如果Varnish命中率比较低的,可以在default.vcl中加入以下规则。
sub vcl_recv{if (!(req.url ~ "wp-(login|admin)")){unset req.http.cookie;}}sub vcl_fetch{if (!(req.url ~ "wp-(login|admin)")){unset obj.http.set-cookie;}}
8、该规则主要是:如果不是wordpress自身的登录和管理页面,就去掉页面http头部的set-cookie标记,从而让Wordpress绝大部分页面都直接使用Varnish缓存。
1、Memcached使用得比较广泛,常用的VPS控制面板,如WDCP、LNMP、AMH等都提供了Memcached扩展安装,参考:WDCP,LNMP安装PHP缓存加速扩展eAccelerator,xcache和memcached
2、对于Wordpress,我们可以使用基于Memcached 的WordPress 缓存插件:Batcache,这个插件主要是将Wordpress的页面缓存到内存中,第二次访问该页面时直接从内存中读取,官网:http://wordpress.org/plugins/batcache/
3、要想成功使用Batcache,你需要先让Wordpress用上Memcached ,下载一个插件:http://wordpress.org/plugins/memcached/,解压,将其中的object-cache.php文件上传到/wp-content/目录中。
4、然后将Batcache下载到本地,解压,将压缩包中的advanced-cache.php上传到/wp-content/目录中。
5、再将Batcache压缩包中的batcache.php上传到/wp-content/plugins/ 目录中。***在wp-config.php 文件首行中加入:define('WP_CACHE', true);
6、Batcache启用后,如果一切正常,刷新几下页面,在</head>标签前面会看到Batcache缓存标识数据。
7、查看Memcached内存使用情况,可以执行:telnet 127.0.0.1 11211,然后输入stats就可以看到Memcached缓存使用情况了。相关的说明如下:
8、注意:有些VPS可能没有安装telnet,执行以下命令安装:yum install xinetd和yum install telnet –server
9、另外,有用WP Super cache的朋友,可以直接启用WP Super cache的Memcached缓存加速:WP Super cache启用Memcached内存缓存加速及其效果分析。
七、Varnish和Memcached缓存加速小结
1、Varnish缓存安装比较简单,主要是注意修改Varnish的端口号为80,然后把Apache和Nginx的端口号修改为非80,Memcached缓存各大VPS控制面板都会提供直接安装方法,安装后在探针中可以检测到。
2、总得来说Varnish和Memcached都属于内存级别的缓存加速,Varnish即使服务器重启也会原有的缓存也不丢失,因此适合页面缓存,而Memcached更适合MysqL数据库缓存,可以大大减少数据库压力。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本文链接:http://www.yuyubebe.cn/2019/05/86/
发表评论