Finally I found the solution. Gatekeeper in macOS ensures only verified applications can be executed and this is achieved by signing the application using codesign. Code signing has been optional on macOS Big Sur and prior but mandatory since macOS Monterey.
PHP module installed using homebrew is not signed, so you need to sign it first before it can be used.
I create a Certificate Authority for code signing and a code signing certificate before I can sign the PHP module using codesign utility.
Step 1 :
How to create Certificate Authority for Code Signing in macOS
Step 2 :
How to create code signing certificate in macOS
Step 3 : Locate location or path of PHP module from Apache's PHP LoadModule directive.
grep -nir "^loadmodule.*php" /etc/apache2 /etc/apache2/other/00-httpd.conf:4:LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
Step 4 : Sign PHP module using codesign with the code signing certificate name you've created.
codesign --sign "<Your Name>" --force --keychain /Library/Keychains/login.keychain-db /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so
Step 5 :
Open Apache configuration file with PHP LoadModule directive using your preferred text editor.
sudo nano /etc/apache2/other/00-httpd.conf
Step 6 :
Add code signing certificate name after module path in PHP LoadModule directive.
LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so "<Your Name>"
Then restart the apache.