Install PHP on Apple Silicon Macbook Pro

I recently went through the process of install PHP on macOS Monterey on my MackBook Pro M1 (Apple Silicon), the installation of PHP itself is easy, the codesigning of the PHP module took a little while to figuring out, I thought it would be a good idea to document the walk-through on how I did it.

Install PHP on macOS Monterey

After 10 years of using my MacBook Pro 2011 edition, I finally bought an Macbook Pro M1 recently. Most of the software that I'm using already has the version that supports Apple Silicon. So what I did is do a full backup-and-restore to move all my data and applications over to the new MacBook Pro, and delete those applications that has the newer Apple Silicon version and then install the new version. For those that are not supported by by Apple Silicon, the Rosetta 2 (https://support.apple.com/en-us/HT211861) helps to translate the older version so that it will be able to run on Apple Silicon. This went well for most of applications, including my MSQL installation and all the databases, except for PHP.

Apple pre-packaged Apache 2.4 in macOS Montery 12. However Apple removed PHP since macOS Monterey 12. In case you need PHP, you will have to install it yourself. Most of the online tutorials or walk-through suggesting of installing the complete pre-packaged Appache-PHP-MYSQL stacks, but I already has the MySQL up and running, and the built-in Apache 2.4 is okay for me, so I only need to install PHP.

Install Homebrew

Although it is possible to download the PHP source code and build it on the machine, but it is much easier to install it using Homebrew. Surprisingly for past 10 years of using my previous MacBook Pro 2011, I never felt the need to use Homebrew, so the first step is to install Homebrew. To download Homebrew, run

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Homebrew will be installed on /opt/homebrew/bin directory, it is therefore need to include the path in the $PATH by adding the follow lines into the .bash_profile file.

# add homebrew path
PATH="${PATH}:/opt/homebrew/bin"
export PATH

Install PHP

With Homebrew, you can choose to install which version of PHP. I choose to install PHP 8.2PHP 7.4 in order to be compatible with my existing applications that I'm running.

Update (March 2023):
A few months after installing PHP 7.4, homebrew stopped the support of PHP7.4 in its tap, I eventually has to re-do the whole process to install PHP8.2 (the latest available at homebrew at this update). All commands are updated based on PHP@8.2 installation.

brew install php@8.2

Link the PHP version (this line may not needed).

brew link --overwrite --force php@8.2

Configure httpd.conf

We are going to use the pre-installed Apache, the configuration file for the Apache is in /etc/apache2. Make a copy of the httpd.conf file and edit the httpd.conf.

cd /etc/apache2
cp httpd.conf httpd.conf.default
sudo nano httpd.conf

Search for #Listen 12.34.56.78:80, add a line below as Listen 127.0.0.1:80.

#Listen 12.34.56.78:80
Listen 127.0.0.1:80

Search #PHP was deprecated, add a line below to add back PHP module.

#PHP was deprecated in macOS 11 and removed from macOS 12
LoadModule php_module /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so

Search for #ServerName www.example.com:80, and add a new line below

#ServerName www.example.com:80
ServerName localhost

Search for dir_module, insert index.php into the DirectoryIndex index.html as show below:

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

Add the following lines right after the <IfModule> block.

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

Save the httpd.conf file.

If you restart the Apache server now, you are likely get an error No code signing authority for module at /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so specified in LoadModule directive. We need to certify that the php module is safe to run as a system share module.

Create a Code Signing Certificate

- Create a Self-Signed Certificate Authority

  • To Create a certificate, launch Keychain Access app (The Keychain Access app can be found in Application -> Utilities folder), click on Keychain Access > Certificate Assistant > Create a Certificate Authority from Menu bar.

  • Fill the name field (I called mines as Henry's CA) for your certificate authority. In the Identify Type and User Certificate fields, select Self-Signed Root CA and SSL Server from the options respectively. Click on Create, and the certificate authority is created.

  • But the certificate authority you just created is marked red, with the message reading: "this root certificate is not trusted". To resolve this, double click on the newly created certificate. A window pops up, click on the collapsed Trust panel to expand it.

  • You will find the When using this certificate: field. From the options, select the Always Trustoption and save the changes.

- Create a Code Signing Certificate

  • Click on Keychain Access > Certificate Assistant > Create a Certificate from Menu bar.

  • A form pops up. The name field you can be anyname you prefer (I named it with my name 'Henry', we will need to use this name later). In the Identity Type and User Certificate fields, select Leaf and Code Signing from the options respectively. Also, check the Let me override defaults box just below.

  • In the next page of the form, fill the Email field. Then click on Continue.

  • From the  Choose An Issuer form that pop-up, select your newly created Certificate Authority and click Continue. Click on the next several pages until the last page where a Create button is shown. Click on Create to complete the creation of Code Signing Certificate.

Code Signing the PHP module

Run the following command from terminal:

security find-identity -v -p codesigning

This will show something like:

henrycheung1@MacBook-Pro ~ % security find-identity -v -p codesigning
  1) CFB70BB9B2067DF1FE28B8C0FAD254D6D3800E43 "Henry"
     1 valid identities found

We can now sign the php module with the code signing certificate.

codesign -f -s CFB70BB9B2067DF1FE28B8C0FAD254D6D3800E43 /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so

We need to go back and edit the httpd.conf again to append the name of the code signing certificate to the line where the PHP shared module is being loaded.

LoadModule php_module /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.so "Henry"

WARNING:
Everytime when macOS upgrade the OS (e.g. from macOS Monterey to Ventura), you will have to do the code signing again or else the PHP will stop working. The upgrade of macOS also overwrite the http.conf, it is therefore recommended to keep a copy of your modified httpd.conf by making a backup:

sudo cp httpd.conf httpd.conf.backup

Restart Apache

sudo apachectl -k restart

Test PHP page

Create a php file in the localhost home directory /Library/WebServer/Documents so that it can be access on the browser via localhost.

echo '<?php echo phpinfo(); ?>' > /Library/WebServer/Documents/index.php

7 comments by readers

  1. 💥 OMG. It actually works. 🔥 Thank you so much for these instructions. ✨

    If you have to choose between local and system (in the process of code signing), choose local, that worked for me.

    It actually loaded libphp.so but then got the next issue… PHP is installed with x86_64 rather than arm64. PHP works at the command line under Rosetta, but not as an .so library.

  2. wow, thanks a million for putting all this out there. you sure have to be patient/careful to get it all working, but it does indeed work!

    THANK YOU

  3. A few editorial comments to make some things clearer. However, I was happily able to get it to run.

    On the line where it says “Search for dir_module”, it should say “insert” rather than “inser”

    Where it says “Add the following lines right after the block.”, it should probably say “Add the following lines right after the block for dir_module.” There are several IfModule blocks in httpd.conf.

  4. Wow this made my day – thank you so much! Upgrading from 2015 intel MacBook Pro to 2023 silicon M2 MacBook Pro was easy. Thank you very muchly

  5. I’ve found a tool called ServBay.dev can provide much easier way for PHP developers, especially the beginners.
    It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. Users can run multiple versions of PHP instances all at once. Trust me, it’s made my PHP dev life a whole lot smoother. Might be worth a shot for you to check it out!

  6. I recently upgraded my Macbook from Catalina to Sonoma, and then found that one of my Apache-PHP based apps stopped working. I spent 3 whole days trying to fix it by following several other posts but still could not make it work. I have frustrated so much and was almost ready to give up, but I thought I would make another last try, hoping for a miracle to happen. Wow – Miracle did happen! I am sure this high-quality post has helped save tons of time for many others, and I am very grateful that the author has done a huge favor to the community! Thank you very much, the author, for your great contribution and willingness to help others!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.