Scripting in PHP accelerates extension opcache

Source: Internet
Author: User
Tags download zend fpm zend

Transfer from http://blog.jjonline.cn/linux/135.html

Today in Azure installed php5.5.4, found that the original php5.4, php5.3 Zend Guard Laoder and php5.2 Zend in optimizer can no longer use, has been very fond of eaccelerator also tragic, The error is directly indicated when compiling.

No way, look at the php5.5.4 installation directory, found in the PHP installation directory of the extension Library directory has a opcached.so; Eh, this file is knitting? Look at the name with cached, is it a cache system? Google down, sure enough, this is php5.5 default PHP script cache extension, compile php5.5 with parameter--enable-opcache can compile Opcache, just to enable it must be configured.

Introduction of installation, acceleration effect and configuration of Opcache in PHP

After testing, php5.4, php5.3 and even php5.2 can also use the Opcache module, the Reference Network test article (http://www.cnblogs.com/xiaocen/p/3709850.html) found: Zend Opcache (generally directly referred to as Opcache) accelerated efficiency is higher than xcache, nature is higher than a year of maintenance eaccelerator. In addition, my test found that after loading the eaccelerator acceleration extension on php5.4, the code snippet in PHP code that uses complex calls to the Preg_replace_callback function can cause a significant memory overflow (memories leak); preg_ The Replace function is no longer recommended in php5.5, but instead instead of preg_replace_callback, discarding eaccelerator is a wise choice.

Below the PHP5.5 version (5.5 and above only need to compile PHP when the--enable-opcache parameter to complete the following compilation process, the last configuration is enabled) PHP to install Opcache is also very simple, download Zend Opcache (http ://pecl.php.net/package/zendopcache), such as Download Zendopcache-7.0.3.tgz, then the compilation and installation is very simple, the code is as follows:

  1. Tar zxvf zendopcache-7.0. 3.tgz
  2. CD Zendopcache-7.0. 3
  3. Phpize
  4. #若环境变量未加入phpize的路径
  5. For #则执行phpize命令时需要加上路径 Add environment variable please refer to http://blog.jjonline.cn/linux/165.html
  6. #如你的php安装在/usr/local/php, you should enter it here
  7. #/usr/local/php/bin/phpize
  8. ./Configure
  9. #同理, the bin directory in the PHP installation directory and the Sbin directory are not added to the environment variables, you need to add parameters
  10. #--with-php-config=/usr/local/php/bin/php-config #路径依据具体路径
  11. Make && make install
  12. #make Install will prompt the location of the compiled opcache.so file, in the php.ini plus the relevant configuration section can be

Introduction to the Opcache on the network: A new generation of PHP accelerator, developed by Zend Company, the principle of implementation is similar to XCache, is to buffer the PHP data into memory to avoid the duplication of the compilation process, can directly use the buffer compiled code to improve speed, Reduce server load, but performance is more superior than XCache, see below test result diagram. The mechanism of the simple point is that the PHP script in the virtual machine (which is called the final PHP machine code execution engine for the virtual machine) in the machine code or the corresponding server can directly run the code to cache, until the next time the user requests the script to skip the PHP script code into the process of machine code, So as to speed up PHP operation, caching effect.

How to configure Enable Opcache?

  1. Zend_extension = "__dir__opcached. So"
  2. Opcache. Force_restart_timeout=3600
  3. Opcache. Memory_consumption=1024x768
  4. Opcache. Optimization_level=1
  5. Opcache. Interned_strings_buffer=8
  6. Opcache. Max_accelerated_files=4096
  7. Opcache. Revalidate_freq=to detect php file change frequency unit seconds How many seconds to detect the php file changes
  8. Opcache. Fast_shutdown=1
  9. Opcache. Enable=1
  10. Opcache. Enable_cli=1
  11. The location of the #配置中的__Dir__请更换成opcached. So files is generally found in the directory under lib/extension under the php file directory

The Opcache cache code block is available in memory, depending on configuration parameters; no more introductions.

Opcached.so is loaded, all scripts execute with Phpinfo to see the cache hit rate, as in the cache Hits is 12, and the cache misses is 1

So how does this cache be cleaned up? Still through configuration file configuration, the specific item is opcache.force_restart_timeout, by specifying the time, at what time the content of the cache expires.

See the key words in this article, someone is asking how to close the Opcache, in fact, it is very simple, the configuration file will be related to the introduction of Opcahe extension of the configuration items commented out (PHP configuration file comments can use the English semicolon, that is, the configuration of the relevant opcache in the beginning of the line to add a semicolon, Then save, restart Apache or PHP-FPM).

Ii. some cases after the opening of Opcache

Typically, the effect is not immediately visible due to code changes caused by Opcache's caching acceleration mechanism. PHP code is converted into an executable "machine code" after a certain amount of cache time to check whether the original PHP files have changed, depending on the configuration item Opcache.revalidate_freq set the number of seconds the situation depends on This causes the code in the PHP file to be updated in some cases, but does not see the effect after it is executed, because the Opcache check for changes in PHP files has not been completed.

This problem is not a bug, but depending on the production environment to set the Opcache.revalidate_freq value, if the PHP code is rarely changed, it is recommended to set a larger value, can reduce the OPCAHCE due to check the PHP file changes in the additional costs, such as 7200

So how can you quickly reduce the problem with Opcache caching after changing PHP files? The simplest way is to restart the next php-fpm (nginx fast-cgi mode) or Apache (Apache-handler mode) after updating the code. Of course, you can also write a PHP script yourself, call the Opcache_reset () function to reset all Opcache cache bytecode, or use Opcache_invalidate (phpfiledir,true) to reset the cached bytecode for the specified PHP file.

Reprint please indicate this article title and link: "PHP script accelerated extension Opcache"

Scripting in PHP accelerates extension opcache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.