Mac 上编译安装 php

一、下载源代码

下载地址:http://www.php.net/downloads.php

下载最新版php安装包。

二、编译安装

php7 编译参数

tar -zxvf php-7.4.7.tar.gz 

cd php-7.4.7/

./buildconf --force

./configure --prefix=/usr/local/php73 
     --with-config-file-path=/usr/local/php73/etc 
     --with-config-file-scan-dir=/usr/local/php73/etc/conf.d 
     --enable-bcmath 
     --with-bz2=/usr/local/opt/bzip2 
     --with-curl=/usr/local/opt/curl 
     --enable-filter 
     --enable-fpm 
     --with-fpm-user=www 
     --with-fpm-group=www 
     --with-gd 
     --enable-gd-native-ttf 
     --with-freetype-dir 
     --with-jpeg-dir 
     --with-png-dir 
     --enable-intl 
     --enable-mbstring 
     --with-mcrypt 
     --enable-mysqlnd 
     --with-mysql-sock=/tmp/mysql.sock 
     --with-mysqli=mysqlnd 
     --with-pdo-mysql=mysqlnd 
     --with-redis 
     --with-pdo-sqlite 
     --with-zlib-dir=/usr/local/lib/
     --with-iconv=/usr/local/Cellar/libiconv/1.17
     --with-icu-dir=/usr/local/opt/icu4c 
     --disable-phpdbg 
     --disable-phpdbg-webhelper 
     --enable-opcache 
     --enable-fileinfo 
     --with-openssl=/usr/local/Cellar/[email protected]/1.1.1q/
     --enable-simplexml 
     --with-sqlite3 
     --enable-xmlreader 
     --enable-xmlwriter 
     --enable-zip 
     --enable-sockets 
     --with-xmlrpc

php8编译参数

安装PHP8 时去除了部分配置项

./configure --prefix=/usr/local/php8 \
    --with-config-file-path=/usr/local/php8/etc \
    --with-config-file-scan-dir=/usr/local/php8/etc/conf.d \
    --enable-bcmath --with-bz2=/usr/local/opt/bzip2 \
    --with-curl=/usr/local/opt/curl  \
    --enable-filter  \
    --enable-fpm \
    --with-fpm-user=www  \
    --with-fpm-group=www  \
    --enable-intl  \
    --enable-mbstring \
    --enable-mysqlnd  \
    --with-mysql-sock=/tmp/mysql.sock \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-pdo-sqlite \
    --with-zlib-dir=/usr/local/opt/zlib \
    --with-iconv=/usr/local/Cellar/libiconv/1.17 \
    --disable-phpdbg \
    --disable-phpdbg-webhelper \
    --enable-opcache \
    --enable-fileinfo \
    --with-openssl=/usr/local/Cellar/openssl\@3/3.0.5 \
    --enable-simplexml \
    --with-sqlite3 \
    --enable-xmlreader \
    --enable-xmlwriter  \
    --enable-sockets

可能的报错一:

configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

解决方法

brew install pkg-config

可能的报错二:

configure: error: Package requirements (openssl >= 1.0.1) were not met:  
No package 'openssl' found

解决方法

brew install openssl

可能的报错三:

checking for icu-uc >= 50.1 icu-io icu-i18n... no
configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:

No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found

解决方法

brew install icu4c

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/icu4c/lib/pkgconfig"

可能的报错四:

configure: error: Cannot find zlib

解决方法

git clone [email protected]:madler/zlib.git

cd zlib

git checkout v1.2.13 # 切换到指定版本

./configure 

make

sudo make install

三、make && make install

make   

make install

可能的报错一:

/usr/local/bin/php-7.4.7/ext/libxml/libxml.c:34:10: fatal error: 'libxml/parser.h' file not found
#include <libxml/parser.h>
         ^~~~~~~~~~~~~~~~~
1 error generated.
make: *** [ext/libxml/libxml.lo] Error 1

解决方法

brew install libxml2

echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/opt/libxml2/lib/pkgconfig"

四、配置自定义命令

vim ~/.bash_profile

alias php='/usr/local/php/bin/php'
alias php-fpm='/usr/local/php/sbin/php-fpm'

alias php8="/usr/local/php8/bin/php"
alias composer8="/usr/local/php8/bin/php /usr/local/Cellar/composer/1.8.5/bin/composer"

source ~/.bash_profile

引用链接

[1] http://www.php.net/downloads.php: http://www.php.net/downloads.php