准备
- Mac安装brew已安装的自行跳过
安装
-
查看提供的PHP所有版本
brew search php
,默认只有PHP最新版本
-
如果想安装其他版本比如php7.3版本,执行
brew tap shivammathur/php
-
再执行
brew search php
就会发现有PHP7.3版本了 -
执行安装PHP命令,遇到
Y/N
输入Y
,静等执行成功,然后启动服务。# 默认最新版本 brew install php # 安装指定版本 比如7.3 brew install php@7.3
切换版本工具
-
安装命令
brew install brew-php-switcher
$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-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!
命令
- PHP启动
brew services start php@7.3
- PHP停止
brew services stop php@7.3
- PHP重启
brew services restart php@7.3
安装位置
- 配置路径(
php.ini
在这个路径下)/opt/homebrew/etc/php/7.3
- 安装路径
/opt/homebrew/opt/php@7.3
- 源路径
/opt/homebrew/Cellar/php
配置
-
加入环境变量,执行命令成功。
php -v
就会发现变成想要的版本# 依次执行如下命令 echo 'export PATH="/opt/homebrew/opt/php@7.3/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php@7.3/sbin:$PATH"' >> ~/.zshrc source ~/.zshrc