How to use the php that brew installed?

97,452

Solution 1

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

Solution 2

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 [email protected]
$ echo 'export PATH="/usr/local/opt/[email protected]/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

Solution 3

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

Solution 4

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'

Solution 5

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 [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

source ~/.profile

~/.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:

~/.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:

~/.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'
Share:
97,452
kramer65
Author by

kramer65

Updated on November 20, 2021

Comments

  • kramer65
    kramer65 over 2 years

    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.

    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?

    All tips are welcome!

  • kramer65
    kramer65 over 10 years
    Awesome! You saved my day! Thanks a million!
  • simPod
    simPod over 8 years
    For PHP 5.5 it's /usr/local/opt/php55/libexec/apache2/libphp5.so which is even better as it doesn't change with updating PHP 5.5 version.
  • Metropolis
    Metropolis about 7 years
    I really want to be able to find my .so file this way, but when I do brew info php71, it does not give me back the same lines that you have here. I get a bunch of other info, but not where this file is. I wonder if I may have a different version of brew.
  • Metropolis
    Metropolis about 7 years
  • Ralpharoo
    Ralpharoo about 7 years
    Thanks AnthonyT - spot on
  • NextThursday
    NextThursday over 6 years
    should be accepted answer, as the /usr/local/opt/phpxx/ is a simlink to the current version and thus update-safe -- and not like in the other version which points to current version directly
  • Ivan Proskuryakov
    Ivan Proskuryakov about 6 years
    A symbolic link could be used, like ln -s /usr/local/Cellar/php71/7.1.11_22/bin/php /usr/bin/php
  • 0yeoj
    0yeoj almost 5 years
    Just amazing! Thank you. I'm updating php7.1 to php7.2
  • Lulu
    Lulu almost 4 years
    Confirmed to work with other versions as well, just replace [email protected] with whatever version you want to install
  • David Gay
    David Gay almost 4 years
    Looks like now brew update doesn't take package names (like php). Also, using $(brew --prefix) literally in the file didn't work for me. I had to manually substitute the actual path, so for me, the line was LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
  • Andy Keith
    Andy Keith over 3 years
    If you want to link Homebrew's PHP in a generic way so it works for future PHP versions, use: echo 'export PATH="/usr/local/opt/php/bin:$PATH"' >> ~/.bash_profile Homebrew syslinks this when you run $ brew link php
  • ina
    ina about 3 years
    Apparently the link needs to be to the specific version and not the general /usr/local/opt/php/lib/httpd/modules
  • JorgeLuisBorges
    JorgeLuisBorges over 2 years
    as a note the last line should be $ source ~/.zshrc (space removed)
  • Kay V
    Kay V over 2 years
    ah! good catch @JorgeLuisBorges. typo fixed.
  • Wirone
    Wirone over 2 years
    After brew upgrade my PHP disappeared, it was still "installed" though, so I hade to do brew reinstall php and it started to work again.
  • Vahid Amiri
    Vahid Amiri about 2 years
    for php 8.0 do brew link [email protected] --force