公司新開發的專案上線了,出于好奇心,打算將網站服務改為swoole測一下性能,正好換了個新電腦,可以從頭安裝一下Swoole擴展,卻發現蘋果電腦M1芯片安裝起擴展來有很多坑,花了一下午時間才搞好,網路上的解決方案都不全或者很分散,在此整合記錄一下,
一、下載
Mac上安裝擴展有兩種方式,可以用PHP自帶的PECL工具,也可以原始碼編譯安裝,原始碼下載地址:
- https://github.com/swoole/swoole-src/releases
- https://pecl.php.net/package/swoole
- https://gitee.com/swoole/swoole/tags
二、安裝
1. PECL工具安裝
比較推薦PECL方式安裝,可以省去一些M1芯片帶來的兼容性問題,相比原始碼方式簡便許多,
執行命令
sudo pecl install swoole
2.原始碼安裝
進入原始碼檔案夾,原始碼檔案夾可以解壓下載的原始碼壓縮包得到
cd swoole-src
按順序執行命令
sudo phpize
sudo ./configure --with-php-config=/opt/homebrew/opt/[email protected]/bin/php-config --enable-sockets=yes --enable-openssl=yes --with-openssl-dir=/opt/homebrew/opt/[email protected] --enable-http2=yes
sudo make
sudo make install
安裝成功后會列印出安裝好的swoole.so的位置,在php.ini中加入以下陳述句啟用擴展
extension=swoole.so
重啟PHP,看一下php -m中是否有swoole,有就說明成功了
三、常見例外
- 使用pecl安裝的時候會提示你需要開啟的功能,如果開啟openssl的話,使用默認的設定會報錯,這是因為默認的openssl是brew安裝的,檔案位置和pecl默認的檔案位置不同,會提示無法找到openssl,這時需要找到你電腦上openssl實際的安裝位置,我的是
/opt/homebrew/opt/[email protected],具體操作如下:
enable sockets supports? [no] : yes
enable openssl support? [no] : yes --with-openssl-dir=/opt/homebrew/opt/[email protected]
enable http2 support? [no] : yes
enable mysqlnd support? [no] : yes
enable json support? [no] : yes
enable curl support? [no] : yes
- 提示
fatal error: 'pcre2.h' file not found,這是由于M1芯片缺少pcre軟連接,解決方法:
ln -s /opt/homebrew/include/pcre2.h /opt/homebrew/Cellar/[email protected]/7.4.16/include/php/ext/pcre/pcre2.h
- 安裝擴展后,運行PHP時提示mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))),注意need后的arm64e,這是由于我的電腦的架構是arm64e的,需要使用相同的架構來安裝擴展,
- 查看本機系統資訊
uname -a
- 得到結果 x86_64
Darwin songruideMacBook-Pro.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 x86_64
- 安裝擴展
arch -arm64e sudo pecl install swoole
- 如果是arm64e則
arch -x86_64 sudo pecl install swoole
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519103.html
標籤:PHP
上一篇:day52-正則運算式03
