马春杰杰 Exit Reader Mode

macOS安装PHP并进行多版本管理

之前一直使用宝塔进行PHP管理,但是macOS系统下暂时不支持宝塔,所以就直接使用系统本地的PHP了。

这里使用homebrew进行安装(感谢@大卫,让我发现原来brew还可以安装PHP扩展)。

这里以PHP7.4PHP7.1的互相切换举例,首先安装homebrew,这个大家应该都装了,没装的可以参考这个:

安装homebrew之后,开始安装PHP

brew install php@7.4

brew install php@7.1

brew install brew-php-switcher

brew services start php@7.4

echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> /Users/mcj/.bash_profile

echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> /Users/mcj/.bash_profile

source ~/.bash_profile

pecl install pcntl

PHP安装成功之后会提示:

/usr/local/Cellar/php@7.2/7.2.34_4/bin/pear
config-set
php_ini
/usr/local/etc/php/7.2/php.ini
system

dyld: Library not loaded: /usr/local/opt/tidy-html5/lib/libtidy.5.dylib
  Referenced from: /usr/local/Cellar/php@7.2/7.2.34_4/bin/php
  Reason: image not found
Warning: The post-install step did not complete successfully
You can try again using:
  brew postinstall php@7.2
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.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/7.2/

php@7.2 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@7.2 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> /Users/mcj/.bash_profile
  echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> /Users/mcj/.bash_profile

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


To start php@7.2:
  brew services start php@7.2
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@7.2/sbin/php-fpm --nodaemonize
==> Summary
?  /usr/local/Cellar/php@7.2/7.2.34_4: 515 files, 75MB

如果想要切换7.2,只需要执行:

brew-php-switcher 7.2

然后按照要求写入.bash_profile即可。

本文最后更新于2021年9月6日,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!