Search code examples
phpapache2homebrewmacos-monterey

Enable php on mac monterey


I try to install php on a mac M1 monterey, but this simple code is not interpretated :

<?php phpinfo(); ?>

I installed php with brew

brew install php
brew link php

I signed the libphp module

codesign --sign "certificate" --force --keychain ~/Library/Keychains/ /opt/homebrew/Cellar/php/8.1.2/lib/httpd/modules/libphp.so

Should I use the dynamic link instead ?

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

Loading the module in the apache2.conf file

LoadModule php_module /opt/homebrew/Cellar/php/8.1.2/lib/httpd/modules/libphp.so "certificate"

enable php page to be view in the apache2.conf file

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.php
</IfModule>

result of which php

/opt/homebrew/bin//php

Is the double slash normal here ?

I can access the index.php of my web directory but php is still not executed.

Thanks


Solution

  • As I mentioned in the comments you probably done most the steps but this guide I found has one last step it mentions for PHP. Here is the link:

    https://wpbeaches.com/updating-to-php-versions-7-4-and-8-on-macos-12-monterey/

    Also I found a lot of my issues went away when I started using homebrew Apache instead.

    I am glad the guide was able to help you get it working.

    For anyone else who is stuck, here is the relevant section from the guide:

    PHP 8 and macOS Apache

    One extra step is needed for PHP 8 and macOS bundled Apache:

    sudo nano /etc/apache2/httpd.conf
    

    Add the new PHP 8 and comment out the old one.

    LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so
    

    Go to the end of the file and add:

    <FilesMatch .php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    

    Restart Apache