关于Nextcloud的PHP OPcache 模块配置不正确警告

Nextcloud后台健康检测报警显示:

PHP OPcache 模块配置不正确。OPcache 驻留字符串缓冲区几乎已满。 为了确保可以有效地缓存重复字符串,建议将 “opcache.interned_strings_buffer” 应用到您的 PHP 配置,其值高于 “8”.. 更多细节,请参见文档 ↗

进入文档检查,官方建议如更改默认检查次数,并未提到修改内存。

opcache.revalidate_freq = 60

可以禁止验证,但官方不建议这么做,理由如下:

延长重新验证(或完全禁用)之间的时间意味着对脚本的手动更改(包括 )将需要更长的时间才能激活(或者永远不会这样做,如果 重新验证完全禁用)。延长时间还会增加出现暂时性服务器和应用程序升级问题的可能性。它还会阻止维护模式的正确切换。

对于内存占用,显示警告的上限值是90%,超过后管理员面板就会报警。

在报警页面建议修改opcache.interned_strings_buffer为8及以上,但PHP的默认值就是8,而且在配置中已经更改增大,需要找别的原因。

先改大看看效果,首先列出部分相关默认值 ↗

[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=1

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000

; The maximum percentage of "wasted" memory until a restart is scheduled.
;opcache.max_wasted_percentage=5

; 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=2

; 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

可以看到官方的修改其实很保守,只是修改了检查次数。

储存量也不小,改大一点。

opcache.memory_consumption=512
opcache.interned_strings_buffer=32

缓存内存占用扩大了,把PHP可用内存也扩一点。

memory_limit = 4096M

先观察效果。

修改完需要重启Apachece2,否则配置不生效。

sudo systemctl restart apache2

经过一系列修改,仍然报错。

通过Nextcloud官方文档的指引,我找到一个显示OPcache的实时工具opcache-gui↗

部署完成后发现问题应该是配置未起效,真正起效的配置不在/etc/php/8.1/apache2/php.ini/etc/php/8.1/cli/php.ini,而是在/etc/php/8.1/mods-available/opcache.ini

模块的单独配置权限似乎比全局配置更高,即使模块配置中并未配置相关值,但只要存在就会以模块配置为先,空缺的该模块配置采用默认值。

就目前Nextcloud目前500G左右的存储量,OPcache实时内存用量大约在100MB以内,驻留字节缓存在10MB以内。

/etc/php/8.1/mods-available/opcache.ini中插入以下配置。

opcache.memory_consumption=512
opcache.interned_strings_buffer=32

单独配置高于全局配置,略坑,但也不是不能理解。