一、安装HomeBrew
参考下面链接:国内 HomeBrew 安装教程
二、安装多个版本PHP
由于homebrew主库中没有PHP7.2 之前的版本,所以需要先挂在第三方的扩展,具体操作如下:
➜ ~ brew tap shivammathur/php #挂载第三方扩展
➜ ~ brew search php #搜索php包文件
==> Formulae
brew-php-switcher ✔ phpstan
php ✔ phpunit
php-code-sniffer ✔ shivammathur/php/php ✔
php-cs-fixer shivammathur/php/php@5.6 ✔
php-cs-fixer@2 ✔ shivammathur/php/php@7.0
php@7.2 ✔ shivammathur/php/php@7.1
php@7.3 shivammathur/php/php@7.2 ✔
php@7.4 ✔ shivammathur/php/php@7.3
php@8.0 ✔ shivammathur/php/php@7.4 ✔
phpbrew shivammathur/php/php@8.0 ✔
phplint shivammathur/php/php@8.2
phpmd pcp
phpmyadmin pup
==> Casks
eclipse-php phpstorm
其中 shivammathur 开头的文件都是属于第三方扩展的。
挂载好第三方扩展后,接下来安装PHP,具体操作以下面演示为例:
详细安装过程省略了,都是脚本自动执行的。
#安装第三方的PHP
➜ ~ brew install shivammathur/php/php@5.6
#安装homebrew的PHP
➜ ~ brew install php@8.0
➜ ~ brew install php@7.2
三、安装版本切换工具
# 安装版本切换工具
➜ ~ brew install brew-php-switcher
四、版本切换操作
- 查看当前使用的PHP版本号,可以看到当前的版本是8.0
# 通过 php -v 命令查看当前使用的PHP版本号
➜ ~ php -v
PHP 8.0.16 (cli) (built: Mar 1 2022 09:59:14) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.16, Copyright (c) Zend Technologies
with Zend OPcache v8.0.16, Copyright (c), by Zend Technologies
➜ ~
2. 使用brew-php-switcher切换到7.2
# 使用brew-php-switcher 切换为7.2版本
➜ ~ brew-php-switcher 7.2
Switching to php@7.2
Switching your shell
Unlinking /usr/local/Cellar/php@5.6/5.6.40_4... 0 symlinks removed.
Unlinking /usr/local/Cellar/php@7.2/7.2.34_4... 0 symlinks removed.
Unlinking /usr/local/Cellar/php@7.4/7.4.28_1... 0 symlinks removed.
Unlinking /usr/local/Cellar/php@8.0/8.0.16_1... 25 symlinks removed.
Unlinking /usr/local/Cellar/php/8.1.3_1... 0 symlinks removed.
Linking /usr/local/Cellar/php@7.2/7.2.34_4... 25 symlinks created.
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc
You will need sudo power from now on
Switching your apache conf
Password:
Restarting apache
All done!
Tips:切换完毕后,记得按照提示添加相关环境变量。然后重启terminal。
If you need to have this software first in your PATH instead consider running:
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc
重启后,查看php版本号:
Last login: Thu Mar 10 15:12:47 on ttys001
➜ ~ php -v
PHP 7.2.34 (cli) (built: Mar 3 2022 07:13:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies
➜ ~
如上所示,终端中的php版本已经切换成功。
需要注意!此时nginx 中使用的版本还没有切换成功。
➜ ~ ps aux | grep php
yamol 25872 0.0 0.0 34361400 3228 ?? S 3:02下午 0:00.01 /usr/local/opt/php@8.0/sbin/php-fpm --nodaemonize
yamol 25871 0.0 0.0 34360376 3620 ?? S 3:02下午 0:00.01 /usr/local/opt/php@8.0/sbin/php-fpm --nodaemonize
yamol 25870 0.0 0.1 34343224 13832 ?? S 3:02下午 0:00.16 /usr/local/opt/php@8.0/sbin/php-fpm --nodaemonize
yamol 29357 0.0 0.0 34122828 792 s000 R+ 4:01下午 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox php
➜ ~
通过 ps 命令可以看到,此时运行的还是 8.0 版本。还需要以下操作:
#停止8.0
➜ ~ brew services stop php@8.0
Stopping `php@8.0`... (might take a while)
==> Successfully stopped `php@8.0` (label: homebrew.mxcl.php@8.0)
#查看是否停用成功
➜ ~ ps aux | grep php
yamol 29547 0.0 0.0 34122828 816 s000 S+ 4:07下午 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox php
#开启7.2
➜ ~ brew services restart php@7.2
==> Successfully started `php@7.2` (label: homebrew.mxcl.php@7.2)
#查看是否启动成功
➜ ~ ps aux | grep php
yamol 29726 0.0 0.0 34131020 828 s000 R+ 4:07下午 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox php
yamol 29720 0.0 0.0 34347612 640 ?? S 4:07下午 0:00.00 /usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize
yamol 29719 0.0 0.0 34347612 656 ?? S 4:07下午 0:00.00 /usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize
yamol 29713 0.0 0.1 34347868 12164 ?? S 4:07下午 0:00.08 /usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize
➜ ~
查看当前使用版本的 php.ini 文件位置使用命令 php --ini
➜ ~ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/7.2
Loaded Configuration File: /usr/local/etc/php/7.2/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.2/conf.d/ext-opcache.ini
➜ ~
到此结束。