在 macOS 编译安装 PHP

2023-08-23 18:43:35 浏览数 (1)

环境

  • macOS 10.15.7
  • PHP 7.1.33

下载 PHP

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

代码语言:javascript复制
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 正确版本正常安装。

代码语言:javascript复制
# libiconv
brew install libiconv

# openssl
brew install openssl

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

配置编译安装

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

代码语言:javascript复制
# 查看配置参数
./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
代码语言:javascript复制
# 4核编译
make clean && make -j4
make install

安装常用库:

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

代码语言:javascript复制
./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

代码语言:javascript复制
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 相关。

代码语言:javascript复制
make clean && make -j4
make install

php --ri curl

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

代码语言:javascript复制
brew install libiconv

Cannot find OpenSSL’s

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

代码语言:javascript复制
brew install openssl
代码语言:javascript复制
./configure --with-openssl-dir=(brew --prefix openssl)

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

代码语言:javascript复制
/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....
代码语言:javascript复制
brew install libzip

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

reentrancy.c error: too few arguments to function call

代码语言:javascript复制
/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....

查找:

代码语言:javascript复制
.../php-src/main/reentrancy.c

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

eaddir_r(dirp, entry)

修改为:

代码语言:javascript复制
readdir_r(dirp, entry) 修改为 readdir_r(dirp, entry, &entry)

PEAR package PHP_Archive not installed

代码语言:javascript复制
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....
代码语言:javascript复制
./configure --without-pear --disable-phar

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

XCode 对编译的影响

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

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

References

  • Mac Pro 编译安装 PHP 5.6.21 及 问题汇总 | cnblogs
  • PHP7 编译安装 | cloud.tencent
  • Mac に php 7.0.3 を phpenv でインストールした時のメモ | qiita
  • php Make 编译 PEAR package PHP_Archive not installed | cnblogs
  • Mac 编译安装 php5.4.45 踩坑记录 1 | zhihu

– EOF –

  • # php
  • # mac

0 人点赞