【问题标题】:How to avoid the preinstalled PHP on MacOS Big sur?如何避免 MacOS Big sur 上预装的 PHP?
【发布时间】:2021-05-18 15:26:05
【问题描述】:

我已经使用自制软件安装了 PHP 7.4 的更新版本。

但是,当我在命令行上运行 php -v 时,我得到:

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Dec 21 2020 21:33:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

如您所见,它仍在使用 Mac Os 自带的那个。

当我从 php 脚本运行 phpinfo 时,我可以看到它使用的是正确的,正如我在 Apache 上配置的那样,如下所示。

我需要做什么才能让命令行使用 brew 安装的 PHP 版本?

【问题讨论】:

  • 如果您想使用不同版本的php,请使用它。引用所需二进制文件的路径,或调整 PATH 以首先找到该二进制文件。
  • 试试这个用户的建议:stackoverflow.com/a/53785784/14612433Good Luck!

标签: php macos homebrew macos-big-sur


【解决方案1】:

安装软件包后,Homebrew 会向您显示一些有用的信息,您必须注意这些信息。

无论如何,之后您可以通过运行以下命令来访问该信息。

brew info php@7.4

如果您注意到下面有一个 注意事项 部分,它包含一些您必须手动执行的配置。

==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.4/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.4/

php@7.4 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.4 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

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


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

在您的情况下,您必须通过在终端中运行这些命令来将波纹管添加到您的 shell runco​​m 文件中,如果您使用的是 Zsh 它应该是 ~/.zshrc 如果您使用的是 Bash,应该是~/.bashrc

echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> path/to/runcom/file
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> path/to/runcom/file

这会将您由 Hombrew 安装的 PHP 添加到路径中,并且比 macOS 捆绑的 PHP 更喜欢它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2021-06-19
    • 2021-08-04
    • 2021-05-31
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    相关资源
    最近更新 更多