fny5jt9 发表于 2024-10-3 06:11:23

WordPress 性能优化:为么我的博客比你的快


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">经常看我爱水煮鱼博客的<span style="color: black;">朋友</span>,有<span style="color: black;">无</span><span style="color: black;">发掘</span>我的博客非常快,而你的博客比较慢呢?那是<span style="color: black;">由于</span>我的博客经过了优化。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">起始</span><span style="color: black;">文案</span>之前,插播一句,<span style="color: black;">首要</span>需要将 WordPress 更新到 6.1,<span style="color: black;">由于</span> WordPress 6.1 无需插件<span style="color: black;">就可</span>实现站点 0 SQL。</span></span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">WordPress 很慢?</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">非常多</span>人都会觉得 WordPress 很慢?</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">做为</span>全世界最通用的建站和博客系统 WordPress <span style="color: black;">来讲</span>,在设计上肯定不会有太大的性能问题,WordPress <span style="color: black;">研发</span>团队<span style="color: black;">亦</span>肯定<span style="color: black;">亦</span><span style="color: black;">思虑</span>到这个问题。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">那样</span> WordPress 效率问题<span style="color: black;">重点</span>是在哪里呢?</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">按照</span>我<span style="color: black;">数年</span> WordPress 建站和<span style="color: black;">守护</span>的经验,<span style="color: black;">非常多</span>性能的问题都是 WordPress <span style="color: black;">运用</span><span style="color: black;">欠妥</span><span style="color: black;">导致</span>的,<span style="color: black;">例如</span>安装太多效率低下的插件,<span style="color: black;">运用</span>代码质量低下的主题,<span style="color: black;">无</span>正确处理 WordPress 的缓存等等,<span style="color: black;">因此</span><span style="color: black;">加强</span> WordPress 性能,就要从这些方面入手。</span></span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/af22dc4307eb40648f17f8a8d2aefc3d~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728087536&amp;x-signature=4mNMj2MFm0mY0fRAtZ30BAZWCi4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">今天就给<span style="color: black;">大众</span>讲讲自己<span style="color: black;">数年</span>总结的 WordPress 优化经验和技巧:</span></span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">动静态分离</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">不管 WordPress 博客还是其他 CMS 建的站点,优化的<span style="color: black;">第1</span>步<span style="color: black;">必定</span>是动静态分离,这<span style="color: black;">亦</span>是腾讯,淘宝等大网站,<span style="color: black;">她们</span><span style="color: black;">照片</span>的域名和网站的域名不是同一个的<span style="color: black;">原由</span>。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">网站的静态文件<span style="color: black;">通常</span>有两种:</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">第1</span>种是网站的 CSS,JS 和<span style="color: black;">有些</span>主题的常用背景<span style="color: black;">照片</span>和按钮文件,这些<span style="color: black;">照片</span><span style="color: black;">倘若</span>网站不进行改版<span style="color: black;">或</span>其他改动,基本上是不会修改的。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">第二种是<span style="color: black;">每日</span>更新的网站内容时上传的<span style="color: black;">照片</span><span style="color: black;">或</span>附件,这些<span style="color: black;">照片</span>文件<span style="color: black;">亦</span>是基本不会改动的。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">处理</span>好静态文件的存储和加速,网站性能就<span style="color: black;">首要</span>能得到基本的<span style="color: black;">保准</span>了,WordPress 构建的网站和博客<span style="color: black;">亦</span>是<span style="color: black;">同样</span>的。<span style="color: black;">针对</span>这些静态文件<span style="color: black;">来讲</span>,最好的<span style="color: black;">处理</span><span style="color: black;">方法</span>永远是<span style="color: black;">运用</span> CDN 网络进行加速,<span style="color: black;">这般</span>服务器的压力将大大降低,<span style="color: black;">由于</span><span style="color: black;">拜访</span>页面<span style="color: black;">仅有</span>当前页面是在自己的服务器上,其他所有<span style="color: black;">照片</span>,JS 和 CSS 都是从 CDN 上获取的。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">WordPress 博客<span style="color: black;">怎样</span><span style="color: black;">运用</span> CDN 服务,我写的 WPJAM Basic 插件的「CDN 加速」功能,<span style="color: black;">已然</span>支持主流的云存储服务:阿里云OSS 和腾讯云COS 等,只要你安装了WPJAM Basic 插件,就<span style="color: black;">能够</span>一键实现 WordPress 博客静态文件 CDN 加速。</span></span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">服务器优化</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">优化好静态文件之后,就要<span style="color: black;">起始</span>对网站的动态内容进行优化,优化动态内容,<span style="color: black;">首要</span>要有一个稳定网络环境,稳定的主机供应商和服务器性能的优化。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">选取</span>一个<span style="color: black;">可靠</span>的主机托管商,个人<span style="color: black;">举荐</span> BGP <span style="color: black;">或</span>多线机房,让全国用户<span style="color: black;">拜访</span>都能有不错的速度,<span style="color: black;">而后</span><span style="color: black;">尽可能</span><span style="color: black;">选取</span>独立的服务器,再不济,<span style="color: black;">亦</span>得 VPS, 因为你<span style="color: black;">无</span>服务器的 Root 权限<span style="color: black;">非常多</span>东西是<span style="color: black;">没法</span>进行的,国内<span style="color: black;">通常</span><span style="color: black;">举荐</span>:阿里云 和 腾讯云。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">服务器问题<span style="color: black;">处理</span>之后,服务器<span style="color: black;">能够</span>做如下优化:</span></span></p><span style="color: black;"><span style="color: black;">安装 Memcached 和 PHP Memcached 扩展,参考:<span style="color: black;">运用</span> Memcached 来缓存 WordPress 数据。</span></span><span style="color: black;"><span style="color: black;">PHP 开启 OPCache:把经过编译后的 PHP 代码缓存到共享内存中,并在用户<span style="color: black;">拜访</span>的时候直接调用从而起到<span style="color: black;">有效</span>的加速<span style="color: black;">功效</span>。</span></span><span style="color: black;"><span style="color: black;">MySQL 设置 MySQL Query Cache,<span style="color: black;">能够</span><span style="color: black;">保留</span><span style="color: black;">保留</span><span style="color: black;">查找</span>结果,<span style="color: black;">一样</span>的<span style="color: black;">查找</span><span style="color: black;">再也不</span>从数据库检索,对速度有很大的<span style="color: black;">提高</span>。</span></span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">WordPress 缓存和优化</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">要彻底明白和搞懂 WordPress 性能优化,<span style="color: black;">首要</span>要理解 WordPress 缓存机制,WordPress 默认是一种叫做 WordPress Object Cache 的对象缓存机制,它是把需要缓存的内容<span style="color: black;">根据</span> Key-Value <span style="color: black;">这般</span>的模式进行缓存(和 No-SQL 的 key-value 的有点类似),当然它还支持<span style="color: black;">根据</span> Group 来划分和避免缓存的内容冲突。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因此</span>最<span style="color: black;">基本</span>的 WordPress 缓存插件<span style="color: black;">便是</span>,把 WordPress 产生的 Key-Value 存起来,<span style="color: black;">倘若</span>是<span style="color: black;">运用</span> Memcached,<span style="color: black;">便是</span>存到内存,<span style="color: black;">倘若</span><span style="color: black;">运用</span>文件<span style="color: black;">便是</span>存到硬盘中。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">当然高级的 WordPress 插件还能做<span style="color: black;">更加多</span>,<span style="color: black;">例如</span> WP Super Cache WordPress <span style="color: black;">各样</span>缓存插件介绍和应用把<span style="color: black;">全部</span>页面缓存到硬盘中,下次直接<span style="color: black;">拜访</span>静态的 HTML 文件,让服务器直接绕过 PHP,节约 CPU 时间。Batcache 会把<span style="color: black;">全部</span>页面当做一个对象存到内存里面。我个人<span style="color: black;">举荐</span><span style="color: black;">运用</span> Memcached 足够。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">Memcached 会把 WordPress 的对象存到内存里面,服务器的内存足够大,读取和存储速度<span style="color: black;">亦</span>够快,并且内存缓存命中率<span style="color: black;">亦</span>大于 99%。<span style="color: black;">另一</span><span style="color: black;">咱们</span> WordPress 程序经过优化之后,<span style="color: black;">每一个</span>页面的<span style="color: black;">查找</span><span style="color: black;">通常</span>在 2 条<span style="color: black;">上下</span>,<span style="color: black;">因此</span><span style="color: black;">全部</span>网站效率很高,</span><strong style="color: blue;"><span style="color: black;">首页<span style="color: black;">乃至</span>做到 0 SQL,页面生成只需要 0.0013秒</span></strong><span style="color: black;">。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">WPJAM Basic 插件<span style="color: black;">已然</span>集<span style="color: black;">成为了</span> Memcached,下载 WPJAM Basic之后,将 wpjam-basic/template/ 目录下的 object-cache.php 文件复制到 wp-content 目录下<span style="color: black;">就可</span>。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">WordPress <span style="color: black;">各样</span>用不上的功能,<span style="color: black;">或</span>可能影响性能的功能,<span style="color: black;">倘若</span>可能<span style="color: black;">亦</span><span style="color: black;">尽可能</span>关闭,WPJAM Basic 的优化设置功能,<span style="color: black;">已然</span>把这<span style="color: black;">一起</span>做到了极致,点击<span style="color: black;">这儿</span>查看一下优化设置功能介绍,这<span style="color: black;">亦</span>是我<span style="color: black;">始终</span><span style="color: black;">举荐</span> WPJAM Basic,是真正的优化插件。</span></span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d1c0fae3d5d64ae7927cf841d8a2324b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728087536&amp;x-signature=N8vPr0NA%2BOLHJsnEX09QA%2BNbLO0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">主题和插件优化</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">主题和插件优化优化<span style="color: black;">亦</span>是基于 WordPress Object Cache 的机制对 WordPress 插件和主题的代码进行优化,<span style="color: black;">重点</span>经验有以下几点:</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">大众</span>都能<span style="color: black;">把握</span>的:</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">只<span style="color: black;">运用</span>必须的 WordPress 插件,安装太多的 WordPress 插件很容易<span style="color: black;">导致</span>性能问题,并且<span style="color: black;">倘若</span>从<span style="color: black;">有些</span>不明途径下载插件,可能还有安全问题。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">从正规站点下载 WordPress 主题,<span style="color: black;">这般</span>下载的主题<span style="color: black;">才可</span><span style="color: black;">保准</span>质量和安全,<span style="color: black;">举荐</span>我和 xintheme 发布的收费的 Autumn-Pro 和免费的 Sweet 主题,关于主题这<span style="color: black;">一起</span><span style="color: black;">能够</span>还<span style="color: black;">能够</span>深度<span style="color: black;">认识</span>一下:WordPress 性能优化的<span style="color: black;">基本</span>是一款速度够快的主题。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">倘若</span>你会写程序:</span></strong></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">WordPress 主题和插件<span style="color: black;">尽可能</span><span style="color: black;">运用</span>模板函数,<span style="color: black;">由于</span> WordPress 模板函数<span style="color: black;">倘若</span>可能都<span style="color: black;">已然</span>做好了 WordPress Object Cache。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">例如</span> </span><span style="color: black;"><span style="color: black;">get_the_terms</span></span><span style="color: black;"> 和 </span><span style="color: black;"><span style="color: black;">wp_get_object_terms</span></span><span style="color: black;"> 这两个函数,功能基本<span style="color: black;">同样</span>,<span style="color: black;">然则</span> </span><span style="color: black;"><span style="color: black;">get_the_terms</span></span><span style="color: black;"> 直接从对象缓存中取数据,无 SQL <span style="color: black;">查找</span>,而 </span><span style="color: black;"><span style="color: black;">wp_get_object_terms</span></span><span style="color: black;"> 每次都从数据库中取数据。</span></span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">WordPress 插件和主题<span style="color: black;">倘若</span><span style="color: black;">必定</span>要直接<span style="color: black;">查找</span>数据库,请做好 Object Cache,将<span style="color: black;">查找</span>的结果<span style="color: black;">运用</span> </span><span style="color: black;"><span style="color: black;">wp_cache_set</span></span><span style="color: black;"> 存到 Object Cache 中,下次直接<span style="color: black;">运用</span> </span><span style="color: black;"><span style="color: black;">wp_cache_get</span></span><span style="color: black;"> 获取。</span></span></p>




页: [1]
查看完整版本: WordPress 性能优化:为么我的博客比你的快