加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

使用Varnish和Memcached给WordPress网站提速

发布时间:2022-06-20 12:34:59 所属栏目:教程 来源:互联网
导读:Varnish是一款高性能的缓存加速器,Varnish把数据存放在服务器的内存中,利用内存可以极大的提高PHP页面执行速度,可以设置0~60秒的精确缓存时间,32位的机器支持的缓存文件最大为2GB。 Varnish采用VCL的配置,而且具有强大的管理功能,如top、stat、admin
  Varnish是一款高性能的缓存加速器,Varnish把数据存放在服务器的内存中,利用内存可以极大的提高PHP页面执行速度,可以设置0~60秒的精确缓存时间,32位的机器支持的缓存文件最大为2GB。
 
  Varnish采用VCL的配置,而且具有强大的管理功能,如top、stat、admin、lis,管理方式比较灵活。Varnish的状态机设计不仅巧妙,结构也很清晰,利用二叉堆管理缓存文件,即可达到随时删除的目的。
 
  由于Varnish采用了VisualPageCache技术,所有缓存的数据都直接从内存读取,而Squid从硬盘读取缓存的数据,所以Varnish在访问速度方面会更快一些。但是Varnish在高并发状态下,CPU、IO和内存等资源的开销高于Squid。
 
  目前Varnish3.0版本解决了服务器重启后Varnish缓存消失的问题,性能优化上有了更大的提升。本篇就来分享一下利用Varnish和Memcached缓存来给Wordpress加速,因为要用到内存,所以比较适合那些大内存的服务器。
 
  一、Varnish安装方法
 
  1、Varnish
 
  varnishcache
 
  2、对于是Centos6的,可以执行以下命令来安装
 
  rpmnosignatureirepo.varnishcacheredhatvarnish3.0el6noarchvarnishreleasevarnishrelease3.01.el6.noarch.rpm
 
  yuminstallvarnish

  3、对于Centos5的,可以执行以下命令来安装
 
  rpmnosignatureirepo.varnishcacheredhatvarnish3.0el5noarchvarnishreleasevarnishrelease3.01.el5.centos.noarch.rpm
 
  yuminstallvarnish
 
  4、如果版本搞错,就会出现如下提示错误
 
  error:Faileddependencies:
 
  rpmlibFileDigests=4.6.01isneededbyvarnishrelease3.01.el6.noarch

  rpmlibPayloadIsXz=5.21isneededbyvarnishrelease3.01.el6.noarch
 
  :MissingDependency:libedit.so.0isneededbypackagevarnish3.0.51.el5.centos.i386varnish3.0
 
  5、Centos5安装时还会提示有依赖关系不能解决,解决的办法就是添加扩展的YUM源,执行以下命令
 
  rpmivhapt.sw.beredhatel5eni386rpmforgeRPMSrpmforgerelease0.5.11.el5.rf.i386.rpm32位

        rpmivhapt.sw.beredhatel5enx86_64rpmforgeRPMSrpmforgerelease0.5.22.el5.rf.x86_64.rpm64位
 
        yumcleanallyumupdate
 
  6、对于是Ubuntu系统,可以执行以下命令来安装
 
  curlrepo.varnishcachedebianGPGkey.txt|sudoaptkeyaddecho"debrepo.varnishcacheubuntuprecisevarnish3.0"|sudoteeaetcaptsources.listsudo
 
        aptgetupdatesudo
 
        aptgetinstallvarnish
 

  7、对于是Debian系统,可以执行以下命令来安装
 
  curlrepo.varnishcachedebianGPGkey.txt|aptkeyaddecho"debrepo.varnishcachedebianwheezyvarnish3.0"etcaptsources.list
 
        aptgetupdate

        aptgetinstallvarnish
 
  8、设置Varnish开机启动,执行 chkconfigvarnishon和chkconfigvarnishncsaon
 
  9、启动Varnish的命令是 servicevarnishstart和servicevarnishncsastart
 
  二、Varnish相关配置
 
  1、设置好Varnish缓存规则。默认是etcvarnishdefault.vcl,大家可以下载这个VarnishWordPress配置文件覆盖原来的, centos.googlecodefilesdefault.vcl。源码内容
     #ThisisabasicVCLconfigurationfileforvarnish.Seethevcl7#manpagefordetailsonVCLsyntaxandsemantics.#Defaultbackenddefinition.Setthistopointtoyourcontent#server.backenddefault{.host="127.0.0.1";.port="8080";}aclpurge{"localhost";"127.0.0.1";}#BelowisacommentedoutcopyofthedefaultVCLlogic.Ifyou#redefineanyofthesesubroutines,thebuiltinlogicwillbe#appendedtoyourcode.subvcl_recv{#Onlycachethefollowingsiteifreq.http.host~"amhg.freehao123.info"{setreq.backend=default;}else{returnpass;}ifreq.request=="PURGE"{if!client.ip~purge{error405"Notallowed.";}returnlookup;}ifreq.restarts==0{ifreq.http.xforwardedfor{setreq.http.XForwardedFor=req.http.XForwardedFor+","+client.ip;}else{setreq.http.XForwardedFor=client.ip;}}ifreq.request!="GET"req.request!="HEAD"req.request!="PUT"req.request!="POST"req.request!="TRACE"req.request!="OPTIONS"req.request!="DELETE"{ NonRFC2616orCONNECTwhichisweird. returnpipe;}ifreq.request!="GET"req.request!="HEAD"{ WeonlydealwithGETandHEADbydefault returnpass;}ifreq.http.Authorization||req.http.Cookie~"wordpress_logged"||req.http.Cookie~"comment_"{ Notcacheablebydefault returnpass;}returnlookup;}subvcl_pipe{#Notethatonlythefirstrequesttothebackendwillhave#XForwardedForset.IfyouuseXForwardedForandwantto#haveitsetforallrequests,makesuretohave:#setbereq.http.connection="close";#here.Itisnotsetbydefaultasitmightbreaksomebrokenweb#applications,likeIISwithNTLMauthentication.returnpipe;}subvcl_pass{returnpass;}subvcl_hash{hash_datareq.url;ifreq.http.host{hash_datareq.http.host;}else{hash_dataserver.ip;}returnhash;}subvcl_hit{ifreq.request=="PURGE"{purge;error200"Purged.";}returndeliver;}subvcl_miss{ifreq.request=="PURGE"{purge;error200"Purged.";}returnfetch;}subvcl_fetch{ifberesp.ttl=0s||beresp.http.SetCookie||beresp.http.Vary==" "{ Markas"HitForPass"forthenext2minutes setberesp.ttl=120s;returnhit_for_pass;}setberesp.ttl=1d;returndeliver;}subvcl_deliver{returndeliver;}subvcl_error{setobj.http.ContentType="texthtml;charset=utf8";setobj.http.RetryAfter="5";synthetic{"?xmlversion="1.0"encoding="utf8"?!DOCTYPEhtmlPUBLIC"W3CDTDXHTML1.0StrictEN""w3TRxhtml1DTDxhtml1strict.dtd"htmlheadtitle"}+obj.status+""+obj.response+{"titleheadbodyError"}+obj.status+""+obj.response+{"h1"}+obj.response+{"GuruMeditation:h3XID:"}+req.xid+{"Varnishcacheserverbodyhtml"};returndeliver;}subvcl_init{returnok;}subvcl_fini{returnok;}
 
  2、下载下来的default.vcl你需要调整的地方有一处,就是将域名更改为自己要使用Varnish缓存的域名。
 
  3、另外default.vcl还设置缓存时间,单位是s秒,h小时,d天。配置Varnish的访问端口。Varnish配置默认的访问端口不是80端口,因此需要修改etcsysconfigvarnish配置文件,把端口设置为80。
 
  4、在etcsysconfigvarnish这个文件中还可以设置Varnish缓存大小,默认是1GB。
 
 

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!