[Article rolling City reproduced please specify the original source: https://mo2g.com/view/137/]
Maybe a lot of friends in contact with PHP, more or less understand: PHP is a scripting language, an interpreted language, so it does not need to be compiled to run, and so on. This is not entirely true, but for beginners, it might be more appropriate to get a quick start and debug your code. But when you want to optimize your PHP-based Web site to support higher concurrent access, you need to know what opcodes is.
Maybe a lot of friends in contact with PHP, more or less understand: PHP is a scripting language, an interpreted language, so it does not need to be compiled to run, and so on.
This is not entirely true, but for beginners, it may be more appropriate to understand this, not only to get started quickly, but also to debug the code quite handy. But when you want to optimize your PHP-based Web site to support higher concurrent access, you need to know what opcodes is.
By default, PHP will generate the corresponding opcode for each request, such as code parsing, compiling, and finally executing through the Zend engine. :
Examples of reference laruence:
12345 |
<?php echo "Hello World" ; $a = 1 + 1; echo $a ; ?> |
PHP executes this code in the following 4 steps (to be exact, it should be PHP's language engine Zend)
1.Scanning (lexing), convert PHP code to language fragment (Tokens)
2.Parsing, convert tokens to simple and meaningful expressions
3.Compilation, compile the expression into Opocdes
4.Execution, execute opcodes sequentially, one at a time, thus realizing the function of PHP script.
The role of the opcodes cache is to save the compiled operation code for a period of time, so that the Zend engine does not have to parse and compile in subsequent requests. :
When you're struggling to find a way to speed up your PHP Web response, I'm sure Cache opcodes is one of the methods you're looking for, and it's the easiest.
This article describes how to use Opcache (formerly known as Zend Optimizer Plus, or o+) to accelerate PHP programs. O+ is a PHP performance improvement tool developed by Zend, and unlike other opcodes caching tools, o+ does a lot of opcodes optimizations in addition to cache opcodes.
Starting with PHP5.5, O+ has been released along with PHP's source code. More than 5.2 of the PHP version can also be compiled to load the o+. Http://pecl.php.net/zendopcache
Under the same configuration, using Apache benchmark for stress testing, the o+ optimization results are as follows:
Ab-c50-t2 10.0.0.59/ab_test.php
Open o+:requests per second:168.77 [#/sec] (mean)
Close o+:requests per second:157.44 [#/sec] (mean)
Increased by 11.33
The ab_test.php code is as follows:
123456789 |
<?php
$x
= 10000;
$arr
=
array
();
while
(
$x
) {
$arr
[
$x
] =
$x
--;
}
foreach
(
$arr
as $row
) {
echo
"$row<br>"
;
}
|
Next, I will post the installation process, because I use Yum to build the environment, may not apply to your situation, for reference only.
1) Download, compile, install
1234567 |
wget http: //pecl .php.net /get/zendopcache-7 .0.3.tgz tar zxvf zendopcache-7.0.3.tgz cd zendopcache-7.0.3/ phpize . /configure make make install |
2) Edit php.ini, add configuration at the bottom of the file
12345678910111213141516 |
vi
/etc/php
.ini
zend_extension=
/usr/lib64/php/modules/opcache
.so
;命令行模式是否开启缓存功能,我建议不开启
opcache.enable_cli=0
;共享内存大小, 这个根据你们的需求可调
opcache.memory_consumption=128
;interned string的内存大小, 也可调
opcache.interned_strings_buffer=8
;最大缓存的文件数目
opcache.max_accelerated_files=4000
;60s检查一次文件更新
opcache.revalidate_freq=60
;打开快速关闭, 打开这个在PHP Request Shutdown的时候,回收内存的速度会提高
opcache.fast_shutdown=1
;不保存文件/函数的注释
opcache.save_comments=0
|
3) Let php-fpm reload the configuration file
1 |
/ect/init .d /php-fpm reload |
4) Run the following code
If you can find a similar text hint, PHP has successfully loaded the Opcache. Try it now, is the PHP website responding faster?
With Zend Opcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
Use Opcache to speed up your website (reprint)