Search code examples
phpmacosapachemacos-monterey

Apache can’t load php7 module on macOS - Monterey


I have installed [email protected] with brew on my Mac. Everything works fine but when I open localhost in Safari, it just show me php codes.

My code :

<?php
echo "Hello world";

and the result on localhost : result on localhost

I think this is my problem. I added this line to http.conf file and nothing worked in localhost (Safari can't connect to server "localhost").

LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so

Solution

  • 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/[email protected]/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/[email protected]/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/[email protected]/lib/httpd/modules/libphp7.so "<Your Name>"
    

    Then restart the apache.