Mac(Sierra 10.13)安装PHP 7.2线程安全版遇阻求助
Hey there, I’ve dealt with this exact frustration before— Homebrew dropped thread-safety support for older PHP versions a while back, so let’s walk through two reliable ways to get a thread-safe (ZTS) PHP 7.2 build running on your Sierra system.
为什么Homebrew的方法没用?
Homebrew removed the --with-thread-safety flag from its PHP formulas because they focus on maintaining the non-thread-safe (NTS) build by default. That’s why you got the warning saying the option would be ignored— it no longer exists in the formula.
方案1:用php-build快速编译ZTS版本
php-build is a handy tool that takes the hassle out of compiling custom PHP versions. Here’s how to use it for ZTS:
First, install php-build and all required dependencies (note we use openssl@1.1 since newer versions don’t support Sierra):
brew install php-build autoconf automake libtool openssl@1.1Compile PHP 7.2 with ZTS enabled:
PHP_BUILD_CONFIGURE_OPTS="--enable-maintainer-zts --with-openssl=$(brew --prefix openssl@1.1)" php-build 7.2.34 ~/local/php72-ts- Swap
7.2.34for any 7.2.x patch version you need (all 7.2 releases are compatible here) ~/local/php72-tsis where your PHP will be installed— feel free to change this path to something more convenient
- Swap
Check if it worked:
~/local/php72-ts/bin/php -vYou’ll know it’s thread-safe if you see
ZTSin the output, like this:PHP 7.2.34 (cli) (built: Oct 12 2024 10:30:00) ( ZTS )
方案2:手动编译PHP源码(完全可控)
If you want full control over every part of the build, go with manual compilation. Here’s the step-by-step:
Install all build dependencies first:
brew install openssl@1.1 curl libxml2 libjpeg libpng freetype autoconf automake libtoolGrab the PHP 7.2 source code and extract it:
curl -O https://www.php.net/distributions/php-7.2.34.tar.gz tar -xzf php-7.2.34.tar.gz cd php-7.2.34Configure the build with ZTS (adjust the flags to include any extensions you need):
./configure \ --prefix=/usr/local/php72-ts \ --enable-maintainer-zts \ --with-openssl=$(brew --prefix openssl@1.1) \ --with-curl \ --with-libxml-dir=$(brew --prefix libxml2) \ --with-jpeg-dir=$(brew --prefix libjpeg) \ --with-png-dir=$(brew --prefix libpng) \ --with-freetype-dir=$(brew --prefix freetype) \ --enable-mbstring \ --enable-cli--enable-maintainer-ztsis the key flag for thread safety (it replaced the old--with-thread-safetyoption you tried earlier)- Add flags like
--enable-mysqlndif you need MySQL support, or remove any extensions you don’t use
Compile and install:
# Use all your CPU cores to speed up compilation make -j$(sysctl -n hw.ncpu) sudo make installVerify the thread-safe build:
/usr/local/php72-ts/bin/php -vLook for the
ZTStag in the version output to confirm it worked.
小提醒
- macOS Sierra 10.13 is no longer supported, so some newer packages might fail to install. If you hit compilation errors, try installing older versions of problematic dependencies (e.g.,
brew install libxml2@2.9if the latest version causes issues). - If you need to use this PHP with a web server like Apache or Nginx, add the corresponding SAPI flag during the configure step (e.g.,
--enable-apache2for Apache).
内容的提问来源于stack exchange,提问作者Dani

