Post view

在OS X El Capitan 10.11.6安裝ImageMagick 7.0.6、php55-ssh2、Zend OPcache的PHP快取模組

請先參考這幾篇前文:

在macOS Mojave 10.14安裝Homebrew與PHP 5.6

在macOS Mojave 10.14安裝Homebrew再繼續安裝ImageMagick的php56-imagick模組

在macOS Mojave 10.14安裝MacProts再繼續加裝php55-imagick、php72-imagick、php55-ssh2模組

由於macOS Mojave 10.14的Server已經不再是真正的Server,只剩下Xsan的網路磁碟機功能。想要在macOS Mojave 10.14架設網站的話,反而會變成困難的任務... 雖然不是「不可能任務」但是難度就會高很多,已經不是網站新手可以自行架設的程度。

也因此在macOS Mojave 10.14安裝架設Web Server網站所需的php55-imagick、php72-imagick、php55-ssh2模組,其實純粹是實驗與測試性質,完全沒有任何的實用價值。更不用說,現在已經完全無法使用Homebrew安裝php55-imagick、php56-imagick模組,也無法使用MacProts安裝想要使用這些的模組!

已經完全無法使用Homebrew或是MacProts安裝php55-imagick、php55-ssh2、Zend OPcache的話,不就完全卡死、束手無策?確實是這樣沒錯!

然而,幸好在去年的2017年,我在BNW網站的Mac mini主機上,有用過Homebrew安裝好php55-imagick、XCache、Zend OPcache。由於擔心Homebrew下載的套件會有安全性漏洞的問題,所以並沒有立即使用在insoler網站上,打算先觀察BNW一陣子,再決定是否把檔案複製到insoler。於是... 經過一段時間以後,連我自己也完全忘記了有做過這件事。

我在前面的文章有說明過,所謂使用Homebrew或是MacProts安裝模組,事實上只不過是從他們的網站上下載所需的檔案(也會一併下載一大堆完全不相關的垃圾檔案與說明文件檔案)到他們預設的檔案夾而已。

既然BNW的Mac mini主機上已經使用Homebrew下載過php55-imagick、XCache、Zend OPcache,而且確定都可以正常使用。我就不需要在insoelr的Mac mini主機上再使用Homebrew重新下載一次。事實上也已經完全無法下載,想要安裝也不可能安裝!

其實完全不需要在insoelr的Mac mini主機上再安裝Homebrew。只要把BNW的整個檔案複製到insoler就行了。

簡單的說,只要把BNW網站上的「/usr/local/」的所有檔案(不能包括MySQL)壓縮成 local.zip 檔案,再把這個 local.zip 檔案複製到insoler主機上,然後解壓縮還原,把全部的檔案都複製到相同的「/usr/local/」位置就行了。

不過,這是針對Homebrew。如果當時是使用MacProts安裝網站模組的話,安裝的路徑會變成「/opt/」,兩個位置完全不同,因為許多「設定檔案」的路徑不同,也無法直接整合。

如果自己動手把MacProts安裝的檔案夾位置「/opt/local/」底下的檔案全部搬移到「/usr/local/」重新啟動Apache,會在錯誤記錄檔案看到這樣的錯誤訊息:

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/lib/php55/extensions/no-debug-non-zts-20121212/ssh2.so' - dlopen(/usr/local/lib/php55/extensions/no-debug-non-zts-20121212/ssh2.so, 9): Library not loaded: /opt/local/lib/libssh2.1.dylib\n  Referenced from: /usr/local/lib/php55/extensions/no-debug-non-zts-20121212/ssh2.so\n  Reason: image not found in Unknown on line 0

主要的問題是Library not loaded: /opt/local/lib/libssh2.1.dylib無法載入位於「/opt/local/」的檔案!我猜想MacProts在編譯原始碼的時候,就直接使用絕對路徑「/opt/local/」而不是相對路徑,才會導致SSH2檔案搬移到「/usr/local/」就會因為找不到檔案,而無法載入SSH2模組。

把BNW網站上的「/usr/local/」的所有檔案複製到insoler網站的Mac mini主機上,然後修改「/etc/php.ini」設定檔案,追加以下的外掛模組的設定指令:

extension=/usr/local/Cellar/php55-imagick/3.4.3_4/imagick.so
zend_extension=/usr/local/Cellar/php55-opcache/5.5.38/opcache.so
extension=/opt/local/lib/php55/extensions/no-debug-non-zts-20121212/ssh2.so

只有加上外掛模組的設定指令,會無法使用php55-opcache,還必須開啟php.ini的相關設定:

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

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

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

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

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

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

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

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

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

; 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

; 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

然後打開OS X El Capitan的Server管理程式,關閉Web Server再重新啟動Web Server就可以讓PHP的設定生效。再寫一個很簡單的phpinfo.php程式,放在insoler網站的根目錄底下:

<? php
phpinfo();

從瀏覽器指定執行insoler網站的phpinfo.php程式,如果沒有安裝錯誤或是設定錯誤的話,應該就可以找到所需模組的相關資訊。

ImageMagick 7.0.6

目前最新的版本是ImageMagick-7.0.8-14.x86_64.rpm,這個開放原始碼的原始程式檔案無法直接安裝在macOS上,必須重新編譯才能使用。我對所有的「開放原始碼」的程式碼完全毫無興趣!不能直接使用的話,下載原始碼完全毫無意義!

Download @ ImageMagick

由於已經無法使用Homebrew安裝php55-imagick,所以就算是去年2017年8月的ImageMagick 7.0.6還是很新,並不會太過老舊,只要可以正常使用就行了。

imagick

imagick module enabled
imagick module version  3.4.3 
imagick classes  Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator, ImagickKernel 
Imagick compiled with ImageMagick version  ImageMagick 7.0.6-7 Q16 x86_64 2017-08-12 http://www.imagemagick.org 
Imagick using ImageMagick library version  ImageMagick 7.0.6-7 Q16 x86_64 2017-08-12 http://www.imagemagick.org 
ImageMagick copyright  © 1999-2017 ImageMagick Studio LLC 
ImageMagick release date  2017-08-12 
ImageMagick number of supported formats:  219 
ImageMagick supported formats  3FR, 3G2, 3GP, AAI, AI, ART, ARW, AVI, AVS, BGR, BGRA, BGRO, BMP, BMP2, BMP3, BRF, CAL, CALS, CANVAS, CAPTION, CIN, CIP, CLIP, CMYK, CMYKA, CR2, CRW, CUR, CUT, DCM, DCR, DCX, DDS, DFONT, DNG, DOT, DPX, DXT1, DXT5, EPDF, EPI, EPS, EPS2, EPS3, EPSF, EPSI, EPT, EPT2, EPT3, ERF, FAX, FILE, FITS, FRACTAL, FTP, FTS, G3, G4, GIF, GIF87, GRADIENT, GRAY, GROUP4, GV, HALD, HDR, HISTOGRAM, HRZ, HTM, HTML, HTTP, HTTPS, ICB, ICO, ICON, IIQ, INFO, INLINE, IPL, ISOBRL, ISOBRL6, JNG, JNX, JPE, JPEG, JPG, JPS, JSON, K25, KDC, LABEL, M2V, M4V, MAC, MAP, MASK, MAT, MATTE, MEF, MIFF, MKV, MNG, MONO, MOV, MP4, MPC, MPEG, MPG, MRW, MSL, MSVG, MTV, MVG, NEF, NRW, NULL, ORF, OTB, OTF, PAL, PALM, PAM, PANGO, PATTERN, PBM, PCD, PCDS, PCL, PCT, PCX, PDB, PDF, PDFA, PEF, PES, PFA, PFB, PFM, PGM, PICON, PICT, PIX, PJPEG, PLASMA, PNG, PNG00, PNG24, PNG32, PNG48, PNG64, PNG8, PNM, PPM, PS, PS2, PS3, PSB, PSD, PTIF, PWP, RADIAL-GRADIENT, RAF, RAS, RAW, RGB, RGBA, RGBO, RGF, RLA, RLE, RMF, RW2, SCR, SCT, SFW, SGI, SHTML, SIX, SIXEL, SPARSE-COLOR, SR2, SRF, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, THUMBNAIL, TIFF, TIFF64, TILE, TIM, TTC, TTF, TXT, UBRL, UBRL6, UIL, UYVY, VDA, VICAR, VID, VIFF, VIPS, VST, WBMP, WMV, WPG, X3F, XBM, XC, XCF, XPM, XPS, XV, YCbCr, YCbCrA, YUV 

 

Directive Local Value Master Value
imagick.locale_fix 0 0
imagick.progress_monitor 0 0
imagick.skip_version_check 0 0

 

php55-ssh2

因為無法使用Homebrew安裝php55-ssh2(也包括php56-ssh2、php71-ssh2等)所以這是使用MacProts安裝的php55-ssh2模組。

ssh2

SSH2 support enabled
extension version  0.13 
libssh2 version  1.8.0 
banner  SSH-2.0-libssh2_1.8.0 

 

Zend OPcache

雖然從php 5.6以後都有內建Opcache,不過我目前使用的OS X El Capitan 10.11.6內建的是PHP 5.5.38。而且就算是新的PHP版本,因為「PHP預設設定會關閉這項功能」所以網站的站長必須自己決定是否開啟Opcache快取模組。

PHP核心開發者告訴你PHP 7的五大效能密技

密技1  啟用Zend Opcache

PHP 7 主要開發者惠新宸也公開了5個PHP開發者一定要知道的新版效能密技。

Opcache是一個將預先編譯的腳本程式快取在共用記憶體的功能,但PHP預設設定會關閉這項功能,因此,很多開發者會忘了善用這個優化機制。即使不啟用Opcache,PHP 7的效能已可超越5.6版,啟用後還可以更快。可以直接在php.ini檔中加入以下設定來啟用Opcache。

zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1

上面這個設定指令只能僅供參考!因為如果你使用的不是PHP內建的Opcache,而是使用Homebrew或是MacProts下載的模組,就必須指定正確的opcache.so模組路徑,否則會因為無法載入模組,而沒有任何效果。如果可以正常啟動Opcache的話,就可以在phpinfo找到這樣的畫面:

 

Zend OPcache

Opcode Caching  Up and Running 
Optimization  Enabled 
Startup  OK 
Shared memory model  mmap 
Cache hits  95 
Cache misses  88 
Used memory  179877824 
Free memory  893864000 
Wasted memory  0 
Interned Strings Used memory  2167968 
Interned Strings Free memory  132049760 
Cached scripts  88 
Cached keys  180 
Max keys  130987 
OOM restarts  0 
Hash keys restarts  0 
Manual restarts  0 

 

Directive Local Value Master Value
opcache.blacklist_filename no value no value
opcache.consistency_checks 0 0
opcache.dups_fix Off Off
opcache.enable On On
opcache.enable_cli On On
opcache.enable_file_override Off Off
opcache.error_log no value no value
opcache.fast_shutdown 1 1
opcache.file_update_protection 2 2
opcache.force_restart_timeout 180 180
opcache.inherited_hack On On
opcache.interned_strings_buffer 128 128
opcache.load_comments 1 1
opcache.log_verbosity_level 1 1
opcache.max_accelerated_files 100000 100000
opcache.max_file_size 0 0
opcache.max_wasted_percentage 5 5
opcache.memory_consumption 1024 1024
opcache.optimization_level 0xFFFFFFFF 0xFFFFFFFF
opcache.preferred_memory_model no value no value
opcache.protect_memory 0 0
opcache.restrict_api no value no value
opcache.revalidate_freq 600 600
opcache.revalidate_path Off Off
opcache.save_comments 1 1
opcache.use_cwd On On
opcache.validate_timestamps On On

 

開啟PHP快取的Opcache模組以後,就一定會加快網頁的顯示速度?並不一定。因為Opcache只是一個快取模組,並不是把PHP純文字程式碼編譯成Assembly組合語言來加速執行的模組,所以沒有快取過的PHP網頁、沒有執行過的PHP網頁、有快取過,但因為太老舊被刪除的PHP網頁,都不會有任何的效果!

為了確認Opcache使用前(關閉時)與使用後(開啟時)到底有多少差別?我在insoler首頁的index.php裡面加上這段程式碼:

$time_start = microtime(true);
...

$time_end = microtime(true);
$time = $time_end - $time_start;
echofile('PHP程式的執行時間:' . $time);

然後打開echofile副程式儲存的記錄檔案,在16:45的時候,是關閉Opcache的index.php執行速度:

2018-11-04 16:45:14 PHP程式的執行時間:0.19024896621704
2018-11-04 16:45:18 PHP程式的執行時間:0.23456406593323
2018-11-04 16:45:22 PHP程式的執行時間:0.23119401931763
2018-11-04 16:45:26 PHP程式的執行時間:0.22738480567932
2018-11-04 16:45:31 PHP程式的執行時間:0.225341796875
2018-11-04 16:45:34 PHP程式的執行時間:0.22701191902161
2018-11-04 16:45:38 PHP程式的執行時間:0.21820092201233
2018-11-04 16:46:34 PHP程式的執行時間:0.1902129650116
2018-11-04 16:46:39 PHP程式的執行時間:0.09122109413147
2018-11-04 16:46:42 PHP程式的執行時間:0.093403816223145
2018-11-04 16:46:45 PHP程式的執行時間:0.091840028762817
2018-11-04 16:46:49 PHP程式的執行時間:0.094829082489014
2018-11-04 16:46:52 PHP程式的執行時間:0.092643976211548
2018-11-04 16:46:55 PHP程式的執行時間:0.097538948059082
2018-11-04 16:46:58 PHP程式的執行時間:0.092372894287109
2018-11-04 16:47:01 PHP程式的執行時間:0.096981048583984
2018-11-04 16:47:05 PHP程式的執行時間:0.097908973693848
2018-11-04 16:47:37 PHP程式的執行時間:0.090149164199829
2018-11-04 16:47:42 PHP程式的執行時間:0.09480094909668

在16:46以後的時間,則是開啟Opcache的index.php執行速度。你可以看到在16:46:34第一次執行的速度需要「0.19秒」,其實與關閉Opcache時16:45:14第一次執行的「0.19秒」完全相同。

但是第二次執行的速度就快很多,幾乎都在「0.1秒」以下。開啟Opcache的index.php執行速度從「0.2秒」進步到「0.1秒」,確實是有很明顯的效果。

測試步驟非常簡單,為了避免不斷重複執行index.php會看不出效果,所以在點選「首頁」以後,會故意點選討論區、相簿等其他幾個網頁,再點選「首頁」來觀察index.php的執行速度。

如果想要知道Opcahe的快取效能與各種狀態,可以在網站底下加裝一個amnuts/opcache-gui的PHP程式。你可以在GitHub網站(已經被微軟併購,屬於微軟的網站)下載這個PHP程式:

A clean, effective and responsive interface for Zend OPcache

從瀏覽器的網址列加上 /Opcahe/ 路徑就可以看到這樣的畫面:

目前OPcache快取模組使用的記憶體是358MB。設定在256MB的話會太小。設定在512MB或是1024MB會比較好。由於insoler的Mac mini主機上有16GB記憶體,就算快取記憶體容量設定在1024MB,事實上目前使用的記憶體容量全部只有5.6GB,還有10GB閒置中,並沒有任何問題。當然,你也可以設定在2048MB或是4096MB,要看你使用的PHP系統而定。

General info
Zend OPcache 7.0.6-dev
PHP 5.5.38
Host www.insoler.com
Server Software Apache
Start time 2018-11-05 08:05:36
Last reset never
Directives
blacklist filename no value
consistency checks 0
dups fix false
enable true
enable cli true
enable file override false
error log no value
fast shutdown true
force restart timeout 600
inherited hack true
interned strings buffer 256
load comments false
log verbosity level 1
max accelerated files 100000
max file size 0
max wasted percentage 0.3
memory consumption 1073741824
optimization level 4294967295
preferred memory model no value
protect memory false
revalidate freq 600
revalidate path false
save comments false
use cwd true
validate timestamps true
Available functions
opcache_reset
opcache_invalidate
opcache_compile_file
opcache_is_script_cached
opcache_get_configuration
opcache_get_status

在目前最新的PHP 7.2也有支援OPcache,不過某些功能已經被移除,並另外新增了許多新的功能。關於OPcache的設定值的意義,可以參考PHP網站上的說明:

OPcache Installing/Configuring

看不懂某些項目的說明的話也很正常。畢竟是PHP的宅男程式設計者寫的說明,只有PHP的專家自己看的懂,別人有看沒有懂也是必然的事情。

蘇言霖 2018/11/04 0 1207
Comments
Order by: 
Per page:
 
  • There are no comments yet
Rate
0 votes
Post info
蘇言霖
「超級懶貓級」社群網站站長
2018/11/04 (2000 days ago)
Actions