2

I have installed php@7.4 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/php@7.4/lib/httpd/modules/libphp7.so
Alirezakvr
  • 75
  • 1
  • 11
  • 1
    Did you also activate php in Apache? Apache needs to know you whant to use php – Baracuda078 May 31 '22 at 13:57
  • I think this is my problem. I add this line to http.conf file and nothing works in localhost `LoadModule php_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so` @Baracuda078 – Alirezakvr May 31 '22 at 14:05
  • Sorry I dont know if that is correct or not, I always used windows and the last couple years I only work from Linux and installing a PHP based webserver there is really a joy compared with windows and mac IMO – Baracuda078 May 31 '22 at 14:13
  • When you install PHP via homebrew it lists the things you need to do for Apache: Load the php module, set .php files to be handled by PHP, add index.php to the DirectoryIndex. Have you done all of those? – cOle2 May 31 '22 at 14:29
  • Yes, I did all. I think Module is the problem, because when I comment it, localhost works. But just works on html files. @cOle2 – Alirezakvr May 31 '22 at 14:40

1 Answers1

0

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.

Alirezakvr
  • 75
  • 1
  • 11
  • The above tutorial doesn't work. got stuck at "choose an identity that will sign your CA invitations" like other has commented – TomSawyer Jun 23 '22 at 14:38