简体   繁体   English

如何在 Mac OSX 上轻松切换 PHP 版本?

[英]How can I easily switch between PHP versions on Mac OSX?

I would like to test my application on PHP 5.3 up to PHP 7.0.我想在 PHP 5.3 到 PHP 7.0 上测试我的应用程序。

Where and how can I install the versions and how can I switch them by running a small script?我在哪里以及如何安装这些版本以及如何通过运行一个小脚本来切换它们?

If you have both versions of PHP installed, you can switch between versions using the link and unlink brew commands.如果您安装了两个版本的 PHP,您可以使用linkunlink brew命令在版本之间切换。

For example, to switch between PHP 7.4 and PHP 7.3例如,在 PHP 7.4 和 PHP 7.3 之间切换

brew unlink php@7.4
brew link php@7.3

PS: both versions of PHP have be installed for these commands to work. PS:已经安装了两个版本的 PHP 以使这些命令正常工作。

I found this very good tutorial on how to install and switch php versions on OSX.我发现这个关于如何在 OSX 上安装和切换 php 版本的非常好的教程

I can switch the version like我可以像这样切换版本

$ sphp 7.0 => PHP 7.0
$ sphp 7.3 => PHP 7.3
$ sphp 7.4 => PHP 7.4

Exactly what I want!正是我想要的!

Example: Let us switch from php 7.4 to 7.3示例:让我们从 php 7.4 切换到 7.3

brew unlink php@7.4
brew install php@7.3
brew link php@7.3

If you get Warning: php@7.3 is keg-only and must be linked with --force Then try with:如果您收到Warning: php@7.3 is keg-only and must be linked with --force然后尝试:

brew link php@7.3 --force

Using brew使用brew

Show current version显示当前版本

$ php -v

Change to different version更改为不同的版本
(eg. changing from 5.5.x to version 7.0.latest) : (例如,从 5.5.x 更改为 7.0.latest 版本):

$ brew unlink php55
$ brew install php70

If you install PHP with homebrew , you can switch between versions very easily.如果你用homebrew安装 PHP,你可以很容易地在版本之间切换。 Say you want php56 to point to Version 5.6.17, you just do:假设您希望php56指向版本 5.6.17,您只需执行以下操作:

brew switch php56 5.6.17

i think unlink & link php versions are not enough because we are often using php with apache(httpd), so need to update httpd.conf after switch php version.我认为 unlink & link php 版本是不够的,因为我们经常使用 php 和 apache(httpd),所以需要在切换 php 版本后更新 httpd.conf。

i have write shell script for disable/enable php_module automatically inside httpd.conf , look at line 46 to line 54 https://github.com/dangquangthai/switch-php-version-on-mac-sierra/blob/master/switch-php#L46我已经在httpd.conf中编写了用于禁用/启用php_module的 shell 脚本,请查看第 46 行到第 54 行https://github.com/dangquangthai/switch-php-version-on-mac-sierra/blob/master/switch -php#L46

Follow my steps:按照我的步骤:

1) Check installed php versions by brew, for sure everything good 1)通过brew检查安装的php版本,确保一切正常

> brew list | grep php
#output
php56
php56-intl
php56-mcrypt
php71
php71-intl
php71-mcrypt

2) Run script 2) 运行脚本

> switch-php 71 # or switch-php 56
#output
PHP version [71] found
Switching from [php56] to [php71] ... 
Unlink php56 ... [OK] and Link php71 ... [OK]
Updating Apache2.4 Configuration /usr/local/etc/httpd/httpd.conf ... [OK]
Restarting Apache2.4 ... [OK]
PHP 7.1.11 (cli) (built: Nov  3 2017 08:48:02) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

3) Finally, when your got above message, check httpd.conf , in my laptop: 3)最后,当您收到上述消息时,请在我的笔记本电脑中检查httpd.conf

vi /usr/local/etc/httpd/httpd.conf

You can see near by LoadModule lines你可以看到附近的 LoadModule 线

LoadModule php7_module /usr/local/Cellar/php71/7.1.11_22/libexec/apache2/libphp7.so
#LoadModule php5_module /usr/local/Cellar/php56/5.6.32_8/libexec/apache2/libphp5.so

4) open httpd://localhost/info.php 4) 打开httpd://localhost/info.php

i hope it helpful我希望它有帮助

I liked the switcher idea because I'm working of different version at the moment so what you need is我喜欢切换台的想法,因为我目前正在使用不同的版本,所以你需要的是

   brew install brew-php-switcher

then brew-php-switcher version然后brew-php-switcher version

for example brew-php-switcher 7.4例如brew-php-switcher 7.4

How to:如何:

  • Find installed PHP versions查找已安装的 PHP 版本
  • Switch from default PHP package to a versioned package从默认 PHP 包切换到版本化包
% brew search php
brew-php-switcher   php-cs-fixer        php@7.3             phplint             phpstan             pup
php ✔               php-cs-fixer@2      php@7.4 ✔           phpmd               phpunit
php-code-sniffer    php@7.2             phpbrew             phpmyadmin          pcp

% brew unlink php
% brew link php@7.4
brew link --overwrite php@7.3

非常适合我:-)

if you are using homebrew , then it allows multiple versions of a formula to be installed.如果您使用的是homebrew ,那么它允许安装多个版本的公式。 hence,因此,

  1. brew unlink is used to detach a version of formula from usage. brew unlink用于从使用中分离公式版本。
  2. brew link is used to attach a version of formula to usage. brew link用于将公式版本附加到使用中。

for example, if you have php 7.4 and 8.0 both installed and want to switch between them assuming you are currently using 7.4, then it can be done as :例如,如果您同时安装了 php 7.4 和 8.0,并且假设您当前使用的是 7.4,并且想要在它们之间切换,那么可以这样做:

brew unlink php@7.4

brew link php@8.0

delete all row with "php" in: nano ~/.zshrc删除所有带有“php”的行:nano ~/.zshrc

after that it will work: brew unlink php@7.4 && brew link php@8.0之后它将起作用: brew unlink php@7.4 && brew link php@8.0

I prefer to use phpbrew where you can easily install ( $ phpbrew install 8.1.3 +default ) and switch ( phpbrew switch 8.1.3 ) the used php version.我更喜欢使用 phpbrew,您可以轻松安装( $ phpbrew install 8.1.3 +default )和切换( phpbrew switch 8.1.3 )使用的 php 版本。

https://github.com/phpbrew/phpbrew https://github.com/phpbrew/phpbrew

Since the OP has specifically asked for switching from "PHP 5.3 up to PHP 7.0", a lot has been deprecated and/or removed in brew due to official support ending for them - as of Dec 2022.由于 OP 特别要求从“PHP 5.3 升级到 PHP 7.0”,因此自 2022 年 12 月起,由于对它们的官方支持结束,brew 中的许多内容已被弃用和/或删除。

So if we are to switch versions and install any of these unsupported versions brew install php@7.3 , it will fail ( Question and its answer here ).因此,如果我们要切换版本并安装任何这些不受支持的版本brew install php@7.3 ,它将失败(问题及其答案在这里)。 You'll need to use the tap shivammathur/php (praise the devs responsible for this) which can be used to install unsupported versions of PHP.您需要使用 tap shivammathur/php (赞美负责此的开发人员),它可用于安装不受支持的 PHP 版本。

brew tap shivammathur/php //need to do only once
brew install shivammathur/php/php@7.0 //or brew install php@7.4 depending on your choice
brew search php //optional, shows all the versions available if you want some other version
brew link php@7.0

Switching henceforth is simple:今后的切换很简单:

brew unlink php@7.0 
brew link php@5.6 //OP asked 5.3 but 5.6 is the lowest version supported by the tap

All versions <= php@7.4 have been deprecated and removed from brew because they reached their end of life .所有 <= php@7.4 的版本都已被弃用并从 brew 中删除,因为它们已达到生命的尽头 As of Dec 2022, the only officially supported versions are 8.0 and 8.1 which brew supports.截至 2022 年 12 月,官方支持的唯一版本是 brew 支持的 8.0 和 8.1。

This too eventually will get outdated as more versions reach EOL but the steps should remain the same as long as that tap remains in development.随着更多版本达到 EOL,这最终也会过时,但只要该水龙头仍在开发中,步骤就应该保持不变。

The link provided by Xaver's answer served as a good further reading. Xaver 的回答提供的链接是一个很好的进一步阅读。

If you have installed php with latest version on your local system and using using homebrew then to switch to older version, run following commands, Here, we are switching to 7.4如果您在本地系统上安装了最新版本的 php 并使用 homebrew 切换到旧版本,运行以下命令,这里我们切换到 7.4

brew unlink php
brew install php@7.4
brew link php@7.4

Old question but it's still a difficulty to have a quick way to switch between php versions in 2022. I use Laravel valet and switching PHP versions requires a re install of valet to work.老问题,但在 2022 年快速切换 php 版本仍然是一个困难。我使用 Laravel 代客,切换 PHP 版本需要重新安装代客才能工作。 I basically have two custom functions defined in my .zhsrc file, but I think this will work for bash too:我基本上在我的 .zhsrc 文件中定义了两个自定义函数,但我认为这也适用于 bash:


switchphp7() {
    valet stop
    brew unlink php@8.1
    brew services stop php@8.1
    brew link --force --overwrite php@7.4
    brew services start php@7.4
    composer global update
    rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}


switchphp8() {
    valet stop
    brew unlink php@7.4
    brew services stop php@7.4
    brew link --force --overwrite php@8.1
    brew services start php@8.1
    composer global update
    rm -f ~/.config/valet/valet.sock
    valet install
    valet start
}

And then just run them in the command line:然后只需在命令行中运行它们:

switchphp8

This works reliably for me, however, if you use Valets virtual SSL certificates, the command line will ask for admin account password for every ssl certificate you have linked which is potentially annoying if you have loads of them.这对我来说很可靠,但是,如果您使用 Valets 虚拟 SSL 证书,命令行会为您链接的每个 ssl 证书询问管理员帐户密码,如果您有大量的证书,这可能会很烦人。 It's not a bug if it keeps asking, eventually you will allow all if you keep typing password/use touch id.如果它一直询问,这不是一个错误,如果您继续输入密码/使用触摸 ID,最终您将允许所有。

My purpose was install old patch of php@7.4.24 to MacOS and I done that this way:我的目的是将 php@7.4.24 的旧补丁安装到 MacOS,我是这样做的:

I found revision of brew, which contains old php patch, which i need, and reset git to them我找到了 brew 的修订版,其中包含我需要的旧 php 补丁,并将 git 重置为它们

repository: https://github.com/Homebrew/homebrew-core/commit/3b342c146da0452d71d982324977567960da2979存储库: https ://github.com/Homebrew/homebrew-core/commit/3b342c146da0452d71d982324977567960da2979

git -C $(brew --repo)/Library/Taps/homebrew/homebrew-core reset --hard 3b09794

brew reinstall php@7.4 
  • First install or reinstall the version you want首先安装或重新安装你想要的版本

    brew reinstall php@7.4 brew 重新安装 php@7.4

  • Secondly you need to switch to the version you want其次你需要切换到你想要的版本

    echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH "' >> ~/.zshrc

  • Then you need to add in your PATH by running below command然后你需要通过运行下面的命令来添加你的 PATH

    brew-php-switcher 7.4 brew-php-switcher 7.4

Don't forget to change you environment PATH with this command:不要忘记使用此命令更改您的环境 PATH:

echo 'export PATH="/usr/local/opt/php@8.1/bin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM