php性能优化--opcache

一、OPcache是什么?

OPcache通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销。

PHP 5.5.0 及后续版本中已经绑定了 OPcache 扩展。 对于 PHP 5.2,5.3 和 5.4 版本可以使用 PECL扩展中的 OPcache 库。
 
二、OPcache如何安装?
我的php版本:5.6.32 (cli) 
因为在5.5版本后php会内置opcache,我在编译安装的时候也安装了此项,因此只需要打开此功能和配置参数即可。
OPcache 只能编译为共享扩展。 如果你使用 --disable-all 参数 禁用了默认扩展的构建, 那么必须使用 --enable-opcache 选项来开启 OPcache。
编译之后,就可以使用 zend_extension 指令来将 OPcache 扩展加载到 PHP 中。在非 Windows 平台使用zend_extension=/full/path/to/opcache.so, Windows 平台使用 zend_extension=C:\path\to\php_opcache.dll
 
vi php.ini (可在php.ini底部添加)
;add opcache 
zend_extension= opcache.so         #加载opcache模块
opcach.enable_cli=1             #开启opcachecli功能
opcache.memory_consumption=128      #设置的共享缓存空间,我这里设置的128Mb   单位:Mb
opcache.max_accelerated_files=4000    #最大缓存的文件数目
opcache.revalidate_freq=60          #定期检查文件的修改时间,我这设置为60s  单位 秒
opcache.fast_shutdown=1            #打开快速关闭,打开此项php request Shutdown的时候速度会提高  

编辑完后重启php-fpm  ,使php.ini配置生效

三、检查OPcache是否生效 

查看phpinfo输出信息是否已经加载成功。

[root@localhost#  php -m |grep 'Zend OPcache'
Zend OPcache
Zend OPcache
[root@localhost]# 

 

四、优化效果

通过监控发现,load负载有明显下降,业务一切正常~

 

 

posted @ 2018-01-02 16:17  Quan7  阅读(3774)  评论(0编辑  收藏  举报