geoffthompson

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.

  1. Install Homebrew

  2. 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 httpd
    

    Homebrew 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:8080
    

    Apache error log is here:

    $ tail -f /opt/homebrew/var/log/httpd/error_log
    

    Update the configuration:

    $ vi /opt/homebrew/etc/httpd/httpd.conf
    

    Listen on port 80:

    # grt 2024-07-08 listen on port 80
    #Listen 8080
    Listen 80
    

    Give it a ServerName:

    # grt 2024-07-08 set server name
    #ServerName www.example.com:8080
    ServerName localhost
    

    Change 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 staff
    

    Edit DocumentRoot and Directory to serve from your Sites directory (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 AllowOverride to allow all:

    # grt 2024-07-08 allow all
    #AllowOverride None
    AllowOverride All
    

    Enable mod_rewrite by removing the comment hash from in front:

    # grt 2024-07-08 load necessary modules
    LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
    

    Add a Sites directory and create a test page:

    $ mkdir ~/Sites
    $ echo "<html><body><h1>My Sites Folder</h1></body></html>" > ~/Sites/index.html
    

    Restart Apache and check your work:

    $ brew services restart httpd
    $ open http://localhost
    
  3. Install PHP

    Use Homebrew:

    $ brew install php
    

    Check 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 Technologies
    

    To install a particular version (the above installs the current stable version):

    $ brew install php@8.2
    

    This version is not linked, so running php --version will 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.2
    

    Some 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.2
    

    Whatever 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 Technologies
    

    To switch back:

    $ brew unlink php@8.2 && brew link php
    

    When installing PHP, Homebrew kindly provides instructions on configuring Apache, which I followed here.

    Edit the configuration file:

    $ vi /opt/homebrew/etc/httpd/httpd.conf
    

    Go to the end of the 'LoadModule' section, and add PHP modules below the rewrite_module added 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.so
    

    Add FilesMatch directive below Load Modules section or below IfModule section (see next) like so:

    # grt 2024-07-08 add FilesMatch for PHP
    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>
    

    Find DirectoryIndex and 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.php
    

    Restart Apache and and check your work:

    $ brew services restart httpd
    $ open http://localhost/index.php
    

    Per 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/php
    

    I 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.6
    

    Pay close attention to the notes provided, as the specifics of the LoadModule statement 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.6
    

    There are a few PHP configurations you need to set. In particular:

    date.timezone = America/Chicago
    
    
  4. Install MariaDB

$ brew install mariadb $ brew services start mariadb $ sudo /opt/homebrew/bin/mysql_secure_installation $ brew install tableplus