最近公司后台访问速度下降,操作卡顿,公司后台环境是 windows server 2008 + iis7 + php5.6,于是在参考网上解决方案后,觉得使用 opcache 最为方便,于是参考几篇配置示例文档后,在线部署了。经过一段时间体验后,发现确实快了很多,但是相应的出现了一个以前几乎没见过的问题,访问后台时经常性 500 错误,但是刷新页面就恢复正常了,将这个现象在网上搜了一番,确实存在这个问题,但是找了许多帖子也没有发现靠谱的解决方案,于是就搁置了这个问题,刷新页面重试也是可以忍受的。
后来,经公司运营反馈发现,500 错误发生的太频繁了,另外有时候刷新也不管用,因为后台有些页面会一次性批量请求后端接口,然后几乎都是 500,这个问题就有些严重了,于是重新开始在网上寻求解决方式。
搜索到两个方案如下:
1. 修改 IIS 应用池配置
在 Internet 信息服务(IIS)管理器下的应用程序池中选择应用,右键-高级设置-标识,将 ApplicationPoolIdentity 修改为 LocalSystem,经验证,失败。
2. 修改 opcache 配置
在 PHP 手册中有这么一个配置项
opcache.mmap_base string
在 Windows 平台上共享内存段的基地址。 所有的 PHP 进程都将共享内存映射到同样的地址空间。 使用此配置指令避免“无法重新附加到基地址”的错误。
另外,在评论中有下面这段话,
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)
大致意思是当我们在 windows 平台下开启 opcache 时,你可能发现运行时出现随机 500 错误,然后在 php.ini 中添加一行配置 opcache.mmap_base = 0x20000000
可以解决。不幸的是,我不知道值“0x20000000”的意义,我只能告诉你这个值可以解决这个问题。
经过验证,成功解决 500 错误!
下面贴下我自己的 opcache 配置
[opcache]
zend_extension="C:/php5.6/ext/php_opcache.dll"
; 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
opcache.mmap_base=0x20000000
; 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 100000 are allowed.
opcache.max_accelerated_files=20000
; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=10
; 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=60
; 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 disabled, PHPDoc comments are not loaded from SHM, so "Doc Comments"
; may be always stored (save_comments=1), but not loaded by applications
; that don't need them anyway.
;opcache.load_comments=1
; If enabled, a fast shutdown sequence is used for the accelerated code
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="D:/opcache_blacklist.txt"
; 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=120
; OPcache error_log file name. Empty string assumes "stderr".
;opcache.error_log="D:/log/opcache/err.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
; 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