Install PHP on macOS using brew

Follow the steps for Apache and PHP on macOS Big Sur then:

brew install php
sudo nano /etc/apache2/httpd.conf

Comment:

LoadModule php7_module libexec/apache2/libphp7.so

Add the following lines:

LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

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

Configure DirectoryIndex section as follow:

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Then:

brew services restart php
sudo apachectl restart