Macos安装php多个版本

所属分类:php | 发布于 2022-12-18 20:42:18

要安装cocopods,所以把brew升级了一下,结果电脑上的php7.4不能用了,正好有个源码需要php5.6的版本,所以就想着装个多版本。然后又发现brew连php7.4都安装不了,只能安装8以上的版本,我这里新安装的是php8.1,又折腾了很近,还试了试MacPorts这款包管理工具,发现也不太好使。最后经过一番折腾,终于成功安装了多个php版本到电脑。这里记录一下。

1、安装php8.1

brew官方提供了php8.0和8.1的包,这里直接执行执行brew search php就能看到,然后执行brew install php@8.1就能顺利安装。

简单演示一下:

brew search php
brew install php@8.1

2、安装php7.4和php5.6

安装这种历史版本最主要的是找到合适的源,这里用shivammathur/php这个源。

2.1、 查看当前brew仓库:

brew tap

2.2、 添加仓库:

brew tap shivammathur/php

2.3、 删除仓库:

brew tap shivammathur/php

2.4、 安装shivammathur/php仓库后,使用brew search查看可安装的php版本:

alan@alans-MacBook-Pro ~ % brew search php
==> Formulae
brew-php-switcher                 php@8.0                           phpstan                           shivammathur/php/php@7.0          shivammathur/php/php@7.3          shivammathur/php/php@8.1 ✔
php                               php@8.1 ✔                         phpunit                           shivammathur/php/php@7.0-debug    shivammathur/php/php@7.3-debug    shivammathur/php/php@8.1-debug
php-code-sniffer                  phpbrew                           shivammathur/php/php              shivammathur/php/php@7.1          shivammathur/php/php@7.4 ✔        shivammathur/php/php@8.3
php-cs-fixer                      phplint                           shivammathur/php/php-debug        shivammathur/php/php@7.1-debug    shivammathur/php/php@7.4-debug    shivammathur/php/php@8.3-debug
php-cs-fixer@2                    phpmd                             shivammathur/php/php@5.6 ✔        shivammathur/php/php@7.2          shivammathur/php/php@8.0          pup
php@7.4 ✔                         phpmyadmin                        shivammathur/php/php@5.6-debug    shivammathur/php/php@7.2-debug    shivammathur/php/php@8.0-debug    pcp

==> Casks
eclipse-php                                         phpstorm                                            phpwebstudy                                         pop

2.5、 安装php7.4:

brew install shivammathur/php/php@7.4

2.6、 安装php5.6:

brew install shivammathur/php/php@5.6

3、php环境配置

不管安装哪个版本,最后控制台都会显示出类似这样的信息。

2023年6月20日更新:Brew在Intel版和苹果自研芯片Silimon CPU的M版上安装路径有区别,主要是Intel的安装目录是/usr/local/opt,M版安装目录是/opt/homebrew/opt。

这是Intel版的输出信息

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /usr/local/opt/php@8.1/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/8.1/

php@8.1 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@8.1 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@8.1/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@8.1/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.1 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@8.1/lib"
  export CPPFLAGS="-I/usr/local/opt/php@8.1/include"

To restart php@8.1 after an upgrade:
  brew services restart php@8.1
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@8.1/sbin/php-fpm --nodaemonize

这是M版的输出信息

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php@8.1/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.1/

php@8.1 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have php@8.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.1 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/php@8.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/php@8.1/include"

To start php@8.1 now and restart at login:
  brew services start php@8.1
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php@8.1/sbin/php-fpm --nodaemonize
==> Summary
????  /opt/homebrew/Cellar/php@8.1/8.1.20_1: 514 files, 81.8MB
==> Running `brew cleanup php@8.1`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> php@8.1
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php@8.1/lib/httpd/modules/libphp.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.1/

php@8.1 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have php@8.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/php@8.1/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/opt/homebrew/opt/php@8.1/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@8.1 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/php@8.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/php@8.1/include"

To start php@8.1 now and restart at login:
  brew services start php@8.1
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php@8.1/sbin/php-fpm --nodaemonize

根据上面的提示信息可以看到,配置主要有三步

3.1、在Apache配置文件httpd.conf中,载入php模块

php8.1版本:

LoadModule php_module /usr/local/opt/php@8.1/lib/httpd/modules/libphp.so

php7.4版本:

LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so

php5.6版本:

LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.so

3.2、在Apache配置文件httpd.conf中配置其它信息

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

DirectoryIndex index.php index.html

3.3、配置环境变量,一般都会用到cli,所以要配置php环境变量,配置到~/.bash_profile中

php8.1版本:

export PATH="/usr/local/opt/php@8.1/bin:$PATH"
export PATH="/usr/local/opt/php@8.1/sbin:$PATH"

php7.4版本:

export PATH="/usr/local/opt/php@7.4/bin:$PATH"
export PATH="/usr/local/opt/php@7.4/sbin:$PATH"

php5.6版本:

export PATH="/usr/local/opt/php@5.6/bin:$PATH"
export PATH="/usr/local/opt/php@5.6/sbin:$PATH"

 

4、多版本切换

网上有用brew-php-switcher来进行切换的,但是我不会,那就手动切换,就是要用哪个版本的php就在httpd.conf中加载哪个版本的Module,把其它的版本注释掉,然后重启apache就可以了。如果要在cli环境下使用不同版本的php,同样的,配置对应的版本的环境变量即可。

 

文哥博客(https://wenge365.com)属于文野个人博客,欢迎浏览使用

联系方式:qq:52292959 邮箱:52292959@qq.com

备案号:粤ICP备18108585号 友情链接