管理多个版本的PHP与自制
#php #homebrew #macos

使用Homebrew是一种在MACOS上安装多个版本的PHP的方便方法。例如,要同时安装php 7.4和8.0,请使用brew install命令。

首先安装7.4版本,例如:

% brew install php@7.4

用于指定使用Homebrew的包装的特定版本的语法为brew install <packageName>@<version>。在这种情况下,软件包名称为php,版本为7.4(截至撰写本文时默认为7.4.32)。如果您完全删除@<version>部分,Homebrew将安装任何可用的最新版本(截至撰写本文时)。

正确安装了php的7.4版:

% /opt/homebrew/opt/php@7.4/bin/php -v  
  PHP 7.4.32 (cli) (built: Sep 29 2022 10:45:51) ( NTS )
  Copyright (c) The PHP Group
  Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies

理想情况下,我们想将/opt/homebrew/opt/php@7.4/bin/php简单地称为命令行上的php。这需要另一个步骤:

% brew link --force --overwrite php@7.4
  Linking /opt/homebrew/Cellar/php@7.4/7.4.32... 812 symlinks created.

  If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

由于运行brew link,我们现在可以在此路径上找到php的链接版本:/opt/homebrew/bin/php

% ls -l /opt/homebrew/bin/php
  lrwxr-xr-x  1 myuser  admin  32 15 Oct 18:34 /opt/homebrew/bin/php -> ../Cellar/php@7.4/7.4.32/bin/php
% /opt/homebrew/bin/php -v
  PHP 7.4.32 (cli) (built: Sep 29 2022 10:45:51) ( NTS )
  Copyright (c) The PHP Group
  Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies

运行brew link的输出有一个建议,建议我们将两行附加到~/.zshrc文件的末尾,以告诉我们的终端,当我们参考php时,它应该首先在/opt/homebrew/opt/php@7.4/bin文件夹中寻找它。您可以执行此操作,但是每次brew link到不同版本的php时,您都必须编辑这些行。相反,我将采用一种稍微修改的方法:

% echo 'export PATH="/opt/homebrew/sbin:/opt/homebrew/bin:$PATH"' >> ~/.zshrc
% source ~/.zshrc

现在,我们的终端将在/opt/homebrew/bin上找到php的链接版本,每当我们参考php时。

% which php    
  /opt/homebrew/bin/php
% php -v
  PHP 7.4.32 (cli) (built: Sep 29 2022 10:45:51) ( NTS )
  Copyright (c) The PHP Group
  Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.32, Copyright (c), by Zend Technologies

现在,我们已经安装了php版本7.4,我们也可以通过安装php版本8.0继续。

% brew install php@8.0

现在,我们告诉Brew链接到新版本的php

% brew link --force --overwrite php@8.0
  Unlinking /opt/homebrew/Cellar/php@7.4/7.4.32... 328 symlinks removed.
  Linking /opt/homebrew/Cellar/php@8.0/8.0.24... 232 symlinks created.

在此命令的输出中,我们再次看到了运行包含命令export PATH...的两个命令的建议

% which php    
  /opt/homebrew/bin/php
% php -v
  PHP 8.0.24 (cli) (built: Sep 30 2022 08:39:20) ( NTS )
  Copyright (c) The PHP Group
  Zend Engine v4.0.24, Copyright (c) Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans
    with Zend OPcache v8.0.24, Copyright (c), by Zend Technologies

现在,我们可以通过重新链接从一个重新链接到另一个:brew link --force --overwrite php@7.4brew link --force --overwrite php@8.0

如果您不确定可以链接到哪些版本,请尝试以下命令:

% brew ls --versions | grep '^php[ @]'
  php@7.4 7.4.32
  php@8.0 8.0.24

最后,下面是我称为phpswitch的更完整脚本的源代码。可以将此脚本设置为executable,然后从终端运行,例如phpswitch 8.0phpswitch 7.4

#!/bin/bash

if [ $# -ne 1 ]; then
    echo 1>&2 "USAGE: $0 <phpVersion>"
    exit 2
fi

INSTALLED_VERSIONS=`find /opt/homebrew/opt | grep 'php@' | sed 's/\/opt\/homebrew\/opt\/php@//'`

if [[ ! -f /opt/homebrew/opt/php@${1}/bin/php ]]; then
    echo 1>&2 "/opt/homebrew/opt/php@${1}/bin/php was not found"
    printf 'valid options:\n%s\n' "${INSTALLED_VERSIONS[*]}"
    exit 2
fi

for VERSION in ${INSTALLED_VERSIONS[*]}; do
    brew unlink php@$VERSION > /dev/null 2>&1
    brew services stop php@$VERSION > /dev/null 2>&1
done

brew link --force --overwrite php@$1 > /dev/null 2>&1
brew services start php@$1 > /dev/null 2>&1