当前位置:首页 > CMS教程 > WordPress > 列表

用Varnish和Memcached给WordPress网站提速

发布:smiling 来源: PHP粉丝网  添加日期:2018-12-24 10:50:30 浏览: 评论:0 

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、如果版本搞错,就会出现如下提示错误

  1. error:Faileddependencies: 
  2.  
  3. rpmlibFileDigests=4.6.01isneededbyvarnishrelease3.01.el6.noarch 
  4.  
  5. rpmlibPayloadIsXz=5.21isneededbyvarnishrelease3.01.el6.noarch 
  6.  
  7. :MissingDependency:libedit.so.0isneededbypackagevarnish3.0.51.el5.centos.i386varnish3.0 

5、Centos5安装时还会提示有依赖关系不能解决,解决的办法就是添加扩展的YUM源,执行以下命令

  1. rpmivhapt.sw.beredhatel5eni386rpmforgeRPMSrpmforgerelease0.5.11.el5.rf.i386.rpm32位 
  2.  
  3.       rpmivhapt.sw.beredhatel5enx86_64rpmforgeRPMSrpmforgerelease0.5.22.el5.rf.x86_64.rpm64位 
  4.  
  5.       yumcleanallyumupdate 

6、对于是Debian系统,可以执行以下命令来安装

  1. curlrepo.varnishcachedebianGPGkey.txt|aptkeyaddecho"debrepo.varnishcachedebianwheezyvarnish3.0"etcaptsources.list 
  2.  
  3.       aptgetupdate 
  4.  
  5.       aptgetinstallvarnish 

7、对于是Ubuntu系统,可以执行以下命令来安装

  1. curlrepo.varnishcachedebianGPGkey.txt|sudoaptkeyaddecho"debrepo.varnishcacheubuntuprecisevarnish3.0"|sudoteeaetcaptsources.listsudo 
  2.  
  3.       aptgetupdatesudo 
  4.  
  5.       aptgetinstallvarnish 

8、设置Varnish开机启动,执行 chkconfigvarnishon和chkconfigvarnishncsaon

9、启动Varnish的命令是 servicevarnishstart和servicevarnishncsastart

二、Varnish相关配置

1、设置好Varnish缓存规则。默认是etcvarnishdefault.vcl,大家可以下载这个VarnishWordPress配置文件覆盖原来的, centos.googlecodefilesdefault.vcl。源码内容

  1. #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。

Tags: Varnish Memcached 网站提速

分享到:

相关文章