Notes of LAMP Setup on MacOS Monterey
Adam C. |

I am mainly using React/NodeJS those days for web development. But I still love PHP and use it for data processing all the time. I plan to work on a simple website and think a few PHP pages would be good enough. No need for a build (i.e., npm run build.) so I plan to set up the LAMP on my Mac (macOS Monterey.) 

Photo by Vladimir Fedotov on Unsplash

Apache

I just use the one came with MacOS. The configuration file is under /etc/apache2/httpd.conf

sudo apachectl configtest // test your configuration
sudo apachectl start/stop/restart

Brew PHP

Assume you have brew installed. Or check here:  https://mac.install.guide/homebrew/3.html

Then, use the command below to install PHP:

brew install php // install the latest version, by the writing it's php@8.1
brew install php@7.4 // if you want to install a specific version

If needed, you can switch the versions by:

brew unlink php@7.4 // remove
brew link php@8.1 --force --overwrite //add

To enable PHP in Apache add the following to httpd.conf and restart Apache:

// YES, THERE IS A "DENIAPP" ON THE FOLLOWING LINE 
// BECAUSE WE NEED A CODESIGN, SEE BELOW
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so DENIAPPS

NOTE: 

Mac Monterey and future Mac OS requires all application to be verified in order to run them on the OS. The php installed from brew is not a verified application and it has to be verified in order for it to run on Mac, without it being verified and loading it in apache, the apache server will fail to start with this error:
No code signing authority for module at /usr/local/opt/php/lib/httpd/modules/libphp.so specified in LoadModule directive.

To resolve this issue, you can use the codesign to sign the php module and then load it in the httpd.conf with authority.

Unfortunately, the process to fix this is so complicated, please check out here: https://www.codexpedia.com/apache-server/no-code-signing-authority-for-module-php-on-mac-monterey/

After that, will be a few more changes in https.conf

DirectoryIndex index.php index.html
....
<FilesMatch \.php$>
  SetHandler application/x-httpd-php
</FilesMatch>

The php.ini and php-fpm.ini file can be found in:

/opt/homebrew/etc/php/8.1/

To restart php after an upgrade:

brew services restart php

Or, if you don't want/need a background service you can just run:

/opt/homebrew/opt/php/sbin/php-fpm --nodaemonize

VHOST

If you're lucky, then you should be able to access your local LAMP at http://localhost, and see “It Works”.

But I like to have a VHOST, i.e., running multiple websites under one LAMP. Here is good tutorial you can check out: https://firxworx.com/blog/it-devops/sysadmin/setting-up-a-local-development-environment-on-macos-for-lamp-wordpress-projects/

My NOTES:

  1. make copies of updated httpd.conf & extrac/httpd-vhosts.conf, because after an OS update, those files will be overwritten. Although they create the copies for you, it's better to have your own copies.
  2. If you put virtualdocmentroot under your user account, as I did, for example: /Users/adam/Projects/Apache, then, you may make the user folder writable/executable, for example: chmod 755 /Users/adam, otherwise, you will get the permission issue when accessing your project webpage.

DNSMASQ

I used to use DNSMASQ for dynamic DNS mapping, but I found out that it does not work properly in macOS Monterey because:

Error: Running Homebrew as root is extremely dangerous and no longer supported.

As Homebrew does not drop privileges on installation you would be giving all

build scripts full access to your system.

Warning: dnsmasq must be run as root to start at system startup!

This means I have to manually start dnsmasq? Anyway, update the host file at /private/etc/hosts should be easy enough:

127.0.0.1 dnx.test

Then I can use http://dnx.test to access my local LAMP website. 

Missing anything? Like MySQL? I will try to complete this.