php 如何使用brew安装的php?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20523183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:15:51  来源:igfitidea点击:

How to use the php that brew installed?

phpmacosapachehomebrew

提问by kramer65

On my mac I've got php installed and working fine. I recently wanted to install mcrypt, so I did so using brew. Although it seemed to install fine, it doesn't show up in my phpinfo(). So I think that the php that brew installed mcrypt in, isn't the php that apache uses.

在我的 Mac 上,我已经安装了 php 并且运行良好。我最近想安装 mcrypt,所以我使用 brew 安装。虽然它似乎安装得很好,但它没有出现在我的 phpinfo() 中。所以我认为 brew 安装 mcrypt 的 php 不是 apache 使用的 php。

Does anybody know how I can:

有谁知道我可以:

  1. check whether there is a difference between the php installed by brew and the php which Apache uses?
  2. make apache use the php that brew installed?
  1. 检查 brew 安装的 php 和 Apache 使用的 php 是否有区别?
  2. 让 apache 使用 brew 安装的 php?

All tips are welcome!

欢迎所有提示!

采纳答案by Manuel Pedrera

You have to make your Apache use the PHP that you just downloaded.

您必须让您的 Apache 使用您刚刚下载的 PHP。

  • Open your httpd.conf(mine is at /etc/apache2/httpd.conf) and look for the line that loads the PHP module, something like:

    LoadModule php5_module path/to/php

  • Then, make it point to the PHP that brew installed for you with mcrypt support. Mine was at this path. Yours can vary depending on the PHP version that you installed.

    /usr/local/Cellar/php54/5.4.21/libexec/apache2/libphp5.so

  • Finally you will need to restart your Apache server to load the new configuration:

    sudo apachectl restart

  • 打开您的httpd.conf(我的是在/etc/apache2/httpd.conf)并查找加载 PHP 模块的行,例如:

    LoadModule php5_module path/to/php

  • 然后,将其指向为您安装的 brew 支持 mcrypt 的 PHP。我的是在这条路上。您的版本可能因您安装的 PHP 版本而异。

    /usr/local/Cellar/php54/5.4.21/libexec/apache2/libphp5.so

  • 最后,您需要重新启动 Apache 服务器以加载新配置:

    sudo apachectl restart

回答by AnthonyT

Can't comment on stackoverflow yet due to my lack of experience but to add to the above answer is correct. Just an additional comment to find the correct path:

由于我缺乏经验,无法对 stackoverflow 发表评论,但添加到上述答案是正确的。只是一个额外的评论来找到正确的路径:

run:

跑:

brew info php54

or which ever version u have installed and it will show you the path:

或者你安装了哪个版本,它会告诉你路径:

To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php5_module    /usr/local/opt/php54/libexec/apache2/libphp5.so

回答by Kay V

According to the contributors of the Homebrew php formula...

根据 Homebrew php 公式的贡献者...

The contributors of the Homebrew php formula give the following instructions. The exact instructions reproduced here install php7.2. Substitute the php version you need.

Homebrew php 公式的贡献者给出了以下说明。此处复制的确切说明安装 php7.2。替换您需要的 php 版本。

(Avoid "special" ways of accomplishing your objective; they are often problematic. "Official" approaches are more likely to give you a predictable, maintainable setup.)

(避免实现目标的“特殊”方法;它们通常有问题。“官方”方法更有可能为您提供可预测、可维护的设置。)

$ brew update php // get the latest homebrew php packages
$ brew install [email protected]
$ brew link [email protected] // create an alias to this keg-only version; see comments output during installation
$ echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.bash_profile // add the alias to your path; see comments output during installation
$ source ~/.bash_profile // reload .bash_profile to use the new settings immediately

The contributors of the formula also provide the following instructions for enabling PHP in Apache:

公式的贡献者还提供了以下在 Apache 中启用 PHP 的说明:

To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module $(brew --prefix)/opt/php/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:
$(brew --prefix)/etc/php/7.2/

要在 Apache 中启用 PHP,请将以下内容添加到 httpd.conf 并重新启动 Apache:
LoadModule php7_module $(brew --prefix)/opt/php/lib/httpd/modules/libphp7.so

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

最后,检查 DirectoryIndex 包含 index.php
DirectoryIndex index.php index.html

php.ini 和 php-fpm.ini 文件可以在以下位置找到:
$(brew --prefix)/etc/php/7.2/

These instructions for enabling PHP in Apache appear in stdoutwhen you install php. Alternatively in Terminal use brew info phpor visit the Homebrew PHP formula page

这些在 Apache 中启用 PHP 的说明会在stdout您安装 php 时出现。或者在终端使用brew info php或访问Homebrew PHP 公式页面

回答by Xeoncross

I would create an alias to it so you don't disturb the system PHP install.

我会为它创建一个别名,这样您就不会打扰系统 PHP 安装。

brew info php71

Brew installs into /usr/local/Cellarso you can add the following to your ~/.bash_aliasor ~/.profile.

Brew 安装到其中,/usr/local/Cellar因此您可以将以下内容添加到您的~/.bash_alias~/.profile.

alias php7='/usr/local/Cellar/php71/7.1.10_21/bin/php'

回答by Mike

brew install phpinstalls php 7.3 at the moment, versions below are keg-only

brew install php目前安装 php 7.3,以下版本仅用于桶

You can make aliases for versions below by adding this to:

您可以通过将其添加到以下版本来为以下版本创建别名:

~/.profile

~/.profile

alias [email protected]='$(brew --prefix [email protected])/bin/php'
alias [email protected]='$(brew --prefix [email protected])/bin/php'
alias [email protected]='$(brew --prefix [email protected])/bin/php'
alias [email protected]='$(brew --prefix [email protected])/bin/php'

~/.bashrc

~/.bashrc

source ~/.profile

~/.zshrc

~/.zshrc

[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

Then you can:

然后你可以:

[email protected] -v
[email protected] -v
[email protected] -v
[email protected] -v

If you use composer and the platform php is not set in your project then this can be handy:

如果你使用 composer 并且你的项目中没有设置平台 php 那么这会很方便:

~/.profile

~/.profile

alias [email protected]='[email protected] $(which composer)'
alias [email protected]='[email protected] $(which composer)'
alias [email protected]='[email protected] $(which composer)'
alias [email protected]='[email protected] $(which composer)'

If you use artisan a lot (artisan maps to php which is 7.3) then this can be handy:

如果您经常使用 artisan(artisan 映射到 php,即 7.3),那么这会很方便:

~/.profile

~/.profile

alias [email protected]='[email protected] artisan'
alias [email protected]='[email protected] artisan'
alias [email protected]='[email protected] artisan'
alias [email protected]='[email protected] artisan'

回答by echo

Try: brew link php71 --forceto use brew specific php version. It worked for me.

尝试:brew link php71 --force使用 brew 特定的 php 版本。它对我有用。