下载源代码

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

下载最新版php安装包。

编译安装

  1. tar -zxvf php-7.4.7.tar.gz
  2. cd php-7.4.7/
  3. ./buildconf --force
  4. ./configure --prefix=/usr/local/php \
  5. --with-config-file-path=/usr/local/php/etc \
  6. --with-config-file-scan-dir=/usr/local/php/etc/conf.d \
  7. --enable-bcmath \
  8. --with-bz2=/usr/local/opt/bzip2 \
  9. --with-curl=/usr/local/opt/curl \
  10. --enable-filter \
  11. --enable-fpm \
  12. --with-fpm-user=www \
  13. --with-fpm-group=www \
  14. --with-gd \
  15. --enable-gd-native-ttf \
  16. --with-freetype-dir \
  17. --with-jpeg-dir \
  18. --with-png-dir \
  19. --enable-intl \
  20. --enable-mbstring \
  21. --with-mcrypt \
  22. --enable-mysqlnd \
  23. --with-mysql-sock=/tmp/mysql.sock \
  24. --with-mysqli=mysqlnd \
  25. --with-pdo-mysql=mysqlnd \
  26. --with-redis \
  27. --with-pdo-sqlite \
  28. --with-zlib-dir=/usr/local/opt/zlib \
  29. --with-iconv=/usr/local/Cellar/libiconv/1.16 \
  30. --with-icu-dir=/usr/local/opt/icu4c \
  31. --disable-phpdbg \
  32. --disable-phpdbg-webhelper \
  33. --enable-opcache \
  34. --enable-fileinfo \
  35. --with-openssl=/usr/local/bin/openssl-1.1.1g \
  36. --enable-simplexml \
  37. --with-sqlite3 \
  38. --enable-xmlreader \
  39. --enable-xmlwriter \
  40. --enable-zip \
  41. --enable-sockets \
  42. --with-xmlrpc

安装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.16 --disable-phpdbg --disable-phpdbg-webhelper --enable-opcache --enable-fileinfo --with-openssl=/usr/local/bin/openssl-1.1.1g --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

解决方法

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/bin/openssl-1.1.1g"

可能的报错三:

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

解决方法

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

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