how to Enable opcode cache PHP 8
#1 EDIT Php php ini
sudo nano /etc/php/7.4/fpm/php.ini
or Whatever the version
sudo nano /etc/php/7.3/fpm/php.ini
sudo nano /etc/php/8.3/fpm/php.ini
#1.1 Uncomment & Change value to 1
;opcache.enable=0
to
opcache.enable=1
or
#1.2 SImple way
just run below command
sudo phpenmod opcache
#2 Opcache configuration settings (Imp)
edit /etc/php/php.ini
or nano /etc/php/7.4/fpm/php.ini
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=200
opcache_revalidate_freq = 240
opcache.error_log= /var/log/nginx/opcahce_error.log
opcache.file_cache=/var/www/html/.opcache (otional or better to avoid)
sudo service php7.4-fpm restart
(512MB mycase 8GB RAM)
#2.1 checking php opcache module status
run below command in terminal
php -m
php -m | grep opcache
[Zend Modules]
Zend OPcache
enable : sudo phpenmod opcache
Howq clear purge opcache without restart php-fpm
PHP Running Method | How to Flush PHP Opcache? |
General Method | Create flush_cache.php in the docroot and browse it |
CGI or FastCGI Method | Cache is already flushing at every request |
CLI Method | Restart the PHP commands (CTRL + C) |
mod_php Method | Reload or restart your apache webserver |
PHP-FPM Method | Send a reload to your PHP-FPM daemon |
service php8.1-fpm restart
or
service php7.4-fpm restart to clear quick
or
reloading php fpm to flush opcache
service php-fpm reload
Flush_cache.php:
<?php
opcache_reset();
?>
Simply browse the flush_cache.php file, while you need to clear the Opcache. The file will call the ocache_reset() for the whole Opcache.
PHP CacheTool – Manage cache in the CLI
Using CacheTool you can clear the PHP opcache without reloading PHP-FPM.
docker only
https://gist.github.com/jdeathe/57287f5833a94a309d78996fba5168b2
Optional 2nd layer caching when shared memory is full ((optional)
1.Make a Directory
sudo mkdir /var/www/html/.opcache
2.Grant Permissions to Php & Nginx user
chown -R www-data:www-data /var/www/html/.opcache
3. Add the file path in php.ini
opcache.file_cache=/var/www/html/.opcache;
RAM: allocate memory based on your server RAM performance-versus-RAM trade-off.
opcache.memory_consumption=128
number of scripts that can be cached
opcache.max_accelerated_files=200
or
opcache.max_accelerated_files=0
unlimited
, revalidate frequency (based on how long PHP code changes) in WordPress may set for a long time
opcache_revalidate_freq = 240 //seconds
sudo service nginx restart
or
sudo service php7.4-fpm restart
how to disable opcache
nano /etc/php7.4/php.ini
if there is opcache.enable=1
changed to below
opcache.enable=0
sudo service php7.4-fpm restart
php -m | grep opache
how to enablle opcache
nano /etc/php7.4/php.ini
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=30
Opcache settings Explained straight Forward
Cache Expiry
opcache.revalidate_freq=0
disable cache expiry check until php-fpm restarts, Its not a good not a idea to Same Compiler code for every Dynamic Php request unlike fastcgi cache its dynamic its has to expiry otherwise we get errors. (ex wordpress admin dashboard).
TO enable Re valiting cache opcache.revalidate_freq=1.
opcache.validate_timestamps=0
when this enable above option works , 0 Means no validation, timestamp = realidating frequency. (set your time) 60mins seconds
opcache.max_accelerated_files=7963
Decide the Max amount php files to be cached.
you can check how much amount of php files in your directory available for executing this command
find /var/www/html/ -iname *.php|wc -l
Most frequently caching Files are better option.
opcache.memory_consumption=192
from your Server ex: 4GB,8Gb,16Gb
4gb, 512Mb for system, mysql innodb buffer pool equal to database 512MB. Fastcgi cache 200MB.
php-fpm fool manager (128*10 = 1GB)* Nginx Server 512MB.
Its a bad idea to dedicate memory without analyzing actual usage. you may frequently get memory hog (memory 100% messages)
(amount of shared memory from your server ram)
opcache.interned_strings_buffer=16 (RAM for strings to cache them as array)
chunks of memory you allocated
ex 1GB allocated then divide 1gb/16= to avoid memory leaks
opcache.fast_shutdown=1 (enables fast reset of cache)
How to decide opcache.max_accelerated_files?
; Only numbers between 200 and 1000000 are allowed.
The maximum number of keys (scripts) in the OPcache hash table.
zend_extension=opcache.so;
opcache.enable=1;
opcache.memory_consumption=32;
opcache.interned_strings_buffer=8;
opcache.max_accelerated_files=3000;
opcache.revalidate_freq=180;
opcache.fast_shutdown=0;
opcache.enable_cli=0;
opcache.revalidate_path=0;
opcache.validate_timestamps=1;
opcache.max_file_size=0;
opcache.file_cache=/kunden/homepages/mein-pfad/htdocs/.opcache;
opcache.file_cache_only=1;
How much amount of memory to allocate opcache?
Memory 1048
adjust to your need opcache.max_accelerated_files=10000 #
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0
; The OPcache shared memory storage size.
opcache.memory_consumption=200
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=16
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=2000
; The maximum percentage of “wasted” memory until a restart is scheduled.
opcache.max_wasted_percentage=5
5 means 5% wasted
memory that has been allocated to the service but is not in use. when it triggers its restarts.
; When this directive is enabled, the OPcache appends the current working
; directory to the script key, thus eliminating possible collisions between
; files with the same name (basename). Disabling the directive improves
; performance, but may break existing applications.
;opcache.use_cwd=1
; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
opcache.validate_timestamps=1
; How often (in seconds) to check file timestamps for changes to the shared
; memory storage allocation. (“1” means validate once per second, but only
; once per request. “0” means always validate)
opcache.revalidate_freq=240
default 1 and disabled
; Enables or disables file search in include_path optimization
;opcache.revalidate_path=0
; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
;opcache.save_comments=1
; If enabled, a fast shutdown sequence is used for the accelerated code
; Depending on the used Memory Manager this may cause some incompatibilities.
opcache.fast_shutdown=1
; Allow file existence override (file_exists, etc.) performance feature.
;opcache.enable_file_override=0
; A bitmask, where each bit enables or disables the appropriate OPcache
; passes
;opcache.optimization_level=0xffffffff
;opcache.inherited_hack=1
;opcache.dups_fix=0
; The location of the OPcache blacklist file (wildcards allowed).
; Each OPcache blacklist file is a text file that holds the names of files
; that should not be accelerated. The file format is to add each filename
; to a new line. The filename may be a full path or just a file prefix
; (i.e., /var/www/x blacklists all the files and directories in /var/www
; that start with ‘x’). Line starting with a ; are ignored (comments).
;opcache.blacklist_filename=
; Allows exclusion of large files from being cached. By default all files
; are cached.
;opcache.max_file_size=0
; Check the cache checksum each N requests.
; The default value of “0” means that the checks are disabled.
;opcache.consistency_checks=0
; How long to wait (in seconds) for a scheduled restart to begin if the cache
; is not being accessed.
;opcache.force_restart_timeout=180
; OPcache error_log file name. Empty string assumes “stderr”.
opcache.error_log=””
; All OPcache errors go to the Web server log.
; By default, only fatal errors (level 0) or errors (level 1) are logged.
; You can also enable warnings (level 2), info messages (level 3) or
; debug messages (level 4).
opcache.log_verbosity_level=2
; Preferred Shared Memory back-end. Leave empty and let the system decide.
;opcache.preferred_memory_model=
; Protect the shared memory from unexpected writing during script execution.
; Useful for internal debugging only.
;opcache.protect_memory=0
opcache.file_update_protection = 2
Default 2
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 setting it
to “0”.
; Allows calling OPcache API functions only from PHP scripts which path is
; started from specified string. The default “” means no restriction
;opcache.restrict_api=
; Mapping base of shared memory segments (for Windows only). All the PHP
; processes have to map shared memory into the same address space. This
; directive allows to manually fix the “Unable to reattach to base address”
; errors.
;opcache.mmap_base=
; 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=””
; Enables or disables opcode caching in shared memory.
;opcache.file_cache_only=0
; Enables or disables checksum validation when script loaded from file cache.
;opcache.file_cache_consistency_checks=1
; 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.
;opcache.file_cache_fallback=1
; Enables or disables copying of PHP code (text segment) into HUGE PAGES.
; This should improve performance, but requires appropriate OS configuration.
;opcache.huge_code_pages=1
; Validate cached file permissions.
;opcache.validate_permission=0
; Prevent name collisions in chroot environment.
;opcache.validate_root=0
Specifies a PHP script that is going to be compiled and executed at server
; start-up.
; http://php.net/opcache.preload
opcache.preload=
http://php.net/manual/en/opcache.configuration.php
don’t restart php-fpm
restarting nginx server doesn’t take effect on php-fpm . so we hae to reload/ restart the php server in nginx.
service php7.0-fpm reload
service php7.0-fpm restart
start /stop/ status you can use these along with above two commands.
#COmment if any doubts related to opcache Configuration.
Trobuleshooting
opcache and WordPress issues
How to monitor Opcache Usage?
Php wordpress plugins
https://wordpress.org/plugins/flush-opcache/
Opcache vs APCu vs redis vs Memcached
Opcache
storing precompiled script bytecode in shared memory,
APC/APCu
robust framework caching and optimizing PHP intermediate code
Memcached
high-performance, distributed memory object caching system
redis
in-memory data structure store, used as a database, cache and message broker.
Also Read Php -fpm configuration Based on Your Server, and Enabling Fastcgi Cache location in memory or disk.