MacOS使用Brew 安装多个PHP版本记录

10 篇文章 0 订阅
5 篇文章 0 订阅

一、安装HomeBrew

参考下面链接:国内 HomeBrew 安装教程

MacOS下安装homebrew包管理器 - 简书MacOS下安装homebrew包管理器 由于官网提供的安装源一直报错,需要梯子才行,所以本人采用国内中科大安装源 此处需要注意自己终端shell使用的是哪种,一般情况下,系...https://www.jianshu.com/p/4e54be14691d

二、安装多个版本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

 四、版本切换操作

  1. 查看当前使用的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

➜  ~

到此结束。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在macOS安装brew,可以按照以下步骤进行操作: 1. 打开终端。 2. 在终端中输入以下命令并按下回车键:`/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"` 3. 如果是第一次安装,会弹出“命令行开发者工具”安装对话框,提示安装命令行开发者工具,按照操作安装即可。 4. 安装过程可能需要您输入mac的登录密码进行确认。请注意,密码输入时屏幕上不会显示任何字符。 5. 安装完成后,您可以使用`brew`命令来安装、卸载和更新各种软件包。 总结: 要在macOS安装brew,您可以打开终端并执行安装命令,然后按照提示完成安装安装完成后,您就可以使用`brew`命令来管理软件包了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [MacOS 安装Brew](https://blog.csdn.net/qq_36991248/article/details/127085330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Mac安装brew的四种方法(指定能行)](https://blog.csdn.net/muyimo/article/details/125211460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值