【系统环境】
操作系统:OS X 10.11.5
Xcode:7.3.1
【注意】
编译之前,需要安装 xcode、Homebrew 套件!
1、准备相关软件包,统一放到 /usr/local/src/php 目录下:
2、安装依赖库
# 安装 libmcryptcd /usr/local/src/phptar -xzvf ./libmcrypt-2.5.7.tar.gzcd ./libmcrypt-2.5.7./configuresudo make sudo make install# 安装 libmhashcd /usr/local/src/phptar -xzvf ./mhash-0.9.9.9.tar.gzcd ./mhash-0.9.9.9./configuresudo makesudo make install# 安装 libiconvcd /usr/local/src/phptar -xzvf ./libiconv-1.14.tar.gzcd ./libiconv-1.14./configuresudo makesudo make install# 安装 bzip2cd /usr/local/src/phptar -xzvf ./bzip2-1.0.6.tar.gzcd ./bzip2-1.0.6sudo make sudo make install# 安装 libpng (png图片处理, GD库依赖)cd /usr/local/src/phptar -xzvf ./libpng-1.6.21.tar.xzcd ./libpng-1.6.21./configuresudo makesudo make install# 安装 ibjpeg (jpg图片处理, GD库依赖)cd /usr/local/src/phptar -xzvf ./jpegsrc.v9a.tar.gzcd ./jpeg-9a./configuresudo makesudo make install# 安装 freetypecd /usr/local/src/phptar -xzvf ./freetype-2.4.12.tar.gzcd ./freetype-2.4.12./configuresudo makesudo make install# 安装 gettextcd /usr/local/src/phptar -xzvf ./gettext-0.19.tar.gzcd ./gettext-0.19./configuresudo makesudo make install
3、安装PHP
# 解压PHP安装包cd /usr/local/src/phptar -xzvf ./php-5.6.21.tar.gzcd ./php-5.6.21
# 编译./configure \--prefix=/usr/local/php \--with-config-file-path=/usr/local/php/etc \--with-openssl \--with-zlib \--with-bz2 \--with-gd \--with-jpeg-dir \--with-png-dir \--with-gettext \--with-mhash \--with-freetype-dir \--with-mcrypt \--with-iconv \--with-curl \--with-xmlrpc \--with-mysql \--with-pdo-mysql \--with-mysqli \--enable-calendar \--enable-pdo \--enable-zip \--enable-mbstring \--enable-mbregex \--enable-bcmath \--enable-soap \--enable-sockets \--enable-ftp \--enable-gd-native-ttf \--enable-shmop \--enable-sysvmsg \--enable-sysvsem \--enable-sysvshm \--enable-xml \--enable-pcntl \--enable-fpm \--enable-opcache \--without-pear
【报错1】
configure: error: Cannot find OpenSSL's <evp.h>
【解决】
(1)、安装 openssl
brew install openssl
(2)、把 --with-openssl 改为 --with-openssl=/usr/local/opt/openssl
(如果安装了 brew,那么openssl的路径为 /usr/local/opt/openssl/)
./configure \--prefix=/usr/local/php \--with-config-file-path=/usr/local/php/etc \--with-openssl=/usr/local/opt/openssl \--with-zlib \--with-bz2 \--with-gd \--with-jpeg-dir \--with-png-dir \--with-gettext \--with-mhash \--with-freetype-dir \--with-mcrypt \--with-iconv \--with-curl \--with-xmlrpc \--with-mysql \--with-pdo-mysql \--with-mysqli \--enable-calendar \--enable-pdo \--enable-zip \--enable-mbstring \--enable-mbregex \--enable-bcmath \--enable-soap \--enable-sockets \--enable-ftp \--enable-gd-native-ttf \--enable-shmop \--enable-sysvmsg \--enable-sysvsem \--enable-sysvshm \--enable-xml \--enable-pcntl \--enable-fpm \--enable-opcache \--without-pear
# 执行 makesudo make
【报错2】
Undefined symbols for architecture x86_64:
"_PKCS5_PBKDF2_HMAC", referenced from: _zif_openssl_pbkdf2 in openssl.o "_TLSv1_1_client_method", referenced from: _php_openssl_setup_crypto in xp_ssl.o "_TLSv1_1_server_method", referenced from: _php_openssl_setup_crypto in xp_ssl.o "_TLSv1_2_client_method", referenced from: _php_openssl_setup_crypto in xp_ssl.o "_TLSv1_2_server_method", referenced from: _php_openssl_setup_crypto in xp_ssl.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_LIBS 节点,删除所有的 -lssl 和 -lcrypto 然后追加 libssl.dylib 和 libcrypto.dylib 的路径(如果你安装了 brew,那么则是 /usr/local/opt/openssl/lib/),重新运行 make 命令。
附上我修改后的 MakeFile EXTRA_LIBS 那一行:
EXTRA_LIBS = -lz -lresolv -lintl -lpng -lz -ljpeg -lcurl -lbz2 -lz -lm -lxml2 -lz -licucore -lm -lcurl -lxml2 -lz -licucore -lm -lfreetype -lz -lbz2 -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm /usr/local/opt/openssl/lib/libcrypto.dylib /usr/local/opt/openssl/lib/libssl.dylib
# 重新编译sudo make cleansudo make
【报错3】
Undefined symbols for architecture x86_64:
"_libiconv_open", referenced from: _do_convert in gdkanji.o _convert in encodings.o "_libiconv", referenced from: _do_convert in gdkanji.o _convert in encodings.o "_libiconv_close", referenced from: _do_convert in gdkanji.o _convert in encodings.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status make: *** [libs/libphp5.bundle] Error 1
【分析】
Makefile中有两个 -liconv,于是想到,会不会在系统中存在两份不同的 libiconv 呢,果然,系统本身 和 brew(或自己编译)各有一份iconv:
$ nm /usr/lib/libiconv.dylib | grep iconv
00000000000f1af0 S ___iconv_2VersionNumber 00000000000f1b90 S ___iconv_2VersionString 00000000000f60f0 D __libiconv_version 000000000000a1e1 T _iconv 000000000000a5a0 T _iconv_canonicalize0000000000013164 T _iconv_close 0000000000013171 T _iconv_open 000000000000a72c T _iconvctl 000000000000a20f T _iconvlist 0000000000014dbd T _libiconv_relocate 0000000000014cff T _libiconv_set_relocation_prefix$ nm /usr/local/lib/libiconv.dylib | grep iconv
00000000000f90c0 D __libiconv_version 0000000000002f10 T _iconv_canonicalize 0000000000002650 T _libiconv0000000000002680 T _libiconv_close00000000000011d0 T _libiconv_open 0000000000002690 T _libiconv_open_into 0000000000017970 t _libiconv_relocate 00000000000fa7a8 b _libiconv_relocate.initialized.b 00000000000178b0 T _libiconv_set_relocation_prefix 0000000000002bc0 T _libiconvctl 0000000000002d00 T _libiconvlist
所以就导致了–with-iconv-dir 无论配置成哪一个都链接不成功。
【解决】
最终的解决方案是直接修改Makefile,把里面的 两个-liconv分别替换成 /usr/lib/libiconv.dylib, /usr/local/lib/libiconv.dylib。使用绝对路径之后,这个链接就没有问题了。
EXTRA_LIBS = -lz -lresolv -lintl -lpng -lz -ljpeg -lcurl -lbz2 -lz -lm -lxml2 -lz -licucore -lm -lcurl -lxml2 -lz -licucore -lm -lfreetype -lz -lbz2 -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm /usr/local/opt/openssl/lib/libcrypto.dylib /usr/local/opt/openssl/lib/libssl.dylib /usr/lib/libiconv.dylib /usr/local/lib/libiconv.dylib
# 重新编译sudo make cleansudo make
【报错4】
"_mcrypt_enc_get_algorithms_name", referenced from:
_zif_mcrypt_get_cipher_name in mcrypt.o ......
【解决】
(1)、安装 mcrypt
brew install libmcrypt
(2)、修改 Makefile 文件,追加 libmcrypt 库的完整路径 /usr/local/lib/libmcrypt.dylib
EXTRA_LIBS = -lz -lresolv -lintl -lpng -lz -ljpeg -lcurl -lbz2 -lz -lm -lxml2 -lz -licucore -lm -lcurl -lxml2 -lz -licucore -lm -lfreetype -lz -lbz2 -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm -lxml2 -lz -licucore -lm /usr/local/lib/libmcrypt.dylib /usr/local/opt/openssl/lib/libcrypto.dylib /usr/local/opt/openssl/lib/libssl.dylib /usr/lib/libiconv.dylib /usr/local/lib/libiconv.dylib
终于没报错,继续安装,需要 sudo 一下获得权限
# 重新编译sudo make cleansudo makesudo make install
4、通过以下指令 启动 和 停止 php-fpm
# 启动 php-fpmphp-fpm -D# 关闭 php-fpmkillall php-fpm
注意:php-fpm.conf 中的 用户名 及 用户组 配置为当前用户,即 jianbao、staff
延伸阅读:
【参考】