最近公司后臺(tái)訪問(wèn)速度下降惰许,操作卡頓席覆,公司后臺(tái)環(huán)境是 windows server 2008 + iis7 + php5.6,于是在參考網(wǎng)上解決方案后汹买,覺(jué)得使用 opcache 最為方便佩伤,于是參考幾篇配置示例文檔后,在線部署了晦毙。經(jīng)過(guò)一段時(shí)間體驗(yàn)后生巡,發(fā)現(xiàn)確實(shí)快了很多,但是相應(yīng)的出現(xiàn)了一個(gè)以前幾乎沒(méi)見(jiàn)過(guò)的問(wèn)題见妒,訪問(wèn)后臺(tái)時(shí)經(jīng)常性 500 錯(cuò)誤孤荣,但是刷新頁(yè)面就恢復(fù)正常了,將這個(gè)現(xiàn)象在網(wǎng)上搜了一番须揣,確實(shí)存在這個(gè)問(wèn)題盐股,但是找了許多帖子也沒(méi)有發(fā)現(xiàn)靠譜的解決方案,于是就擱置了這個(gè)問(wèn)題耻卡,刷新頁(yè)面重試也是可以忍受的疯汁。
后來(lái),經(jīng)公司運(yùn)營(yíng)反饋發(fā)現(xiàn)卵酪,500 錯(cuò)誤發(fā)生的太頻繁了幌蚊,另外有時(shí)候刷新也不管用秸谢,因?yàn)楹笈_(tái)有些頁(yè)面會(huì)一次性批量請(qǐng)求后端接口,然后幾乎都是 500霹肝,這個(gè)問(wèn)題就有些嚴(yán)重了估蹄,于是重新開(kāi)始在網(wǎng)上尋求解決方式。
搜索到兩個(gè)方案如下:
1. 修改 IIS 應(yīng)用池配置
在 Internet 信息服務(wù)(IIS)管理器下的應(yīng)用程序池中選擇應(yīng)用沫换,右鍵-高級(jí)設(shè)置-標(biāo)識(shí)臭蚁,將 ApplicationPoolIdentity 修改為 LocalSystem,經(jīng)驗(yàn)證讯赏,失敗垮兑。
2. 修改 opcache 配置
在 PHP 手冊(cè)中有這么一個(gè)配置項(xiàng)
opcache.mmap_base string
在 Windows 平臺(tái)上共享內(nèi)存段的基地址。 所有的 PHP 進(jìn)程都將共享內(nèi)存映射到同樣的地址空間漱挎。 使用此配置指令避免“無(wú)法重新附加到基地址”的錯(cuò)誤系枪。
另外,在評(píng)論中有下面這段話磕谅,
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)
大致意思是當(dāng)我們?cè)?windows 平臺(tái)下開(kāi)啟 opcache 時(shí)私爷,你可能發(fā)現(xiàn)運(yùn)行時(shí)出現(xiàn)隨機(jī) 500 錯(cuò)誤,然后在 php.ini 中添加一行配置 opcache.mmap_base = 0x20000000
可以解決膊夹。不幸的是衬浑,我不知道值“0x20000000”的意義,我只能告訴你這個(gè)值可以解決這個(gè)問(wèn)題放刨。
經(jīng)過(guò)驗(yàn)證工秩,成功解決 500 錯(cuò)誤!
下面貼下我自己的 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