Konfiguracja wykonawcza

Na działanie tych funkcji wpływają ustawienia zawarte w pliku php.ini.

OPcache configuration options
Nazwa Domyślnie Możliwość zmian Rejestr zmian
opcache.enable "1" PHP_INI_ALL  
opcache.enable_cli "0" PHP_INI_SYSTEM Between PHP 7.1.2 and 7.1.6 inclusive, the default was "1"
opcache.memory_consumption "128" PHP_INI_SYSTEM Before PHP 7.0.0 the default was "64"
opcache.interned_strings_buffer "8" PHP_INI_SYSTEM Before PHP 7.0.0 the default was "4"
opcache.max_accelerated_files "10000" PHP_INI_SYSTEM Before PHP 7.0.0 the default was "2000"
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 Removed in PHP 7.0.0.
opcache.fast_shutdown "0" PHP_INI_SYSTEM Removed in PHP 7.2.0
opcache.enable_file_override "0" PHP_INI_SYSTEM  
opcache.optimization_level "0x7FFEBFFF" PHP_INI_SYSTEM Changed from 0x7FFFBFFF in PHP 7.3.0, and from 0xFFFFFFFF in PHP 5.6.18
opcache.inherited_hack "1" PHP_INI_SYSTEM Removed in PHP 7.3.0
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  
opcache.file_update_protection "2" PHP_INI_ALL  
opcache.huge_code_pages "0" PHP_INI_SYSTEM  
opcache.lockfile_path "/tmp" PHP_INI_SYSTEM  
opcache.opt_debug_level "0" PHP_INI_SYSTEM Available as of PHP 7.1.0
opcache.file_cache NULL PHP_INI_SYSTEM Available as of PHP 7.0.0
opcache.file_cache_only "0" PHP_INI_SYSTEM Available as of PHP 7.0.0
opcache.file_cache_consistency_checks "1" PHP_INI_SYSTEM Available as of PHP 7.0.0
opcache.file_cache_fallback "1" PHP_INI_SYSTEM Windows only. Available as of PHP 7.0.0
opcache.validate_permission "0" PHP_INI_SYSTEM Available as of PHP 7.0.14
opcache.validate_root "0" PHP_INI_SYSTEM Available as of PHP 7.0.14
opcache.preload "" PHP_INI_SYSTEM Available as of PHP 7.4.0
opcache.preload_user "" PHP_INI_SYSTEM Available as of PHP 7.4.0
opcache.cache_id "1" PHP_INI_SYSTEM Windows only. Available as of PHP 7.4.0
Szczegóły i definicje dotyczące działania PHP_INI_* znajdują się w rozdziale Where a configuration setting may be set.

Oto krótkie wyjaśnienie dyrektyw konfiguracji.

opcache.enable boolean

Enables the opcode cache. When disabled, code is not optimised or cached. The setting opcache.enable can not be enabled at runtime through ini_set(), it can only be disabled. Trying to enable it at in a script will generate a warning.

opcache.enable_cli boolean

Enables the opcode cache for the CLI version of PHP.

opcache.memory_consumption integer

The size of the shared memory storage used by OPcache, in megabytes.

opcache.interned_strings_buffer integer

The amount of memory used to store interned strings, in megabytes. This configuration directive is ignored in PHP < 5.3.0.

opcache.max_accelerated_files integer

The maximum number of keys (and therefore scripts) in the OPcache hash table. The actual value used will be the first number in the set of prime numbers { 223, 463, 983, 1979, 3907, 7963, 16229, 32531, 65407, 130987 } that is greater than or equal to the configured value. The minimum value is 200. The maximum value is 100000 in PHP < 5.5.6, and 1000000 in later versions.

opcache.max_wasted_percentage integer

The maximum percentage of wasted memory that is allowed before a restart is scheduled.

opcache.use_cwd boolean

If enabled, OPcache appends the current working directory to the script key, thereby eliminating possible collisions between files with the same base name. Disabling this directive improves performance, but may break existing applications.

opcache.validate_timestamps boolean

If enabled, OPcache will check for updated scripts every opcache.revalidate_freq seconds. When this directive is disabled, you must reset OPcache manually via opcache_reset(), opcache_invalidate() or by restarting the Web server for changes to the filesystem to take effect.

opcache.revalidate_freq integer

How often to check script timestamps for updates, in seconds. 0 will result in OPcache checking for updates on every request.

This configuration directive is ignored if opcache.validate_timestamps is disabled.

opcache.revalidate_path boolean

If disabled, existing cached files using the same include_path will be reused. Thus, if a file with the same name is elsewhere in the include_path, it won't be found.

opcache.save_comments boolean

If disabled, all documentation comments will be discarded from the opcode cache to reduce the size of the optimised code. Disabling this configuration directive may break applications and frameworks that rely on comment parsing for annotations, including Doctrine, Zend Framework 2 and PHPUnit.

opcache.load_comments boolean

If disabled, documentation comments won't be loaded from the opcode cache even if they exist. This can be used with opcache.save_comments to only load comments for applications that require them.

opcache.fast_shutdown boolean

If enabled, a fast shutdown sequence is used that doesn't free each allocated block, but relies on the Zend Engine memory manager to deallocate the entire set of request variables en masse.

This directive has been removed in PHP 7.2.0. A variant of the fast shutdown sequence has been integrated into PHP and will be automatically used if possible.

opcache.enable_file_override boolean

When enabled, the opcode cache will be checked for whether a file has already been cached when file_exists(), is_file() and is_readable() are called. This may increase performance in applications that check the existence and readability of PHP scripts, but risks returning stale data if opcache.validate_timestamps is disabled.

opcache.optimization_level integer

A bitmask that controls which optimisation passes are executed.

opcache.inherited_hack boolean

In PHP < 5.3, OPcache stores the places where DECLARE_CLASS opcodes used inheritance; when the file is loaded, OPcache then tries to bind the inherited classes using the current environment. The problem is that while the DECLARE_CLASS opcode may not be needed for the current script, if the script requires that the opcode be defined, it may not run.

This configuration directive is ignored in PHP 5.3 and later.

opcache.dups_fix boolean

This hack should only be enabled to work around "Cannot redeclare class" errors.

opcache.blacklist_filename string

The location of the OPcache blacklist file. A blacklist file is a text file containing the names of files that should not be accelerated, one per line. Wildcards are allowed, and prefixes can also be provided. Lines starting with a semi-colon are ignored as comments.

A simple blacklist file might look as follows:

; Matches a specific file.
; A prefix that matches all files starting with x.
; A wildcard match.
opcache.max_file_size integer

The maximum file size that will be cached, in bytes. If this is 0, all files will be cached.

opcache.consistency_checks integer

If non-zero, OPcache will verify the cache checksum every N requests, where N is the value of this configuration directive. This should only be enabled when debugging, as it will impair performance.

opcache.force_restart_timeout integer

The length of time to wait for a scheduled restart to begin if the cache isn't active, in seconds. If the timeout is hit, then OPcache assumes that something is wrong and will kill the processes holding locks on the cache to permit a restart.

If opcache.log_verbosity_level is set to 2 or above, a warning will be recorded in the error log when this occurs.

opcache.error_log string

The error log for OPcache errors. An empty string is treated the same as stderr, and will result in logs being sent to standard error (which will be the Web server error log in most cases).

opcache.log_verbosity_level integer

The log verbosity level. By default, only fatal errors (level 0) and errors (level 1) are logged. Other levels available are warnings (level 2), information messages (level 3) and debug messages (level 4).

opcache.preferred_memory_model string

The preferred memory model for OPcache to use. If left empty, OPcache will choose the most appropriate model, which is the correct behaviour in virtually all cases.

Possible values include mmap, shm, posix and win32.

opcache.protect_memory boolean

Protects shared memory from unexpected writes while executing scripts. This is useful for internal debugging only.

opcache.mmap_base string

The base used for shared memory segments on Windows. All PHP processes have to map shared memory into the same address space. Using this directive allows "Unable to reattach to base address" errors to be fixed.

opcache.restrict_api string

Allows calling OPcache API functions only from PHP scripts which path is started from specified string. The default "" means no restriction.

opcache.file_update_protection string

Prevents caching files that are less than this number of seconds old. It protects from caching of incompletely updated files. In case all file updates on your site are atomic, you may increase performance by setting it to "0".

opcache.huge_code_pages string

Enables or disables copying of PHP code (text segment) into HUGE PAGES. This should improve performance, but requires appropriate OS configuration.

opcache.lockfile_path string

Absolute path used to store shared lockfiles (for *nix only)

opcache.opt_debug_level string

Produces opcode dumps for debugging different stages of optimizations. 0x10000 will output opcodes as the compiler produced them before any optimization occurs while 0x20000 will output optimized codes.

opcache.file_cache string

Enables and sets the second level cache directory. It should improve performance when SHM memory is full, at server restart or SHM reset. The default "" disables file based caching.

opcache.file_cache_only boolean

Enables or disables opcode caching in shared memory.

opcache.file_cache_consistency_checks boolean

Enables or disables checksum validation when script loaded from file cache.

opcache.file_cache_fallback boolean

Implies opcache.file_cache_only=1 for a certain process that failed to reattach to the shared memory (for Windows only). Explicitly enabled file cache is required.


Disabling this configuration option may prevent processes to start, and is therefore discouraged.

opcache.validate_permission boolean

Validates the cached file permissions against the current user.

opcache.validate_root boolean

Prevents name collisions in chroot'ed environments. This should be enabled in all chroot'ed environments to prevent access to files outside the chroot.

opcache.preload string

Specifies a PHP script that is going to be compiled and executed at server start-up, and which may preload other files, either by includeing them or by using the opcache_compile_file() function. All the entities (e.g. functions and classes) defined in these files will be available to requests out of the box, until the server is shut down.

opcache.preload_user string

Preloading code as root is not allowed for security reasons. This directive facilitates to let the preloading to be run as another user.

opcache.cache_id string

On Windows, all processes running the same PHP SAPI under the same user account having the same cache ID share a single OPcache instance. The value of the cache ID can be freely chosen.

add a note add a note

User Contributed Notes 6 notes

wessos at example dot org
1 year ago
The optimization levels as of php 7.3 are the following:

#define ZEND_OPTIMIZER_PASS_1        (1<<0)   /* CSE, STRING construction     */
#define ZEND_OPTIMIZER_PASS_2        (1<<1)   /* Constant conversion and jumps */
#define ZEND_OPTIMIZER_PASS_3        (1<<2)   /* ++, +=, series of jumps      */
#define ZEND_OPTIMIZER_PASS_4        (1<<3)   /* INIT_FCALL_BY_NAME -> DO_FCALL */
#define ZEND_OPTIMIZER_PASS_5        (1<<4)   /* CFG based optimization       */
#define ZEND_OPTIMIZER_PASS_6        (1<<5)   /* DFA based optimization       */
#define ZEND_OPTIMIZER_PASS_7        (1<<6)   /* CALL GRAPH optimization      */
#define ZEND_OPTIMIZER_PASS_8        (1<<7)   /* SCCP (constant propagation)  */
#define ZEND_OPTIMIZER_PASS_9        (1<<8)   /* TMP VAR usage                */
#define ZEND_OPTIMIZER_PASS_10        (1<<9)   /* NOP removal                 */
#define ZEND_OPTIMIZER_PASS_11        (1<<10)  /* Merge equal constants       */
#define ZEND_OPTIMIZER_PASS_12        (1<<11)  /* Adjust used stack           */
#define ZEND_OPTIMIZER_PASS_13        (1<<12)  /* Remove unused variables     */
#define ZEND_OPTIMIZER_PASS_14        (1<<13)  /* DCE (dead code elimination) */
#define ZEND_OPTIMIZER_PASS_15        (1<<14)  /* (unsafe) Collect constants */
#define ZEND_OPTIMIZER_PASS_16        (1<<15)  /* Inline functions */

Source: https://lxr.room11.org/xref/php-src%40master/ext/opcache/Optimizer/zend_optimizer.h
damien at overeem dot org
3 years ago
When using PHP on a windows platform and enabling opcache, you might run into occasional 500 errors. These will appear to show up entirely random.

When this happens, your windows Event log (Windows Logs/Application) will show (probably multiple)  entries from Zend OPcache with Event ID 487. Further information will state the following error message: "Base address marks unusable memory region".

This issue can be resolved by adding the following to your php.ini:

    opcache.mmap_base = 0x20000000

Unfortunately I do not know the significance of the value "0x20000000". I can only tell you that this value works to solve the problem (Tried and tested)
bdurand at ensemblegroup dot net
3 years ago
It would appear as though the [opcache.enable] setting is indeed NOT PHP_INI_ALL.
For changing it within user.ini yields no effect when disabled at global level. user.ini is ignored for that setting.
tizian dot schmidlin at gmail dot com
1 year ago
It should be noted that according to the original RFC (https://wiki.php.net/rfc/preload) `opcache.preload` caches preloaded files *forever* for all instances of the underlying PHP process.

That means, that hosting multiple websites on the same server might result in some unexpected behaviour.

Concrete example:
- you have a Symfony 3.2 App (which might be an endpoint of some type) and a Symfony 3.4 App (which might be your main application)
- both apps have a main Class called App that is in the same namespace (as it is usual, since the class name is unique to each project)
- depending on which app is loaded first, one or the other will work, since `opcache.preload` has no file based distinction of what class is used where and simply provides them to the user space

This is avoidable by simply not preloading user space classes or, if you work with FPM, by defining a pool for each app.

In order to optimize memory consumption, you might also use a common FPM Pool for all Symfony 3.4 Apps and preload the entire framework in there and simply not preload user space classes (which might be cached by opcache anyway but is slower, since it will be checked if the file has changed on every request).
paul dot lindgreen at msvu dot ca
2 years ago
Is opcache.memory_consumption limited by max_memory? If not what limitations are there, can you have too much?
5 years ago
opcache.max_wasted_percentage must have a value between 1 and 50. Otherwise it will automatically set the variable 5 %.
To Top