Every time Apple releases a new version of macOS, I have to go in and re-configure Apache with PHP the way I want it. So this time, I thought I’d automate the configuration changes with a single command.

Background

Prior versions of this article have more in-depth explanations, e.g.

No major change this time - macOS Catalina 10.15.1 ships with:

  • Apache 2.4.41 (Mojave 10.14 came with Apache 2.4.34)
  • PHP 7.3.9 (Mojave 10.14 came with PHP 7.1.19)

You can check the PHP version with php -v:

PHP 7.3.9 (cli) (built: Sep 10 2019 17:45:01) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.9, Copyright (c) 1998-2018 Zend Technologies

And the Apache version with apachectl -version:

Server version: Apache/2.4.41 (Unix)
Server built:   Oct 17 2019 18:04:28

Seems Apache now has the Multi-Processing Module mpm_prefork_module enabled by default.

Setup Script

My previous instructions still hold - edit the Apache configuration in /private/etc/apache2/httpd.conf:

  • Uncomment the the PHP and rewrite modules.
  • Change the DocumentRoot to one in your user folder (the default /Library/WebServer/Documents is read-only) and
  • Add make the corresponding change to the Directory section, e.g. in the example below,
    • [mylogin] is your login profile’s home directory
    • [www] is a folder serving the HTML and PHP files
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so

DocumentRoot "/Users/[mylogin]/[www]"

<Directory "/Users/[mylogin]/[www]">
  Require all granted
</Directory>

Danger: Running as superuser, with irreversible changes made! Be warned.

So, all you need is to run this:

  • The first regular expression replacement s/a/b looks for pattern a and replaces it with b. In this case, it matches a commented out line that starts with # and uncomments it (replaces with the first match, i.e. without the #).
  • The second s/ does the same for the Rewrite module.
  • The third replaces the default DocumentRoot and directory - remember to replace [mylogin] and [www] as appropriate.
  • -i creates a backup since sed will edit the file in-place.
sudo sed -e 's/^#\(LoadModule php7.*\)/\1/' \
  -e 's/^#\(LoadModule rewrite.*\)/\1/' \
  -e 's/\/Library\/WebServer\/Documents/\/Users\/[mylogin]\/[www]/' \
  -i .bak /private/etc/apache2/httpd.conf

This is basically all I need. Check and then re-start Apache with sudo apachectl -k restart.

You may use this tiny script echo "<?php phpinfo();" > /Users/[mylogin]/[www]/phpinfo.php to make sure PHP is enabled.

You may refer to my last post on this topic if you need to edit php.ini also.

Setup Script Part Deux

There are many other settings you may need to change depending on your requirements!

sudo sed -e 's/^#\(LoadModule php7.*\)/\1/' \
  -e 's/^#\(LoadModule rewrite.*\)/\1/' \
  -e 's/\/Library\/WebServer\/Documents/\/Users\/[mylogin]\/[www]/' \
  -e 's/AllowOverride None/AllowOverride All/' \
  -e 's/User _www/User [mylogin]/' \
  -e 's/Group _www/Group staff/' \
  -e 's/ServerRoot ".*"/ServerRoot "\/Users\/[mylogin]\/[www]"/' \
  -e 's/Require all granted/Require local/' \
  -i .bak /private/etc/apache2/httpd.conf

Happy PHP-ing.