Varnish是一款高性能的緩存加速器,Varnish把數據存放在服務器的內存中,利用內存可以極大的提高PHP頁面執行速度,可以設置0~60秒的精確緩存時間,32位的機器支持的緩存文件最大為2GB。
Varnish采用VCL的配置,而且具有強大的管理功能,如top、stat、admin、lis,管理方式比較靈活。Varnish的狀態機設計不僅巧妙,結構也很清晰,利用二叉堆管理緩存文件,即可達到隨時刪除的目的。
Memcached是一個高性能的分布式內存對象緩存系統,通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動的速度。Memcached對于減少MysqL數據查詢壓力非常有幫助。
由于Varnish采用了VisualPageCache技術,所有緩存的數據都直接從內存讀取,而Squid從硬盤讀取緩存的數據,所以Varnish在訪問速度方面會更快一些。但是Varnish在高并發狀態下,CPU、IO和內存等資源的開銷高于Squid。
目前Varnish3.0版本解決了服務器重啟后Varnish緩存消失的問題,性能優化上有了更大的提升。本篇就來分享一下利用Varnish和Memcached緩存來給Wordpress加速,因為要用到內存,所以比較適合那些大內存的服務器。
用Varnish和Memcached緩存給Wordpress提速內存級加速
一、Varnish安裝方法
1、Varnish
varnishcache
2、對于Centos5的,可以執行以下命令來安裝
rpmnosignatureirepo.varnishcacheredhatvarnish3.0el5noarchvarnishreleasevarnishrelease3.01.el5.centos.noarch.rpm
yuminstallvarnish
3、對于是Centos6的,可以執行以下命令來安裝
rpmnosignatureirepo.varnishcacheredhatvarnish3.0el6noarchvarnishreleasevarnishrelease3.01.el6.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、對于是Debian系統,可以執行以下命令來安裝
- curlrepo.varnishcachedebianGPGkey.txt|aptkeyaddecho"debrepo.varnishcachedebianwheezyvarnish3.0"etcaptsources.list
- aptgetupdate
- aptgetinstallvarnish
7、對于是Ubuntu系統,可以執行以下命令來安裝
- curlrepo.varnishcachedebianGPGkey.txt|sudoaptkeyaddecho"debrepo.varnishcacheubuntuprecisevarnish3.0"|sudoteeaetcaptsources.listsudo
- aptgetupdatesudo
- 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天。
4、配置Varnish的訪問端口。Varnish配置默認的訪問端口不是80端口,因此需要修改etcsysconfigvarnish配置文件,把端口設置為80。
5、在etcsysconfigvarnish這個文件中還可以設置Varnish緩存大小,默認是1GB。
新聞熱點
疑難解答
圖片精選