下面的實(shí)驗(yàn)以php7.4.29版本為基礎(chǔ),如果你的版本不同,請(qǐng)注意替換某些路徑
一、確定PHP的根目錄
在macos12版本之后(也可能是11版本),homebrew安裝目錄發(fā)生了改變,請(qǐng)注意。
如果你是brew安裝的php,老版本的根目錄應(yīng)該在 /usr/local/homebrew/Cellar/php@7.4/7.4.29,新版本的目錄應(yīng)該在 /opt/homebrew/Cellar/php@7.4/7.4.29中,如果是其它方式安裝,請(qǐng)自定確定根目錄
二、下載PHP源碼
通過(guò)brew方式安裝的 php ,大概率沒(méi)有 openssl 擴(kuò)展的源碼,具體請(qǐng)查看 php根目錄/include/php/ext 中是否有 openssl 目錄
如果沒(méi)有這個(gè)目錄,需要到 PHP官網(wǎng)下載源碼。
通過(guò) php根目錄/bin/php --version 命令可以查看自己的php版本號(hào)


如果官網(wǎng)沒(méi)有提供你對(duì)應(yīng)的版本,可以選擇差不多的版本,拷貝連接,把版本數(shù)字改一下就行。
根據(jù)你下載的壓縮包格式,自行解壓,找到解壓出的 ext 目錄,里面會(huì)有 openssl目錄,將openssl目錄拷貝到 php根目錄/include/php/ext 中
三、開(kāi)始安裝
進(jìn)入 php根目錄/include/php/ext/openssl目錄
首先需要修改config0.m4為config.m4
然后執(zhí)行下面的命令安裝
$ `php根目錄/bin/phpize
$ ./configure --with-php-config=php根目錄/bin/php-config
$ make
$ make install
修改配置文件加入
extension=openssl.so


四、遇到的問(wèn)題
正常情況下,configure命令執(zhí)行中大概率會(huì)遇到 openssl 相關(guān)錯(cuò)誤,下面是常見(jiàn)錯(cuò)誤解決方式
1、缺少pkg-config
configure: error: The pkg-config script could not be found or is too old. Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.
這種容易解決,只需要 brew install pkg-config 安裝即可
2、缺少 openssl 或者 openssl 版本不匹配
No package 'openssl' found
這個(gè)問(wèn)題是最難解決的,造成的原因也很多
首先確定你安裝了 openssl

第二確定你當(dāng)前的openssl版本正確,php7安裝openssl需要1.1.1系列版本

這里大概率會(huì)遇到,看著安裝的是1.1,實(shí)際版本卻不對(duì)應(yīng)的情況,需要將1.1版本的路徑加到環(huán)境變量
如果是brew安裝的openssl,需要將下面的三行加入
.bash_profile 文件
export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
重新加載環(huán)境變量
$ source ~/.bash_profile
這里根據(jù)你用的 terminal,加入到不同的文件,比如zsh可能用的 .zshrc 文件
直到打印的版本是你安裝的1.1.1系列版本

查看 openssl 是否在 pkgconfig 中
$ pkg-config --list-all | grep openssl
或者去目錄直接查看
如果是brew安裝的pkgconfig,目錄應(yīng)該是 /opt/homebrew/lib/pkgconfig


如果沒(méi)有 openssl.pc 則需要手動(dòng)連接
ln -s /opt/homebrew/Cellar/openssl\@1.1/1.1.1n/lib/pkgconfig/openssl.pc /opt/homebrew/lib/pkgconfig/

到這里重新執(zhí)行 configure命令,應(yīng)該可以解決這個(gè)問(wèn)題
3、缺少libssl
Package 'libssl', required by 'openssl', not found
需要在環(huán)境變量中聲明 openssl lib/include 的路徑
加入環(huán)境變量
export OPENSSL_LIBS="-L/usr/local/Cellar/openssl@1.1/1.1.1d/lib"
export OPENSSL_CFLAGS="-I/usr/local/Cellar/openssl@1.1/1.1.1d/include"