OPcache

OPcache 是 PHP 中的 Zend 扩展,可以大大提升 PHP 的性能。
OPcache 通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销。

php.ini 配置
#加载opcache模块
zend_extension=opcache.so

[opcache]
;开启OPCache
opcache.enable=1

; CLI 环境下启用 Opcache
opcache.enable_cli=1

;OPcache共享内存存储大小,单位MB
opcache.memory_consumption=128

; 存储临时字符串缓存大小,单位为MB,PHP5.3.0以前会忽略此项配置
opcache.interned_strings_buffer=8

; 缓存文件数最大限制,命中率不到100%,可以试着提高这个值
opcache.max_accelerated_files=10000

; 在计划重新启动之前,“已浪费”内存的最大百分比。
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

; 线上环境默认是不开启,开启则设为1.不开启的话,会一直用缓存
opcache.validate_timestamps=0

; 一定时间内检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, 单位为秒
opcache.revalidate_freq=60


; 开启快速停止续发事件,依赖于Zend引擎的内存管理模块,一次释放全部请求变量的内存,而不是依次释放内存块
opcache.fast_shutdown=1

; 在include_path优化中启用或禁用文件搜索
;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

; 允许文件存在覆盖(file_exists等)性能功能。
opcache.enable_file_override=1

; A bitmask, where each bit enables or disables the appropriate OPcache
; passes
;opcache.optimization_level=0x7FFFBFFF

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

; 允许从缓存中排除大型文件。 默认情况下,所有文件都被缓存。
;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=1

; 首选共享内存后端。 留空,让系统决定。
opcache.preferred_memory_model="mmap"

; Protect the shared memory from unexpected writing during script execution.
; Useful for internal debugging only.
;opcache.protect_memory=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=

; 配置二级缓存目录并启用二级缓存。启用二级缓存可以在 SHM 内存满了、服务器重启或者重置 SHM 的时候提高性能。默认值为空字符串 "",表示禁用基于文件的缓存。
opcache.file_cache=/dev/shm

; 在共享内存中启用或禁用操作码缓存。
opcache.file_cache_only=1

; 从文件缓存加载脚本时启用或禁用校验和验证。
;opcache.file_cache_consistency_checks=1

; 表示opcache.file_cache_only = 1表示某个进程未能重新连接到共享内存(仅适用于Windows)。 需要明确启用文件缓存。
;opcache.file_cache_fallback=1

; 启用或禁用将PHP代码(文本段)复制到巨大页面中。 这样可以提高性能,但是需要适当的OS配置。
opcache.huge_code_pages=1

; 验证缓存的文件权限。
;opcache.validate_permission=0

; 防止在chroot环境中发生名称冲突。
;opcache.validate_root=0

; 如果指定,它将生成用于调试优化的不同阶段的操作码转储。
;opcache.opt_debug_level=0
END

本文标题:PHP7开启OPcache加速

本文作者:宇宙最帅的男人

本文链接:https://lolicp.com/linux/20211564.html

版权声明:转载或者引用本文内容请注明来源及原作者,本文著作权归作者 (宇宙最帅的男人) 所有。

除非另有说明,本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

最后修改:2021 年 06 月 15 日
如果觉得我的文章对你有用,请随意赞赏