Running PHP on Mac M1
Mac is stupid for development. I got a assignment from a friend for writing his PHP site. He had contracted a PHP programmer for cheap, and just like any cheap programmer the code of the site was bad, non reusable, inconsistent UI and UX were there and changes made added to complexity.
The code was full of inconsistencies and trailing white spaces. I use Ruby for creation of websites, but my friend had bought a cheap hosting provider and was unwilling to change, that hosting provider gave only PHP on his servers for dirt pricing. So I had to do with PHP on Mac.
Trying out XAMP
XAMP is a wonderful tool to run AMP stack (Apache, Maria DB, PHP) on any OS. I first installed on it on my laptop and tried. To my horror for some reason PHP files got downloaded rather than it being executed on server and HTML sent back. Mac sucks in development, the support was bad it took very long time to figure it out. You see it may be due to XAMP using PHP 8 and I later found out that my Mac had inbuilt PHP 7 which could be the cause of the issue. These mismatches means some how XAMP PHP won’t work on Mac.
If this kind of problem occurred on Ubuntu GNU/Linux, then it would be going to a forum and a 20 minute fix, because this happened in mac, it took nearly a day to find the solution. Pheeew!! Mac sucks for development, its a closed box and good enough developers should not use it.
So now knowing that XAMP won’t work properly, I switched to Mac’s internal installation. thankfully XAMP had an uninstall script which I triggered to remove it.
Using Mac’s Apache and PHP
Okay, Mac has inbuilt Apache and PHP. Why a company which builds opaque OS and Processors that does not disclose about it to developers includes these software? I don’t know. So start Apache like this:
$ apachectl start
Launch localhost
in your browser and you should see this
Now edit this file:
$ sudo vi /etc/apache2/httpd.conf
In that find this one
LoadModule php7_module libexec/apache2/libphp7.so
and uncomment it if its commented, if you can’t find it add it at last. Now in finder locate /Library/WebServer/Documents/
and right click on it and click Get Info, it must show something like this:
In that I have added myself and given me Read and Write permissions. Now restart Apache like this:
$ apachectl restart
Place your PHP files in /Library/WebServer/Documents/
and access it in your browser.
If you are a Mac user, my only advise is don’t be a stupid, switch to GNU/Linux.