使用memcache对wordpress优化,提速

环境:

一个本地开发环境

一个部署在虚拟机中( 虚拟机安装memcache缓存),然后用这个优化的版本跟本地开发环境对比.

wordpress版本:4.9.5

php:5.6版本

1,首先在主题的functions.php文件中,加入以下监控统计代码

  1. function performance( $visible = true ) {
  2.     $stat = sprintf(  '%d queries in %.3f seconds, using %.2fMB memory',
  3.         get_num_queries(),
  4.         timer_stop( 03 ),
  5.         memory_get_peak_usage() / 1024 / 1024
  6.     );
  7.     // echo $visible ? $stat : "<!-- {$stat} -->" ;
  8.     echo "<div class='container_12'><p class='grid_12 footer clearfix'>$stat</p></div>";
  9. }
  10. add_action( 'wp_footer', 'performance', 20 );

样式可以根据自己的主题定制,这段代码可以统计出页面有多少次数据库查询。总共花了多少秒,耗费多少内存,类似如下的样式:

  1. 16 queries in 1.306 seconds, using 12.98MB memory

2,安装memcached服务端

  1. [root@bogon wp-content]# ps -ef | grep memcached
  2. root      5050  3466  0 14:44 pts/1    00:00:00 grep memcached
  3. root     29269     1  0 13:57 ?        00:00:02 /usr/local/memcached/bin/memcached -d -l 127.0.0.1 -p 11211 -u root -m 64 -c 1024 -P /var/run/memcached.pid

3,安装php memcache扩展

wget http://pecl.php.net/get/memcache-3.0.8.tgz,用phpize外挂方式安装,安装完成之后,在php.ini中启用,再重启php,确定memcache已经加载

  1. memcache
  2. memcache support    enabled
  3. Version    3.0.8
  4. Revision    $Revision: 329835 $
  5. Directive    Local Value    Master Value
  6. memcache.allow_failover    1    1
  7. memcache.chunk_size    32768    32768
  8. memcache.compress_threshold    20000    20000
  9. memcache.default_port    11211    11211
  10. memcache.hash_function    crc32    crc32
  11. memcache.hash_strategy    consistent    consistent
  12. memcache.lock_timeout    15    15
  13. memcache.max_failover_attempts    20    20
  14. memcache.protocol    ascii    ascii
  15. memcache.redundancy    1    1

4,下载wordpress memcache插件

wget https://downloads.wordpress.org/plugin/memcached.3.0.1.zip

把object-cache.php解压到wp-content目录下

  1. [root@bogon wp-content]# ls
  2. index.php  languages  object-cache.php  package.xml  plugins  themes  upgrade  uploads
  3. [root@bogon wp-content]#

最后比较服务器

安装了memcache缓存的版本,首页性能:

  1. 3 queries in 0.451 seconds, using 13.56MB memory

没有启用缓存的系统,首页性能:

  1. 16 queries in 1.336 seconds, using 12.98MB memory

可以看见有很明显的提升,其他页面都会被优化,缓存到

启用opcache,再次提速:

  1. 3 queries in 0.129 seconds, using 3.48MB memory

php.ini中opcache需要这样加载:

  1. zend_extension=opcache.so

opcache相关配置:

  1. [opcache]  
  2. ; Determines if Zend OPCache is enabled  
  3. opcache.enable=1  
  4.   
  5. ; Determines if Zend OPCache is enabled for the CLI version of PHP  
  6. opcache.enable_cli=1  
  7.   
  8. ; The OPcache shared memory storage size.  
  9. opcache.memory_consumption=528  
  10.   
  11. ; The amount of memory for interned strings in Mbytes.  
  12. opcache.interned_strings_buffer=8  
  13.   
  14. ; The maximum number of keys (scripts) in the OPcache hash table.  
  15. ; Only numbers between 200 and 100000 are allowed.  
  16. opcache.max_accelerated_files=10000  
  17.   
  18. ; The maximum percentage of "wasted" memory until a restart is scheduled.  
  19. ;opcache.max_wasted_percentage=5  
  20. "php.ini" 2051L, 74201C written                                                                                        
  21.   
  22.     
  23. [ldap]  
  24. ; Sets the maximum number of open links or -1 for unlimited.  
  25. ldap.max_links = -1  
  26.   
  27. [mcrypt]  
  28. ; For more information about mcrypt settings see http://php.net/mcrypt-module-open  
  29.   
  30. ; Directory where to load mcrypt algorithms  
  31. ; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)  
  32. ;mcrypt.algorithms_dir=  
  33.   
  34. ; Directory where to load mcrypt modes  
  35. ; Default: Compiled in into libmcrypt (usually /usr/local/lib/libmcrypt)  
  36. ;mcrypt.modes_dir=  
  37.   
  38. [dba]  
  39. ;dba.default_handler=  
  40.   
  41. [opcache]  
  42. ; Determines if Zend OPCache is enabled  
  43. opcache.enable=1  
  44.   
  45. ; Determines if Zend OPCache is enabled for the CLI version of PHP  
  46. opcache.enable_cli=1  
  47.   
  48. ; The OPcache shared memory storage size.  
  49. opcache.memory_consumption=528  
  50.   
  51. ; The amount of memory for interned strings in Mbytes.  
  52. opcache.interned_strings_buffer=8  
  53.   
  54. ; The maximum number of keys (scripts) in the OPcache hash table.  
  55. ; Only numbers between 200 and 100000 are allowed.  
  56. opcache.max_accelerated_files=10000  

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有M币奖励和额外收入!

模板下载吧 快速入门 使用memcache对wordpress优化,提速 https://www.mbxzb.com/blog/file/rumen/9974.html

从明天起,做一个幸福的人,喂马、劈柴、周游世界…

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

评论
暂无评论
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 威武 友军 顶贴
官方客服团队

为您解决烦忧 - 24小时在线 专业服务