• 10步教你来优化WordPress速度 为服务器和访客减压


    1.Cookie的静态化制作

    约有80%至90%的时间,访客要花费大量的时间等你的WordPress加载静态内容。这意味着,有大部分的时间,用户浏览您的网站,他们正在等待加载,如:图像,CSS,JS脚本,flash等等。所以你可以优化内容,更快捷地让WordPress加载一个cookie域。消除了负载时的一些珍贵的时间,当然了这可能看起来并不多,但添加其他拖延问题时,才能真正减少时间损失。

    我不知道其他源码的网站应该怎么做,但是wordpress很简单只要修改一下wp-config.php就好了!

    在合适的位置加入:

    define("WP_CONTENT_URL", "http://static.***.com");

    define("COOKIE_DOMAIN", "www.***.com");

    然后用“bloginfo(template_directory')”这段代码加入你要实现的静态内容中即可!

    2.设置静态内容的缓存

    静态的内容缓存,我就不多介绍了,你用相关的缓存插件也需要这个步骤,那就是修改.htaccess文件

    代码如下:

    <IfModule mod_expires.c>
    ExpiresActive on
    # Perhaps better to whitelist expires rules? Perhaps.
    ExpiresDefault      "access plus 1 month"
    # cache.appcache needs re-requests
    # in FF 3.6 (thx Remy ~Introducing HTML5)
    ExpiresByType text/cache-manifest "access plus 0 seconds"
    # Your document html
    ExpiresByType text/html "access plus 0 seconds"
    # Data
    ExpiresByType text/xml "access plus 0 seconds"
    ExpiresByType application/xml "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"
    # RSS feed
    ExpiresByType application/rss+xml "access plus 1 hour"
    # Favicon (cannot be renamed)
    ExpiresByType image/x-icon "access plus 1 week"
    # Media: images, video, audio
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType video/ogg "access plus 1 month"
    ExpiresByType audio/ogg "access plus 1 month"
    ExpiresByType video/mp4 "access plus 1 month"
    ExpiresByType video/webm "access plus 1 month"
    # HTC files  (css3pie)
    ExpiresByType text/x-component "access plus 1 month"
    # Webfonts
    ExpiresByType font/truetype "access plus 1 month"
    ExpiresByType font/opentype "access plus 1 month"
    ExpiresByType application/x-font-woff   "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
    ExpiresByType text/javascript "access plus 1 year"
    <IfModule mod_headers.c>
    Header append Cache-Control "public"
    </IfModule>
    </IfModule>

     3.优化数据库

    有一个方法来为的服务器减压,怎样才能优化wordpress的数据库呢?自然是用代码,用代码,嘿嘿,你可能编辑了几个小时都没有效果!WordPress的免费插件使得这个问题变得简单,个人推荐Yoast Optimize DBWP DB Manager。牢记大多数缓存插件还帮助了很多本,所以你会发现大多数正在采取照顾您已经现存的缓存软件的工作量。

    4.尽量缓存媒体

    创建缓存可以很简单地通过插件完成。从本质上讲,这些新增转成静态页面,所有这些方式,既方便加载,又节省您的浏览器前端的滞后。那么,什么插件,听我的建议吧。

    我最喜欢的缓存工具:WP Super Cache, Hyper Cache,和W3 Total Cache。用这些生成HTML文件,可以节省更多的时间。

    5.使用CDN

    CDN加速目前很火,那么自然火是有一定的道理的,因为CDN可以加速呀。呵呵,cdn一般是收费,不过免费的CDN也有。

    下面为大家推荐一些实用的免费CDN:

    免费CDN:Weblukercloudflare

    6.压缩JS和CSS文件

    你可能只听过说JS和CSS文件的库转移,总没有听说过压缩吧。呵呵,这个方法是很异想天开,但是就是有人做到了,并且开发了压缩的实用工具,当然机器的压缩并不尽人意,你也可以手工压缩。

    JS压缩工具推荐:Closure Compiler ,Minify JavaScript. CSS压缩工具推荐:Minify CSS ,CSS Compressor

    7.压缩图像

    图像的加载也是比较耗时的,想你如果试用BMP或者PSD作为图像那么速度一定和蜗牛爬一样,比较高质量保存的PNG文件我也不怎么推荐。建议试用JPG压缩,但是也不要过度!不然图片会失真。

    通过WP Smush.it插件你可以比较简单的来压缩图片!

    8.GZIP压缩

    GZIP的压缩在一定限度内是很不错的!而且通过gzip压缩不仅能够加速而且还可以屏蔽广告(免费空间的福音啊)GZIP的压缩代码交给你!修改.htaccess文件

    <IfModule mod_deflate.c>
    # force deflate for mangled headers
    # developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping/
    <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
    SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)s*,?s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
    RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
    </IfModule>
    </IfModule>
    # HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
    <IfModule filter_module>
    FilterDeclare   COMPRESS
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/html
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/css
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/plain
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $text/x-component
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/javascript
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/json
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/xhtml+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/rss+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/atom+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/vnd.ms-fontobject
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $image/svg+xml
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $application/x-font-ttf
    FilterProvider  COMPRESS  DEFLATE resp=Content-Type $font/opentype
    FilterChain     COMPRESS
    FilterProtocol  COMPRESS  DEFLATE change=yes;byteranges=no
    </IfModule>
    <IfModule !mod_filter.c>
    # Legacy versions of Apache
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
    </IfModule>
    </IfModule>

     9.禁用ETAGS

    ETAGS可能我也搞不懂,但是你仅用了它就可以加速了,所以你管他神马ETAGS你说是不?

    在.htaccess 文件中+这一行!

    File ETag none

     10.服务器环境

    使用LAMP环境或者LNMP环境,可以更好的支持wordpress并且风险系数低,也很安全,估计你的服务器或者VPS几年都不会重启一次,所以网上总是我自己的linux密码忘记了重启怎么办! 这就是免费惹的祸,真是躺着也中枪啊!

    总结:

    这些方法中大多数只适合在LAMP或者LNMP的环境下,所以使用wordpress就必须要在linux环境下,win环境下的wordpress你会遇到一些列的问题!

    本文来自:米饭维谷 教程餐厅组 | http://www.wg369.com/archives/1989.html

  • 相关阅读:
    树莓派安装aria2轻松下载小资源
    利用树莓派3搭建可以发射无线局域网的微型服务器
    Vue语法
    redis-cluster集群
    分布式锁
    Redis--消息队列
    设计模式---享元模式
    设计模式(单例模式+原型模式)+ c#的内存分配机制
    MVC的ActionFilter
    MVC异常处理的7大场景 + MVC的异常处理的过滤器 + 全局异常
  • 原文地址:https://www.cnblogs.com/gcy77/p/4027549.html
Copyright © 2020-2023  润新知