PHP笔记网

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2017-10-26 13:11:29  修改时间:2024-10-09 20:20:12  分类:Linux软件安装/参数优化  编辑

系统环境

操作系统:OS X 10.11.5

Xcode:7.3.1

注意

编译之前,需要安装 xcodeHomebrew 套件!

Mac Pro 安装 Homebrew 软件包管理工具

 

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、安装依赖库

# 安装 libmcrypt
cd /usr/local/src/php
tar -xzvf ./libmcrypt-2.5.7.tar.gz
cd ./libmcrypt-2.5.7
./configure
sudo 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
sudo make
sudo make install

# 安装 libiconv
cd /usr/local/src/php
tar -xzvf ./libiconv-1.14.tar.gz
cd ./libiconv-1.14
./configure
sudo make
sudo make install

# 安装 bzip2
cd /usr/local/src/php
tar -xzvf ./bzip2-1.0.6.tar.gz
cd ./bzip2-1.0.6
sudo make 
sudo make install

# 安装 libpng (png图片处理, GD库依赖)
cd /usr/local/src/php
tar -xzvf ./libpng-1.6.21.tar.xz
cd ./libpng-1.6.21
./configure
sudo make
sudo make install

# 安装 ibjpeg (jpg图片处理, GD库依赖)
cd /usr/local/src/php
tar -xzvf ./jpegsrc.v9a.tar.gz
cd ./jpeg-9a
./configure
sudo make
sudo make install

# 安装 freetype
cd /usr/local/src/php
tar -xzvf ./freetype-2.4.12.tar.gz
cd ./freetype-2.4.12
./configure
sudo make
sudo make install

# 安装 gettext
cd /usr/local/src/php
tar -xzvf ./gettext-0.19.tar.gz
cd ./gettext-0.19
./configure
sudo 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-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

 

# 执行 make
sudo 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 clean
sudo 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_canonicalize
0000000000013164 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 _libiconv
0000000000002680 T _libiconv_close
00000000000011d0 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 clean
sudo 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 clean
sudo make
sudo make install

 

4、通过以下指令 启动 和 停止 php-fpm

# 启动 php-fpm
php-fpm -D

# 关闭 php-fpm
killall php-fpm

 

注意:php-fpm.conf 中的 用户名 及 用户组 配置为当前用户,即  jianbaostaff

 

延伸阅读:

Mac Pro 开机自启动 PHP-FPM,Nginx,MySql 等软件

 

 

参考

http://www.widlabs.com/article/mac-os-x-compile-install-php

http://superuser.com/questions/394219/compiling-php-on-os-x-iconv-works-only-if-forced-to-64-bit

https://segmentfault.com/q/1010000004137098

https://upliu.net/compile-php-with-openssl-on-max-osx-error.html

http://b.liuctic.com/2012/01/build-php-on-lion/

https://segmentfault.com/a/1190000000606752

全新安装Mac OSX 开发者环境 同时使用homebrew搭建 PHP,Nginx ,MySQL,Redis,Memcache ... ... (LNMP开发环境)

Mac下安装LNMP(Nginx+PHP5.6)环境