Before we start Opcache, let's introduce the compilation and explanation:
The compiler compiles each statement of the source program into a machine language and saves it as a binary file so that the runtime computer can run the program directly in machine language, and the interpreter is executed only when the program is executed, one by one, to the machine language, So the running speed is not as fast as the compiled program.
In the implementation of the interpreted language, the translator does not produce the target machine code, but produces an easy-to-execute intermediate code, which differs from the machine code, where the interpretation of the intermediate code is supported by the software and cannot be directly used, and the software interpreter usually results in less efficient execution. A program written in an interpreted language is executed by another interpreter that can understand the intermediate code. Unlike the compiler, the task of the interpreter is to interpret the source program's statements as executable machine instructions, without having to translate the source program into the target code before executing it. For an interpreted basic language, a specialized interpreter is required to interpret the execution.
In many cases we become compiled, but it is actually explained. For a compiled program, it is compiled and executed separately, first compiled into a binary executable file, and then executed at the next time.
For PHP, Python is an interpreted language, does not produce machine code, but the intermediate code is generated (the middle code is not directly executed, the middle of the interpreter can only be recognized, the middle code to rely on the parser to perform)
For example, PHP parser is zend,php use Zend Engine, intermediate code we also called as opcode (opcode) Basic program, each language only in the execution before being translated. This interpreted language is translated once per execution and thus inefficient.
Brother Bird said in the blog, improve PHP 7 performance of a few tips, the first is to open Opache, quoting the following text:
Remember to enable Zend Opcache because PHP7 is faster than PHP-5.6 enabled even if Opcache is not enabled,
So there was a time before the test that someone had not enabled Opcache.
Open Opcache method
PHP 5.5+ version above, you can use PHP's own Opcache turn on performance acceleration (the default is off), PHP5.5 after Opcache can be directly --enable-opcache
. The following versions of PHP 5.5 require the use of APC for caching, which is not explained here.
PHP5.5 need to use APC for caching before
1. Open php.ini File
2. Locate: [opcache]
, set to:
[Opcache]; switch on opcache.enable=1; Set the shared memory size in: mbopcache.memory_consumption=128 If enabled, the Opcache will check if the script is updated every opcache.revalidate_freq set number of seconds. If you disable this option, you must manually reset the Opcache using the Opcache_reset () or opcache_invalidate () function, or you can make the file system changes take effect by restarting the WEB server. Opcache.validate_timestamps=#提示: In the case of Opcache using a soft connection, there is a situation where opcache is not cleared. You can use the restart fastcgi to solve this problem.
3. Add opcache.so
Adding a opcache.so
primary role to the last line of PHP.ini is to refer to Opcache
4. Restart Nginx and PHP
Turn on Opcache to improve performance