刚刚从阿里云申请了个服务器,部署wordpress后,发现网站打开特别慢,经页面性能检测后,发现整体性能差的一踏糊涂,决定对网站性能进行调优。顺便记录下来,方便以后自己或其他同学查看。

经过一系列调优,分数从40多分提升到90多分。

性能分析

  • 性能检测工具: https://gtmetrix.com/
  • 这个工具不错,同时给出优化建议:https://varvy.com/pagespeed/

性能调优

1. google字体问题

打开wp-includes/script-loader.php,将https://fonts.googleapis.com替换成http://fonts.useso.com

2. 启动缓存

使用插件:WP Super Cache

3. Add Expires headers

# xxx/www/www/.htaccess
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
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"
# CSS
ExpiresByType text/css "access 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

4. Enable gzip compression

实际上又综合了这篇文章才解决的问题:

<IfModule mod_gzip.c>
AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

<IfModule mod_deflate.c>
     # Insert filter on all content
     ###SetOutputFilter DEFLATE
     # Insert filter on selected content types only
     AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
     #DeflateCompressionLevel 9
     SetOutputFilter DEFLATE

     # Netscape 4.x has some problems...
     #BrowserMatch ^Mozilla/4 gzip-only-text/html

     # Netscape 4.06-4.08 have some more problems
     #BrowserMatch ^Mozilla/4\.0[678] no-gzip

     # MSIE masquerades as Netscape, but it is fine
     #BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

     # Don't compress images
     #SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

     # Make sure proxies don't deliver the wrong content
     #Header append Vary User-Agent env=!dont-vary

 </IfModule>