启用OPcache后应用更新仅重启PHP-FPM生效,cachetool无效求助
OPcache重置无效,仅重启PHP-FPM生效的问题分析与解决
问题详情
- 已在php.ini中启用OPcache,应用代码更新后,只有重启PHP-FPM才能让更新生效
- 使用cachetool工具执行缓存重置命令后,代码更新仍不生效:
sudo php /usr/bin/cachetool.phar opcache:reset sudo php /usr/bin/cachetool.phar opcache:reset --fcgi=/run/php/php8.2-fpm.sock sudo php /usr/bin/cachetool.phar apcu:cache:clear
- 仅执行以下命令重启PHP-FPM后,更新才生效:
service php8.2-fpm restart
当前OPcache配置
sudo php /usr/bin/cachetool.phar opcache:configuration Zend OPcache 8.2.17 +---------------------------------------+----------------------------------------------+ | Directive | Value | +---------------------------------------+----------------------------------------------+ | opcache.enable | true | | opcache.enable_cli | false | | opcache.use_cwd | true | | opcache.validate_timestamps | false | | opcache.validate_permission | false | | opcache.validate_root | false | | opcache.dups_fix | false | | opcache.revalidate_path | true | | opcache.log_verbosity_level | 1 | | opcache.memory_consumption | 268435456 | | opcache.interned_strings_buffer | 8 | | opcache.max_accelerated_files | 20000 | | opcache.max_wasted_percentage | 0.05 | | opcache.consistency_checks | 0 | | opcache.force_restart_timeout | 180 | | opcache.revalidate_freq | 2 | | opcache.preferred_memory_model | '' | | opcache.blacklist_filename | '' | | opcache.max_file_size | 0 | | opcache.error_log | '' | | opcache.protect_memory | false | | opcache.save_comments | true | | opcache.record_warnings | false | | opcache.enable_file_override | false | | opcache.optimization_level | 2147401727 | | opcache.lockfile_path | '/tmp' | | opcache.file_cache | '' | | opcache.file_cache_only | false | | opcache.file_cache_consistency_checks | true | | opcache.file_update_protection | 2 | | opcache.opt_debug_level | 0 | | opcache.restrict_api | '' | | opcache.huge_code_pages | false | | opcache.preload | '/var/www/hostname/prod/config/preload.php' | | opcache.preload_user | 'www-data' | | opcache.jit | '' | | opcache.jit_buffer_size | 0 | | opcache.jit_debug | 0 | | opcache.jit_bisect_limit | 0 | | opcache.jit_blacklist_root_trace | 16 | | opcache.jit_blacklist_side_trace | 8 | | opcache.jit_hot_func | 127 | | opcache.jit_hot_loop | 64 | | opcache.jit_hot_return | 8 | | opcache.jit_hot_side_exit | 8 | | opcache.jit_max_exit_counters | 8192 | | opcache.jit_max_loop_unrolls | 8 | | opcache.jit_max_polymorphic_calls | 2 | | opcache.jit_max_recursive_calls | 2 | | opcache.jit_max_recursive_returns | 2 | | opcache.jit_max_root_traces | 1024 | | opcache.jit_max_side_traces | 128 | | opcache.jit_prof_threshold | 0 | +---------------------------------------+----------------------------------------------+
问题原因
opcache.validate_timestamps = false:该配置关闭了OPcache对文件修改时间的自动检测,即使执行opcache:reset,OPcache也不会主动重新加载修改后的文件,除非重启PHP-FPM。- 启用了OPcache预加载:
opcache.preload指定的脚本会在PHP-FPM启动时将文件预加载到内存,这些预加载的内容是持久化的,cachetool的重置命令无法清除预加载的文件,必须重启PHP-FPM才能重新加载预加载的内容。
解决方法
临时生效方案(无需重启FPM)
- 临时将
opcache.validate_timestamps设置为true,执行cachetool重置命令后,可再改回false:# 编辑php.ini或PHP-FPM池配置文件,将opcache.validate_timestamps改为true # 重载PHP-FPM配置(无需重启) sudo service php8.2-fpm reload # 执行OPcache重置 sudo php /usr/bin/cachetool.phar opcache:reset --fcgi=/run/php/php8.2-fpm.sock # 可选:改回opcache.validate_timestamps=false并再次重载配置
长期解决方案
方案1:关闭预加载功能
如果不需要预加载,注释掉php.ini或FPM池配置中的以下两行,然后重启PHP-FPM:
; opcache.preload = '/var/www/hostname/prod/config/preload.php' ; opcache.preload_user = 'www-data'
之后使用cachetool的opcache:reset命令即可正常刷新缓存。
方案2:调整预加载策略
- 仅将不频繁更新的核心文件加入预加载,频繁修改的业务代码不预加载。
- 启用
opcache.validate_timestamps = true,并设置合理的opcache.revalidate_freq(比如保持默认的2秒),让OPcache自动检测文件更新,无需手动重置或重启FPM(会有轻微性能开销)。
内容的提问来源于stack exchange,提问作者hous
相关产品推荐
相关产品推荐

