Windows 2008 下 PHP 開啟 opcache 后頻繁 500 錯誤解決方式

最近公司后臺訪問速度下降,操作卡頓,公司后臺環(huán)境是 windows server 2008 + iis7 + php5.6,于是在參考網(wǎng)上解決方案后,覺得使用 opcache 最為方便,于是參考幾篇配置示例文檔后,在線部署了。經(jīng)過一段時間體驗后,發(fā)現(xiàn)確實快了很多,但是相應的出現(xiàn)了一個以前幾乎沒見過的問題,訪問后臺時經(jīng)常性 500 錯誤,但是刷新頁面就恢復正常了,將這個現(xiàn)象在網(wǎng)上搜了一番,確實存在這個問題,但是找了許多帖子也沒有發(fā)現(xiàn)靠譜的解決方案,于是就擱置了這個問題,刷新頁面重試也是可以忍受的。

后來,經(jīng)公司運營反饋發(fā)現(xiàn),500 錯誤發(fā)生的太頻繁了,另外有時候刷新也不管用,因為后臺有些頁面會一次性批量請求后端接口,然后幾乎都是 500,這個問題就有些嚴重了,于是重新開始在網(wǎng)上尋求解決方式。

搜索到兩個方案如下:

1. 修改 IIS 應用池配置

在 Internet 信息服務(IIS)管理器下的應用程序池中選擇應用,右鍵-高級設置-標識,將 ApplicationPoolIdentity 修改為 LocalSystem,經(jīng)驗證,失敗。


2. 修改 opcache 配置

在 PHP 手冊中有這么一個配置項

opcache.mmap_base string
在 Windows 平臺上共享內(nèi)存段的基地址。 所有的 PHP 進程都將共享內(nèi)存映射到同樣的地址空間。 使用此配置指令避免“無法重新附加到基地址”的錯誤。

另外,在評論中有下面這段話,

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 時,你可能發(fā)現(xiàn)運行時出現(xiàn)隨機 500 錯誤,然后在 php.ini 中添加一行配置 opcache.mmap_base = 0x20000000 可以解決。不幸的是,我不知道值“0x20000000”的意義,我只能告訴你這個值可以解決這個問題。
經(jīng)過驗證,成功解決 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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容