/

在 macOS 编译安装 PHP

环境

  • macOS 10.15.7
  • PHP 7.1.33

下载 PHP

源码地址:https://www.php.net/releases/#7.1.33

cd /tmp
wget https://www.php.net/distributions/php-7.1.33.tar.gz --no-check-certificate
tar zxvf php-7.1.33.tar.gz

安装相关库

确保 Xcode 正确版本正常安装。

# libiconv
brew install libiconv

# openssl
brew install openssl

# zlib 实现 GZIP 压缩页面
brew install zlib

配置编译安装

核心配置选项列表 | php.net

# 查看配置参数
./configure --help
./configure --help | grep openssl

# 注意替换 --prefix
# 按需进行调整
./configure --prefix=/Users/yifan/php/php71 \
--enable-bcmath \
--enable-calendar \
--enable-dba \
--enable-debug \
--enable-exif \
--enable-ftp \
--enable-fpm \
--enable-mbregex \
--enable-mbstring \
--enable-mysqlnd \
--enable-opcache-file \
--enable-soap \
--enable-sockets \
--enable-zip \
--with-curl=/usr/local/opt/curl \
--with-freetype-dir=/usr/local/opt/freetype \
--with-gd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv=/usr/local/opt/libiconv \
--with-icu-dir=/usr/local/opt/icu4c \
--with-jpeg-dir=/usr/local/opt/jpeg \
--with-libzip \
--with-openssl=/usr/local/opt/openssl@1.1 \
--with-bz2=/usr/local/opt/bzip2 \
--with-libxml-dir=/usr/local/opt/libxml2 \
--with-zlib=/usr/local/opt/zlib
# 4核编译
make clean && make -j4
make install

安装常用库:

[https://pecl.php.net/]

./bin/pecl install grpc
./bin/pecl install rdkafka
./bin/pecl install redis

./bin/pecl install mongodb
# PHP7.1
./bin/pecl install https://pecl.php.net/get/mongodb-1.11.1.tgz

./bin/pecl install swoole
# PHP7.1
./bin/pecl install https://pecl.php.net/get/swoole-4.5.11.tgz

遇到的问题

“_libiconv”, referenced from

Undefined symbols for architecture x86_64:
"_libiconv", referenced from:
_php_iconv_string in iconv.o
__php_iconv_strlen in iconv.o
__php_iconv_substr in iconv.o
__php_iconv_strpos in iconv.o
__php_iconv_mime_encode in iconv.o
__php_iconv_appendl in iconv.o
_php_iconv_stream_filter_append_bucket in iconv.o
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sapi/cli/php] Error 1

解决方法:打开 Makefile 找到:

  • EXTRA_LDFLAGS
  • EXTRA_LDFLAGS_PROGRAM

删除后面的 -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib 相关。

make clean && make -j4
make install

php --ri curl

Please specify the install prefix of iconv with –with-iconv=<DIR>

brew install libiconv

Cannot find OpenSSL’s

configure: error: Cannot find OpenSSL’s <evp.h>

brew install openssl
./configure --with-openssl-dir=(brew --prefix openssl)

error: implicit declaration of function ‘getpid’ is invalid in C99

/Users/.../php/php-7.1.33/ext/zip/lib/mkstemp.c:75:8: error:
implicit declaration of function 'getpid' is invalid in C99
[-Werror,-Wimplicit-function-declaration]
pid = getpid();
^
1 error generated.
make: *** [ext/zip/lib/mkstemp.lo] Error 1
make: *** Waiting for unfinished jobs....
brew install libzip

# ./configure add
--with-libzip=(brew --prefix libzip) \

reentrancy.c error: too few arguments to function call

/Users/.../php/php-7.1.33/main/reentrancy.c:139:23: error: too few
arguments to function call, expected 3, have 2
readdir_r(dirp, entry);
~~~~~~~~~ ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/dirent.h:110:1: note:
'readdir_r' declared here
int readdir_r(DIR *, struct dirent *, struct dirent **) __DARWIN_INODE64...
^
1 error generated.
make: *** [main/reentrancy.lo] Error 1
make: *** Waiting for unfinished jobs....

查找:

.../php-src/main/reentrancy.c

int readdir_r(DIR *, struct dirent *, struct dirent **)

eaddir_r(dirp, entry)

修改为:

readdir_r(dirp, entry) 修改为 readdir_r(dirp, entry, &entry)

PEAR package PHP_Archive not installed

opcache.a(shared_alloc_shm.o) has no symbols
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
clicommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
pharcommand.inc
make: *** [ext/phar/phar.phar] Segmentation fault: 11
make: *** Waiting for unfinished jobs....
./configure --without-pear --disable-phar

注意:不能加这个选项,会影响 composer 的使用。

XCode 对编译的影响

请保证 Xcode 正确的版本安装,否则可能有异常情况。如果系统不是最新版本可以通过下面的链接安装指定版本。

[https://developer.apple.com/download/all/?q=11.5]

References

– EOF –