Turn [PHP]-performance acceleration-turn on Opcache

Source: Internet
Author: User
Tags apc fpm php memcached phpinfo set time zend zend framework saltstack

Original address: [PHP]-performance acceleration-turn on Opcache

PHP7 has been released as the largest version upgrade in PHP10 years, the biggest performance upgrade, PHP7 in the multi-release test showed a significant performance improvement

First, open the Opcache

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 computer can run the program directly in machine language at a very fast speed.
And the interpreter is only in the execution of the program, only one interpretation of the machine language to the computer to execute, so the speed is not as fast as the compiled program to run.

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 call as opcode (opcode)

Basic programs, each language is translated only when it is executed. This interpreted language is translated once per execution and thus inefficient.

1. Edit: Use the editing software (EDIT.EXE or Notepad) to form the source program (. ASM), such as: lx.asm;
2. Assembly: Compile the source program with assembler (MASM.EXE) to form the target file (. OBJ), in the following format: MASM LX. ASM;
3. Connection: Use the connection program (LINK.EXE) to connect the target program to form an executable file (. EXE) in the following format: LINK LX. OBJ;
4. Execute: If the result is displayed on the screen, execute the executable file directly.
5. Debug: Debug the executable file with the Debug program (DEBUG.EXE), the format is as follows: Debug LX. Exe

Pick From: http://www.cnblogs.com/bluestorm/archive/2012/12/09/2810167.html

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 . For PHP versions below 5.5, you need to use APC for caching, which is not explained here and can be viewed in the PHP article on our path to optimization

PHP5.5 need to use APC for caching before

PHP Opcache Chinese Manual

1. Open php.ini File
I am using PHP7, about PHP7 installation can view my another article
http://www.abcdocker.com/abcdocker/2144
The path is installed at compile time.

2. Locate: [opcache] , set to:

[opcache]; 开关打开opcache.enable=1; 设置共享内存大小, 单位为:Mbopcache.memory_consumption=128;如果启用,那么 OPcache 会每隔 opcache.revalidate_freq 设定的秒数 检查脚本是否更新。 如果禁用此选项,你必须使用 opcache_reset() 或者 opcache_invalidate() 函数来手动重置 OPcache,也可以 通过重启 Web 服务器来使文件系统更改生效。opcache.validate_timestamps=60#提示:在opcache使用软连接的情况下,会存在opcache没有被清除的情况.可以使用重启fastcgi来解决这个问题.
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

3. Add opcache.so
Adding a opcache.so primary role to the last line of PHP.ini is to refer to Opcache

[root@abcdocker ~]# tail /etc/php.ini zend_extension="opcache.so"
    • 1
    • 2

4. Restart Nginx and PHP

5. Testing
After the configuration is complete, you can query the Opcache using the following code:

<?php    phpinfo();?>
    • 1
    • 2
    • 3

Note: We need to configure the test file in the Nginx directory phpinfo

访问页面得到如下界面:http://www.abcdocker.com/phpinfo.php

We can view them in addition to the phpinfo commands. php-fpm -m

[root@abcdocker ~]# /usr/local/php/sbin/php-fpm -m[PHP Modules]....[Zend Modules]Zend OPcache
    • 1
    • 2
    • 3
    • 4
    • 5

The following are the configuration instructions for Opcache:

Opcache configuration options

name default Modifiable Range
Opcache.enable "1" Php_ini_all
Opcache.enable_cli "0" Php_ini_system
Opcache.memory_consumption "64" Php_ini_system
Opcache.interned_strings_buffer "4" Php_ini_system
Opcache.max_accelerated_files "2000" Php_ini_system
Opcache.max_wasted_percentage "5" Php_ini_system
Opcache.use_cwd "1" Php_ini_system
Opcache.validate_timestamps "1" Php_ini_all
Opcache.revalidate_freq "2" Php_ini_all
Opcache.revalidate_path "0" Php_ini_all
Opcache.save_comments "1" Php_ini_system
Opcache.load_comments "1" Php_ini_all
Opcache.fast_shutdown "0" Php_ini_system
Opcache.enable_file_override "0" Php_ini_system
Opcache.optimization_level "0xFFFFFFFF" Php_ini_system
Opcache.inherited_hack "1" Php_ini_system
Opcache.dups_fix "0" Php_ini_all
Opcache.blacklist_filename “” Php_ini_system
Opcache.max_file_size "0" Php_ini_system
Opcache.consistency_checks "0" Php_ini_all
Opcache.force_restart_timeout "180" Php_ini_system
Opcache.error_log “” Php_ini_system
Opcache.log_verbosity_level "1" Php_ini_system
Opcache.preferred_memory_model “” Php_ini_system
Opcache.protect_memory "0" Php_ini_system
Opcache.mmap_base Null Php_ini_system
Opcache.restrict_api “” Php_ini_system

Reprint: http://php.net/manual/zh/opcache.configuration.php

Configuration can be set range

These patterns determine when and where a PHP instruction can be set. Each instruction in the manual has its own mode. For example, some directives can be set in PHP scripts with Ini_set (), while others are only available in php.ini or httpd.conf. For example, the output_buffering directive belongs to Php_ini_perdir, so it cannot be set with Ini_set (). But the display_errors directive is belonging to Php_ini_all and thus can be set anywhere, including Ini_set ().

Definition mode meaning of php_ini_* mode

Mode meaning
Php_ini_user Can be set in user scripts (e.g. Ini_set ()) or Windows registry (from PHP 5.3) and. User.ini
Php_ini_perdir Can be set in php.ini,.htaccess or httpd.conf
Php_ini_system Can be set in php.ini or httpd.conf
Php_ini_all can be set anywhere
Opcache. Enable boolean enables opcode caching. If this option is disabled, the code is not optimized and cached. Using the Ini_set () function at run time can only disable Opcache. Enable setting, this setting cannot be enabled. If you try to enable this setting in the script, a warning is generated. Opcache. enable_cli Boolean only forThe CLI version of PHP enables opcode caching. It is usually used for testing and debugging. OpcacheThe shared memory size of the. Memory_consumption Integeropcache, in megabytes. OpcacheThe. Interned_strings_buffer integer is used to store the memory size of the temporary string, in megabytes. Php5.3This configuration directive is ignored by versions prior to. 0. OpcacheThe maximum number of script files that can be stored in the. max_accelerated_files integer opcache hash table. The real value is in the set of mass {223,463,983,1979,3907,7963,16229,32531,65407,The first prime number found in 130987} is larger than the set value. Set value range The minimum value is200, maximum value in PHP5.5.6 ago is100000,php5.5.6 and later Yes1000000. OpcacheThe. Max_wasted_percentage integer wastes the upper limit of memory, in percent. If this limit is reached, then the Opcache will generate a restart-renewal event. Opcache. USE_CWD Boolean If enabled, Opcache appends the script key to the hash table to the working directory of the modified script to avoid the problem of scripting conflicts of the same name. Disabling this option can improve performance, but may cause your app to crash. Opcache. validate_timestamps Boolean if enabled, then Opcache will be opcache every. Revalidate_freq sets the number of seconds to check if the script is updated. 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. OpcacheThe. Revalidate_freq integer checks whether the script timestamp has an updated period, in seconds. Set to0 causes Opcache to check for script updates for each request. If OpcacheIf the. Validate_timestamps configuration directive is set to disabled, this setting item will be ignored. Opcache. Revalidate_path Boolean If this option is disabled, cached files that already exist in the same include_path will be reused. Therefore, you cannot find a file with the same name that is not in the containing path. Opcache. save_comments Boolean If disabled, the comment content in the script file will not be included in the opcode cache file, which can effectively reduce the volume of the optimized file. Disabling this configuration directive may cause some applications or frameworks that rely on annotations or annotations to not work correctly, such as: Doctrine, Zend framework2 and PHPUnit. Opcache. load_comments Boolean If disabled, the comment content is not loaded even if the file contains comments. This option can be used with Opcache. Save_comments is used together to enable loading of annotation content on demand. Opcache. Fast_shutdown Boolean If enabled, a quick Stop renewal event is used. The so-called fast stop-and-resume event refers to the memory management module that relies on the Zend engine to release the memory of all request variables at once, rather than releasing each allocated block of memory in turn. Opcache.enable_file_override boolean if enabled, the function is called file_exists (), Is_file (), and is_readable () , the opcode cache is checked regardless of whether the file has been cached. This can improve performance if the app includes features that check the existence and readability of PHP scripts. However, if the Opcache.validate_timestamps option is disabled, there may be a risk of returning stale data. Opcache.optimization_level integer controls the bits mask for the optimization level. Opcache.inherited_hack Boolean version before PHP 5.3, Opcache Stores code that uses the Declare_class opcode to implement the inherited location. When the file is loaded, Opcache attempts to bind the inherited class using the current environment. Because the Declare_class opcode may not be required in the current script, it may not work if such a script requires that the corresponding opcode be defined. This configuration directive is ignored in PHP 5.3 and later versions. Opcache.dups_fix Boolean is used only as a solution for "non-redefined class" errors. Opcache.blacklist_filename string opcache blacklist file location. The blacklist file is a text file that contains a file name that is not pre-compiled for optimization, one file name per line. Filenames in the blacklist can use wildcards, or they can use a prefix. Lines beginning with a semicolon (
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
简单的黑名单文件可能如下所示:; 将特定文件加入到黑名单/var/www/broken.php; 以字符 x 文件打头的文件/var/www/x; 通配符匹配/var/www/*-broken.php
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
Opcache. max_file_size integer The maximum file size of the cache in bytes. Set to0 means that all files are cached. Opcache. consistency_checks Integer If non-A value of 0, Opcache will check the cache checksum every N times. N is the setting value for this configuration directive. Because this option has a significant impact on performance, use it in the debug environment. Opcache. force_restart_timeout integer If the cache is inactive, wait a few seconds after the restart is scheduled. If the set time is exceeded, the Opcache module will kill the process that holds the cache lock and reboot. If the option Opcache. Log_verbosity_level set to3 orAbove 3, an error message is logged in the log when a restart occurs. Opcache. error_log string Opcache The error log file for the module. If left blank, it is treated as stderr and the error log is sent to the standard error output (usually the WEB server's error log file). Opcache.log_verbosity_level the log level of the integer Opcache module. By default, only the logs of the fatal level (0) and the error level (1) are logged. Other levels available are: Warning (2), information (3) and debugging (4). Opcache.preferred_memory_model string Opcache preferred memory module. If left blank, Opcache will select the applicable module and, typically, automatically select to meet the requirements. Optional values include: Mmap,shm, POSIX, and Win32. Opcache.protect_memory Boolean protects shared memory to avoid unintended writes when script execution occurs. For internal debugging only. Opcache.mmap_base string The base address of the shared memory segment on the Windows platform. All PHP processes map shared memory to the same address space. Use this configuration directive to avoid "unable to reattach to base address" errors. Opcache.restrict_api string only allows the path to be called by the PHP script starting with the specified string Opcache API function. The default value is an empty string 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

Reprint: http://php.net/manual/zh/opcache.configuration.php

Ii. adding new modules to PHP

We can use this method when we want to add a new expansion module to PHP. Of course, the following method is saltstack to install the expansion module

Project one: Saltstack add php memcached module

Memcache-plugin:file.managed:- Name:/usr/local/src/memcache-2.2.7.tgz- Sourcesalt://php/files/memcache-2.2.7.tgz- User:root- Group:root- Mode755 Cmd.run:- NAME:CD/USR/LOCAL/SRC && tar zxf memcache-2.2.7.tgz && CD memcache-2.2.7&& /usr/local/php/bin/phpize &&/configure--enable-memcache-- With-php-config=/usr/local/php/bin /php-config && make&& make install- unless:test-f/usr/local/php/lib/php/extensions/*/memcache.so require:-< Span class= "Ruby" > file:memcache-plugin-  Cmd:php-install/usr/local/php/etc/php.ini:file.append:- text:-< span class= "Ruby" > extension=memcache.so       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

#温馨提示: In Saltstack, do not install the program directly copied the original, will cause ID conflictsmemcache-plugin

Step Explanation:

1. Download the memcached Package

2. Using the/usr/local/php/bin/phpize command to phpize command meaning: Phpize is used to extend the PHP extension module, through the phpize can build PHP plug-in module

3. Add a extension= module name in php.ini. So

4. Restart PHP to

Turn [PHP]-performance acceleration-turn on 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.