如何使用酿造安装的 php?

在我的 Mac 上,我已经安装了 php,而且运行良好。我最近想要安装 mcrypt,所以我使用了 brew。虽然它看起来安装得很好,但是在 phpinfo ()中没有显示出来。所以我认为 Brew 安装了 mcrypt 的 PHP 不是 Apache 使用的那个。

有人知道我该怎么做吗:

  1. 检查通过 brew 安装的 php 和 Apache 使用的 php 之间是否有区别?
  2. 让 Apache 使用 Brew 安装的 php?

欢迎任何提示!

120988 次浏览

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

  • 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

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:

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

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

brew info php71

Brew installs into /usr/local/Cellar so you can add the following to your ~/.bash_alias or ~/.profile.

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

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

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

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

~/.profile

alias php@5.6='$(brew --prefix php@5.6)/bin/php'
alias php@7.0='$(brew --prefix php@7.0)/bin/php'
alias php@7.1='$(brew --prefix php@7.1)/bin/php'
alias php@7.2='$(brew --prefix php@7.2)/bin/php'

~/.bashrc

source ~/.profile

~/.zshrc

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

Then you can:

php@5.6 -v
php@7.0 -v
php@7.1 -v
php@7.2 -v

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

~/.profile

alias composer@5.6='php@5.6 $(which composer)'
alias composer@7.0='php@7.0 $(which composer)'
alias composer@7.1='php@7.1 $(which composer)'
alias composer@7.2='php@7.2 $(which composer)'

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

~/.profile

alias artisan@5.6='php@5.6 artisan'
alias artisan@7.0='php@7.0 artisan'
alias artisan@7.1='php@7.1 artisan'
alias artisan@7.2='php@7.2 artisan'

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

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

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

$ brew search php // since php can be installed by homebrew but be missing from your PATH, review the list of php versions available through homebrew; a checkmark next to a version indicates one is installed
$ brew install php@7.4
$ echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc // add the alias to your path (issues you are using zsh, the default now for macOS); see comments output during installation
$ source ~/.zshrc // reload . zshrc to use the new settings immediately

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

To enable PHP in Apache add the following to httpd.conf and restart Apache:

   LoadModule php_module /usr/local/opt/php/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/7.4/

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

As of 2021, all you need is

brew install php

then

brew link php

This will give you php 8.0 and setup your symlinks.