MacBook Pro 编译安装PHP 解决依赖的一种方法


当编译安装时,我们需要查看编译选项:

./configure --help

确定了我们需要开启的特性,例如:

./configure --enable-fpm --with-openssl --with-zlib   --enable-bcmath  --enable-calendar --with-curl --with-enchant  --enable-exif --with-ffi   --enable-gd --with-webp --with-jpeg --with-freetype --with-gettext --with-mhash --without-iconv --with-ldap --with-ldap-sasl   --enable-mbstring --with-mysqli  --enable-pcntl --with-pdo-mysql  --enable-shmop  --enable-sockets --with-tidy --with-zip  --enable-mysqlnd

然后,不出意外,会得到错误信息如下:

checking for libxml-2.0 >= 2.7.6... no
configure: error: in `/Users/nemo/Softwares/php-7.4.2':
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.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details

如果使用brew来管理软件包的话,此时可以通过brew search libxml来搜索相关软件包列表,得到如下几个软件包:

libxml++    libxml++3    libxml2    libxmlsec1

看起来libxml2就是我们要安装的软件包了,执行:

brew install libxml2

安装过程可能有点儿慢,竟然要依赖sqlite、python。

安装完毕后,提示问内容很关键:

libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

最后三行说,如果需要编译的时候用到libxml2可以通过设置两个环境变量达到目的。但,这两个环境变量跟编译PHP的错误提示中并不相同。PHP中要求的是:LIBXML_CFLAGSLIBXML_LIBS。那么怎么办呢?

如果对编译有一定了解的话,知道编译有链接的过程,编译器需要找依赖的软件包的头文件等。那么去哪找呢?一般就是通过系统默认位置或者环境变量指定。

为什么PHP所要求的环境变量和软件包提示的不同呢?其实很容易理解,PHP依赖很多个第三方软件包,如果做区分处理,那么将是非常容易维护的,并且很容易自动化处理。

对比两组环境变量名称发现,PHP要求的LIBXML_CFLAGS就是软件包中提到的CPPFLAGSCCPP应该就是c语言、c++语言的意思吧。而LIBXML_LIBS就是软件包中提到的LDFLAGS了,这个可以从路径结尾是lib猜测出来。

执行:

export LIBXML_CFLAGS="-I/usr/local/opt/libxml2/include"
export LIBXML_LIBS="-L/usr/local/opt/libxml2/lib"

声明两个当前session有效的环境变量(关掉终端就失效了)

继续编译,不出意外还会碰到错误:

checking for openssl >= 1.0.1... no
configure: error: in `/Users/nemo/Softwares/php-7.4.2':
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.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details

似曾相识吧~ 首先搜索一下brew search openssl,发现我竟然安装过:

curl-openssl    glib-openssl
openssl@1.1 ✔   homebrew/portable-ruby/portable-openssl

那么,我们也没记住安装完提示的那两个环境变量对应的路径是啥呀?怎么办呢?

答案是:brew info openssl查看软件包安装信息:

openssl@1.1: stable 1.1.1d (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/usr/local/Cellar/openssl@1.1/1.1.1d (7,983 files, 17.9MB)
  Poured from bottle on 2020-01-12 at 11:03:06
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/openssl@1.1.rb
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl@1.1/certs

and run
  /usr/local/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /usr/local,
because openssl/libressl is provided by macOS so don't link an incompatible version.

If you need to have openssl@1.1 first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"

==> Analytics
install: 424,571 (30 days), 1,324,052 (90 days), 3,309,185 (365 days)
install-on-request: 57,213 (30 days), 198,971 (90 days), 516,123 (365 days)
build-error: 0 (30 days)

熟悉的内容又出现了~ 如法炮制:

export OPENSSL_CFLAGS="-I/usr/local/opt/openssl@1.1/include"
export OPENSSL_LIBS="-L/usr/local/opt/openssl@1.1/lib"

继续编译。。。继续如上步骤,直到所有的依赖全部解决即可

make && make install

了~


发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注