1. For many code files, especially those containing many include files (include or require). They take more time to parse and generate intermediate code.
2. Even if the PHP code file has not changed, the execution process will be executed strictly in accordance with the process. In other words, no matter whether your application program changes or not, you need to recompile and generate opcode every time you call it. (In fact, this is the reason why the compilation cache exists)
3. This process not only occurs in the main code file, but also for every include and require. (This can continue to be optimized)
What areas can be optimized?
1. Make mod_php fast-cgi to avoid loading this module every time. This module also needs to initialize the PHP interpretation environment every time.
2. Cache the opcode code of the php file, so as to avoid compiling it every time.
APC can be used to achieve point 2. The compile cache removes the parsing process during PHP execution, so it is very effective for applications containing a large amount of PHP code. Under normal circumstances, the speed can be increased by more than 2-3 times. For projects that contain a large number of include files, compilation caching can better demonstrate its advantages.
Note: include will not be cached by the compilation cache. For example, there are two files: main.php and tobeInclude.php. Main.php has the statement include tobeInclude.php’. It is assumed that the suffix of the intermediate code is .op (this is not the case). Then after adding the cache main.php=>main.op, tobeInclude.php=>tobeInclude.op. But when PHP executes main.php, she still needs to parse the include command in main.op and call the content of tobeInclude.op. The specific process is as follows.
…=>Execute main.op=>Execute tobeInclude.op=>…
Instead of simply executing main.op
So it is said that "too many include files will reduce program performance."
Specific configuration of APC.
Alternative PHP Cache (APC) is a free and public optimized code cache for PHP. It is used to provide a free, open and robust framework for caching and optimizing PHP intermediate code.
The official website of APC is http://pecl.php.net/package/apc
1. Installation
Install as PHP extension
phpize
./configure --enable-apc --enable-apc-mmap
make
make install
Generate .so, copy .so to the directory where PHP references modules, and modify permissions to 755
2. Configuration
apc.enabled boolean
apc.optimization optimization
Options can be changed in scripts
Detailed explanation of APC PHP.ini configuration options
[APC]
; Alternative PHP Cache is used to cache and optimize PHP intermediate code
apc.cache_by_default = On
;SYS
; Whether to enable buffering for all files by default.
; If set to Off and used with the apc.filters directive starting with a plus sign, files will only be cached if they match the filter.
apc.enable_cli = Off
;SYS
; Whether to enable the APC function for the CLI version, turn this command on only for testing and debugging purposes.
apc.enabled = On
; Whether to enable APC? If APC is statically compiled into PHP and you want to disable it, this is the only way.
apc.file_update_protection = 2
;SYS
; When you modify files on a running server, you should perform atomic operations.
; That is, first write into a temporary file, and then rename (mv) the file to the final name.
; Text editors and programs such as cp and tar do not operate in this way, resulting in the possibility of buffering incomplete files.
; The default value 2 means that when accessing a file, if the modification time is found to be less than 2 seconds from the access time, no buffering will be performed.
; The unlucky visitor may get incomplete content, but the bad effect is not magnified by caching.
; If you can ensure that all update operations are atomic operations, you can use 0 to turn off this feature.
; If your system updates slowly due to heavy IO operations, you may need to increase this value.
apc.filters =
;SYS
; A comma-separated list of POSIX extended regular expressions.
; If the source file name matches any of the patterns, the file will not be cached.
; Note that the file name used for matching is the file name passed to include/require, not the absolute path.
; If the first character of the regular expression is "+" it means that any file matching the expression will be cached,
; If the first character is "-" then any matches will not be cached. "-" is the default value and can be omitted.
apc.ttl = 0
;SYS
; The number of seconds a cache entry is allowed to stay in the buffer. 0 means never times out. The recommended value is 7200~36000.
; Setting to 0 means that the buffer may be filled with old cache entries, preventing new entries from being cached.
apc.user_ttl = 0
;SYS
; Similar to apc.ttl, but for each user, the recommended value is 7200~36000.
; Setting to 0 means that the buffer may be filled with old cache entries, preventing new entries from being cached.
apc.gc_ttl = 3600
;SYS
; The number of seconds the cache entry can exist in the garbage collection table.
; This value provides a safety measure even if a server process crashes while executing a cached source file,
; And the source file has been modified, and the memory allocated for the old version will not be reclaimed until this TTL value is reached.
; Set to zero to disable this feature.
apc.include_once_override = Off
;SYS
; Please keep it Off, otherwise it may cause unexpected results.
apc.max_file_size = 1M
;SYS
; Disables files larger than this size from being cached.
apc.mmap_file_mask =
;SYS
; If MMAP support is compiled for APC using –enable-mmap (enabled by default),
; The value here is the mktemp-style file mask passed to the mmap module (the recommended value is "/tmp/apc.XXXXXX").
; This mask is used to determine whether the memory mapped area should be file-backed or shared memory backed.
; For direct file-backed memory mapping, set it to "/tmp/apc.XXXXXX" (exactly 6 X's).
; To use POSIX-style shm_open/mmap, you need to set it to "/apc.shm.XXXXXX".
; You can also set it to "/dev/zero" to use the kernel's "/dev/zero" interface for anonymously mapped memory.
; Not defining this directive means forcing the use of anonymous mapping.
apc.num_files_hint = 1000
;SYS
; The approximate number of different source files that may be included or requested on the web server (recommended value is 1024~4096).
; If you are not sure, set to 0; This setting is mainly used for sites with thousands of source files.
apc.optimization = 0
; Optimization level (recommended value is 0).
; A positive integer value indicates that the optimizer is enabled, and higher values use more aggressive optimization.
; Higher values may have a very limited speed increase, but are currently experimental.
apc.report_autofilter = Off
;SYS
; Whether to record all scripts that are automatically not cached due to early/late binding.
apc.shm_segments = 1
;SYS
; The number of shared memory blocks allocated for the compiler buffer (recommended value is 1).
; If APC runs out of shared memory and the apc.shm_size directive has been set to the maximum allowed by the system,
; You can try increasing this value.
apc.shm_size = 30
;SYS
; The size of each shared memory block (in MB, the recommended value is 128~256).
; Some systems (including most BSD variants) have a very small default shared memory block size.
apc.slam_defense = 0
;SYS (It is against the use of this command, it is recommended to use the apc.write_lock command)
; On a very busy server, whether starting a service or modifying a file,
; may cause a race condition due to multiple processes trying to cache a file at the same time.
; This directive is used to set the percentage of the process that skips the caching step when processing uncached files.
; For example, setting it to 75 means that there is a 75% probability of not caching when an uncached file is encountered, thereby reducing the chance of collision.
; It is encouraged to set this to 0 to disable this feature.
apc.stat = On
;SYS
; Whether to enable script update checking.
; Be very careful when changing this command value.
; The default value On means that APC checks whether the script has been updated every time it is requested,
; If updated, the compiled content will be automatically recompiled and cached. However, doing so has a negative impact on performance.
; If set to Off, it means no checking will be performed, thus greatly improving performance.
; But in order for the updated content to take effect, you must restart the web server.
; This directive is also valid for include/require files. But it should be noted that
; If you use relative paths, APC must check to locate the file every include/require.
; Using absolute paths can skip the check, so you are encouraged to use absolute paths for include/require operations.
apc.user_entries_hint = 100
;SYS
; Similar to the num_files_hint directive, but for each different user.
; Set to 0 if you are not sure.
apc.write_lock = On
;SYS
; Whether to enable write lock.
; On a very busy server, whether starting a service or modifying a file,
; may cause a race condition due to multiple processes trying to cache a file at the same time.
; Enabling this directive can avoid race conditions.
apc.rfc1867 = Off
;SYS
; After turning on this command, APC will automatically create a user cache entry for upload_ (that is, the APC_UPLOAD_PROGRESS field value) for each uploaded file that contains the APC_UPLOAD_PROGRESS field just before the file field.
3. php function