注意】編譯之前,需要把 xcode 套件安裝好!
1、準備相關軟體包,統一放到 /usr/local/src/php 目錄下:
http://cn2.php.net/distributions/php-5.6.21.tar.gz
http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
https://sourceforge.net/projects/libpng/files/libpng16/1.6.21/libpng-1.6.21.tar.xz/download
http://www.ijg.org/files/jpegsrc.v9a.tar.gz
http://www.bzip.org/1.0.6/bzip2-1.0.6.tar.gz
http://download.savannah.gnu.org/releases/freetype/freetype-2.4.12.tar.gz
http://ftp.gnu.org/gnu/gettext/gettext-0.19.tar.gz
2、安裝依賴庫
# 安裝 libmcryp
cd /usr/local/src/php
tar -xzvf ./libmcrypt-2.5.7.tar.gz
cd ./libmcrypt-2.5.7
./configure
make
sudo make install
# 安裝 libmhash
cd /usr/local/src/php
tar -xzvf ./mhash-0.9.9.9.tar.gz
cd ./mhash-0.9.9.9
./configure
make
sudo make install
# 安裝 mcrypt
cd /usr/local/src/php
tar -xzvf ./libmcrypt-2.5.7.tar.gz
cd ./libmcrypt-2.5.7
./configure
make
sudo make install
# 安裝 libiconv
cd /usr/local/src/php
tar -xzvf ./libiconv-1.14.tar.gz
cd ./libiconv-1.14
./configure
make
sudo make install
# 安裝 bzip2
cd /usr/local/src/php
tar -xzvf ./bzip2-1.0.6.tar.gz
cd ./bzip2-1.0.6
make
make install
# 安裝 libpng (png圖片處理, GD庫依賴)
cd /usr/local/src/php
tar -xzvf ./libpng-1.6.21.tar.xz
cd ./libpng-1.6.21
./configure
make
sudo make install
# 安裝 ibjpeg (jpg圖片處理, GD庫依賴)
cd /usr/local/src/php
tar -xzvf ./jpegsrc.v9a.tar.gz
cd ./jpeg-9a
./configure
make
sudo make install
# 安裝 freetype
cd /usr/local/src/php
tar -xzvf ./freetype-2.4.12.tar.gz
cd ./freetype-2.4.12
./configure
make
sudo make install
# 安裝 gettext
cd /usr/local/src/php
tar -xzvf ./gettext-0.19.tar.gz
cd ./gettext-0.19
./configure
make
sudo make install
3、安裝PHP
# 解壓PHP安裝包
cd /usr/local/src/php
tar -xzvf ./php-5.6.21.tar.gz
cd ./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-curlwrappers \
--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
【報錯】
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
# 執行 make
make
【報錯】
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-liconv -liconv -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/libssl.dylib /usr/local/opt/openssl/lib/libcrypto.dylib
# 重新make
make clean
make
【報錯】
ld: library not found for -lresolv-liconv
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sapi/cli/php] Error 1
【解決】