一,简介:
PHP每次执行都会先去编译,为了避免重复编译造成的资源浪费,PHP5.5以后提供了opcache的module,把PHP编译好的 bytecode 放到内存,达到加速的效果。
二,下面介绍如何安装opcache,其实跟其他module一样安装即可
1,进入opache扩展目录,进行编译安装
# cd /mnt/tool/php-5.6.8/ext/opcache/ # /usr/local/php/bin/phpize # ./configure --with-php-config=/usr/local/php/bin/php-config # make # make install
2,修改php.ini,注意opache是zend_extension
zend_extension=opcache.so
3,重启php-fpm
service php-fpm restart
4,查看安装扩展
# php -m [PHP Modules] Core ctype date ········· [Zend Modules] Zend OPcache
或者在phpinfo()也可查看
三,php默认是关闭opcache的,需要在php.ini开启和配置相关参数。
以下为常用配置
[opcache] ; Determines if Zend OPCache is enabled ; 开启 opcache opcache.enable=1 ; Determines if Zend OPCache is enabled for the CLI version of PHP ; 客户端也可用 opcache.enable_cli=1 ; The OPcache shared memory storage size. ; 可用内存,总共能够存储多少预编译的代码,单位MB, opcache.memory_consumption=64 ; The maximum number of keys (scripts) in the OPcache hash table. ; Only numbers between 200 and 100000 are allowed. ; 最大缓存的文件数目 opcache.max_accelerated_files=2000 ; How often (in seconds) to check file timestamps for changes to the shared ; memory storage allocation. ("1" means validate once per second, but only ; once per request. "0" means always validate) ; 2秒检查一次文件更新,注意:0是一直检查不是关闭 ; 如果是生产环境稳定下来,可以调大,比如300秒 opcache.revalidate_freq=300
Leave a Reply