前提条件
确保你已经安装了 Homebrew
和 Xcode Command Line Tools
。你可以通过以下命令安装它们:
- /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
-
- xcode-select --install
https://www.cnblogs.com/niuben/p/16109703.html
安装必要的依赖项
使用 Homebrew
安装 PHP
编译所需的依赖项:
brew install autoconf bison re2c libxml2 openssl@3 libiconv icu4c bzip2 readline krb5 curl
下载 PHP 源代码
从 PHP
官方网站下载源码包,或者使用 wget
:
- wget https://www.php.net/distributions/php-8.3.9.tar.gz
- tar -xvf php-8.3.9.tar.gz
- cd php-8.3.9
编写配置脚本
创建一个名为 configure_php.sh
的脚本文件,并写入以下内容:
- #!/bin/bash
-
- # 清理之前的编译结果
- make clean
- rm -rf config.cache
-
- # 设置PKG_CONFIG_PATH
- export PKG_CONFIG_PATH="/opt/homebrew/opt/krb5/lib/pkgconfig:/opt/homebrew/opt/openssl@3/lib/pkgconfig:/opt/homebrew/opt/libiconv/lib/pkgconfig:/opt/homebrew/opt/icu4c/lib/pkgconfig:/opt/homebrew/opt/readline/lib/pkgconfig:$PKG_CONFIG_PATH"
-
- # 设置CFLAGS和LDFLAGS,确保不重复
- export CFLAGS="-I/opt/homebrew/opt/krb5/include -I/opt/homebrew/opt/bzip2/include -I/opt/homebrew/opt/libiconv/include -I/opt/homebrew/opt/icu4c/include -I/opt/homebrew/opt/readline/include"
- export LDFLAGS="-L/opt/homebrew/opt/krb5/lib -L/opt/homebrew/opt/bzip2/lib -L/opt/homebrew/opt/libiconv/lib -L/opt/homebrew/opt/icu4c/lib -L/opt/homebrew/opt/readline/lib"
-
- # 运行配置脚本
- ./configure \
- --prefix=/usr/local/php \
- --with-config-file-path=/usr/local/php/etc \
- --with-config-file-scan-dir=/usr/local/php/etc/php.d \
- --enable-bcmath \
- --enable-calendar \
- --enable-exif \
- --enable-fpm \
- --enable-intl \
- --enable-mbstring \
- --enable-mysqlnd \
- --enable-opcache \
- --enable-pcntl \
- --enable-soap \
- --enable-sockets \
- --with-bz2=/opt/homebrew/opt/bzip2 \
- --with-curl \
- --with-freetype \
- --with-jpeg \
- --with-kerberos=/opt/homebrew/opt/krb5 \
- --with-libxml \
- --with-mysqli \
- --with-openssl=/opt/homebrew/opt/openssl@3 \
- --with-pdo-mysql \
- --with-pdo-sqlite \
- --with-readline=/opt/homebrew/opt/readline \
- --with-xsl \
- --with-zlib \
- --with-iconv=/opt/homebrew/opt/libiconv
运行配置脚本
赋予脚本执行权限并运行它:
- chmod +x configure_php.sh
- ./configure_php.sh
运行测试
建议在安装之前运行测试:
make test
编译和安装
运行以下命令进行编译和安装:
- make -j$(sysctl -n hw.ncpu)
- sudo make install
安装完成
- Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20230831/
- Installing PHP CLI binary: /usr/local/php/bin/
- Installing PHP CLI man page: /usr/local/php/php/man/man1/
- Installing PHP FPM binary: /usr/local/php/sbin/
- Installing PHP FPM defconfig: /usr/local/php/etc/
- Installing PHP FPM man page: /usr/local/php/php/man/man8/
- Installing PHP FPM status page: /usr/local/php/php/php/fpm/
- Installing phpdbg binary: /usr/local/php/bin/
- Installing phpdbg man page: /usr/local/php/php/man/man1/
- Installing PHP CGI binary: /usr/local/php/bin/
- Installing PHP CGI man page: /usr/local/php/php/man/man1/
- Installing build environment: /usr/local/php/lib/php/build/
- Installing header files: /usr/local/php/include/php/
- Installing helper programs: /usr/local/php/bin/
- program: phpize
- program: php-config
- Installing man pages: /usr/local/php/php/man/man1/
- page: phpize.1
- page: php-config.1
- Installing PDO headers: /usr/local/php/include/php/ext/pdo/
验证安装
安装完成后,检查 PHP 的版本和已启用的扩展:
- /usr/local/php/bin/php -v
- /usr/local/php/bin/php -m
配置 PHP 环境
为了方便使用,将 PHP
二进制文件添加到系统的 PATH
中:
- echo 'export PATH="/usr/local/php/bin:$PATH"' >> ~/.zshrc
- source ~/.zshrc
验证 PATH
重新打开终端或运行 source
命令后,验证 PHP
是否在 PATH
中:
php -v
你应该能够看到 PHP 版本信息。