Configuring MAMP without MAMP
Hats off to Andy Miller for posting an excellent tutorial that I basically robbed blind here. You can find his post here.
-
Install Homebrew
-
Install Apache
Apache is pre-installed on Mac OS Sonoma, but not configured to run (why?). But PHP is not part of the pre-installed version, and since I want all of this to play nice with Homebrew, I'm going to follow this advice and install it via
brew.$ brew install httpdHomebrew starts it when it is installed, so you should now be able to open a web browser to see the 'It works!' default page.
$ open http://localhost:8080Apache error log is here:
$ tail -f /opt/homebrew/var/log/httpd/error_logUpdate the configuration:
$ vi /opt/homebrew/etc/httpd/httpd.confListen on port 80:
# grt 2024-07-08 listen on port 80 #Listen 8080 Listen 80Give it a
ServerName:# grt 2024-07-08 set server name #ServerName www.example.com:8080 ServerName localhostChange User and Group so Apache runs as you (eliminates the need to run as sudo):
# grt 2024-07-08 change user/group that apache will run as me #User _www #Group _www User geoff Group staffEdit
DocumentRootandDirectoryto serve from yourSitesdirectory (created next):# grt 2024-07-08 change document root #DocumentRoot "/opt/homebrew/var/ww" DocumentRoot "/Users/geoff/Sites" #<Directory "/opt/homebrew/var/www"> <Directory "/Users/geoff/Sites">Change
AllowOverrideto allow all:# grt 2024-07-08 allow all #AllowOverride None AllowOverride AllEnable
mod_rewriteby removing the comment hash from in front:# grt 2024-07-08 load necessary modules LoadModule rewrite_module lib/httpd/modules/mod_rewrite.soAdd a Sites directory and create a test page:
$ mkdir ~/Sites $ echo "<html><body><h1>My Sites Folder</h1></body></html>" > ~/Sites/index.htmlRestart Apache and check your work:
$ brew services restart httpd $ open http://localhost -
Install PHP
Use Homebrew:
$ brew install phpCheck version:
$ php --version PHP 8.3.9 (cli) (built: Jul 2 2024 14:10:14) (NTS) Copyright (c) The PHP Group Zend Engine v4.3.9, Copyright (c) Zend Technologies with Zend OPcache v8.3.9, Copyright (c), by Zend TechnologiesTo install a particular version (the above installs the current stable version):
$ brew install php@8.2This version is not linked, so running
php --versionwill not reflect the version just installed. To do this, you need to unlink/relink to the correct version:$ brew unlink php && brew link php@8.2Some blog posts say you must use overwrite and force as such (but I did not find this to be the case):
$ brew unlink php && brew link --overwrite --force php@8.2Whatever the case, now the new version will be active:
$ php --version PHP 8.2.21 (cli) (built: Jul 2 2024 12:51:54) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.21, Copyright (c) Zend Technologies with Zend OPcache v8.2.21, Copyright (c), by Zend TechnologiesTo switch back:
$ brew unlink php@8.2 && brew link phpWhen installing PHP, Homebrew kindly provides instructions on configuring Apache, which I followed here.
Edit the configuration file:
$ vi /opt/homebrew/etc/httpd/httpd.confGo to the end of the 'LoadModule' section, and add PHP modules below the
rewrite_moduleadded previously:# grt 2024-07-08 load necessary modules LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so #LoadModule php_module /opt/homebrew/opt/php@8.2/lib/httpd/modules/libphp.soAdd
FilesMatchdirective below Load Modules section or belowIfModulesection (see next) like so:# grt 2024-07-08 add FilesMatch for PHP <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>Find
DirectoryIndexand change as follows:<IfModule dir_module> # grt 2024-07-08 add index.php #DirectoryIndex index.html DirectoryIndex index.php index.html </IfModule>Create a PHP test page:
$ echo "<?php phpinfo(); >?" > ~/Sites/index.phpRestart Apache and and check your work:
$ brew services restart httpd $ open http://localhost/index.phpPer the article referenced above, if you want to install more versions of PHP than are available via the Homebrew Core packages, you may want to use shivammathur instead:
~ $ brew tap shivammathur/phpI uninstalled the previously installed versions of PHP from core and used this tap in order to install PHP 5.6. Same basic steps:
~ $ brew install shivammathur/php/php@5.6Pay close attention to the notes provided, as the specifics of the
LoadModulestatement used in the Apache configuration vary slightly depending on which version you are installing. If you need those instructions repeated:~ $ brew info shivammathur/php/php@5.6There are a few PHP configurations you need to set. In particular:
date.timezone = America/Chicago -
Install MariaDB
$ brew install mariadb $ brew services start mariadb $ sudo /opt/homebrew/bin/mysql_secure_installation $ brew install tableplus