首先,我参考How to Install PHP 7.0 / 7.1 using HomeBrew on Mac Big Sur用exolnet/homebrew-deprecated 这个源安装失败了

/bin/sh ./config.sub -apple-darwin20.2.0 failed

多方查找如果,最后使用shivammathur/php安装成功了

安装php7.1

$brew tap shivammathur/php
$brew search php

==> Formulae
brew-php-switcher ✔            php@7.2                        phpmd                          shivammathur/php/php           shivammathur/php/php@7.2
php                            php@7.3                        phpmyadmin                     shivammathur/php/php@5.6       shivammathur/php/php@7.3
php-code-sniffer               php@7.4 ✔                      phpstan                        shivammathur/php/php@7.0       shivammathur/php/php@7.4 ✔
php-cs-fixer                   phplint                        phpunit                        shivammathur/php/php@7.1     shivammathur/php/php@8.1
==> Casks
eclipse-php                                         netbeans-php                                        phpstorm

$brew install php@7.1

切换版本

安装brew-php-swither切换工具

$brew install brew-php-switcher

==> Downloading https://github.com/philcook/brew-php-switcher/archive/v2.2.tar.g
==> Downloading from https://codeload.github.com/philcook/brew-php-switcher/tar.
#=#=-#  #                                                                     
==> Installing brew-php-switcher 
🍺  /usr/local/Cellar/brew-php-switcher/2.2: 6 files, 11.0KB, built in 4 seconds
$brew install php@7.1

brew切换php版本

$brew-php-switcher 7.1
Switching to php@7.1
Switching your shell
Unlinking /opt/homebrew/Cellar/php@7.1/7.1.33... 0 symlinks removed
Unlinking /opt/homebrew/Cellar/php@7.4/7.4.14_1... 25 symlinks removed
Linking /opt/homebrew/Cellar/php@7.1/7.1.33... 25 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
You will need sudo power from now on
Switching your apache conf
Restarting apache
All done!

环境变量

$echo 'export PATH="/opt/homebrew/opt/php@7.1/bin:$PATH"' >> ~/.zshrc
$echo 'export PATH="/opt/homebrew/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
$source .zshrc 
$php -v
PHP 7.1.33 (cli) (built: Dec 25 2020 07:27:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.33, Copyright (c) 1999-2018, by Zend Technologies

安装mongodb扩展

$sudo pecl install mongodb
………………

Build process completed successfully
Installing '/opt/homebrew/Cellar/php@7.1/7.1.33/pecl/20160303/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.9.0
Extension mongodb enabled in php.ini

$php -i | grep mongo
mongodb

安装失败的解决办法

如果在2022年还需要安装7.1的mongodb,你会发现pecl默认的mongodb版本已经不支持7.1了。这个时候可以查询一下pecl上面mongodb的发布日志,找到最后支持7.1的版本。
在这里我们可以看到,在1.12版本去掉了7.1的支持。
[PHPC-1880] - Drop support for PHP 7.1
所以指定安装1.12的上一个版本 1.11.1就行

$sudo pecl install mongod
………………
Build process completed successfully
Installing '/opt/homebrew/Cellar/php@7.1/7.1.33_4/pecl/20160303/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.11.1
Extension mongodb enabled in php.ini


$php -m | grep mongo
mongodb

buildyuan
95 声望6 粉丝

talk is cheap,show me the code