How to use the php that brew installed?

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

Leave a Comment