加速WordPress页面加载速度

本文介绍通过一系列优化,有效加速wordpress的页面读取速度,同时降低VPS的CPU使用率。本文环境:Centos + nginx +wordpress

首先,修改/etc/nginx/nginx.conf,开启针对Permalink的rewrite功能。在location / 字段内做如下修改:

        location / {
            root   /usr/share/nginx/html;
            index  index.php index.html index.htm;
            # START REWRITE
            if (!-e $request_filename) {
              rewrite ^.*$ /index.php last;
            }
            # END REWRITE
         }

然后重启nginx:

service nginx restart

然后在wordpress后台Settings/Permalinks里将Default改成”Custom Structure”,并且在右边填写下面字串:

/%postname%/

应该会提示需要修改.htaccess文件,这时将页面下方生成的代码贴到网站根目录的.htaccess里面(如没有则新建):

vim /usr/share/nginx/html/.htaccess

这时重新访问主页,点击任意文章应该能够正常访问。此时文章的URL会变成类似下面(IE):

http://wordpress.youran.me/%E5%8A%A0%E9%80%9Fwordpress%E9%A1
 %B5%E9%9D%A2%E5%8A%A0%E8%BD%BD%E9%80%9F%E5%BA%A6/

或者下面(Chrome):

http://wordpress.youran.me/加速WordPress页面加载速度/

如果提示404 ERROR请再确认nginx的配置。

接着下载Wordpress插件WP Super Cache,解压后存放到wp-content/plugins/目录下。到后台Plugins里启用WP Super Cache插件,并且按照提示修改wp-content目录以及wp-config.php文件的权限。

插件正常工作后,接下来在Easy选项卡里选中Caching On,然后到Advanced选项卡中查看是否选中了下列选项:

Cache hits to this website for quick access.
Use PHP to serve cache files.
Compress pages so they’re served more quickly to visitors.
Don’t cache pages for known users.
Cache rebuild. Serve a supercache file to anonymous users while a new file is being generated.
Extra homepage checks. (Very occasionally stops homepage caching)

其他选项可以不选。

配置好以后,就可以拿一个没有登录管理员账户的浏览器测试你的页面访问速度了!在我这里,速度有了质的提高。另外,可以去这里测试网站的gzip压缩是否已启用。

这里附上加速前后,网站的响应速度图表(400ms -> 250ms):

在有些本身访问慢的地区,这一优化更是使响应速度有着天壤之别(8000ms -> 900ms):

本文所述方法在本站应用成功,如有疑点及不正之处,欢迎留言讨论!

本文为悠然居(https://wordpress.youran.me/)的原创文章,转载请注明出处!

One thought on “加速WordPress页面加载速度”

Leave a Reply

Your email address will not be published. Required fields are marked *