Enabling Apache and PHP7 on macOS Mojave 10.14


As in my previous guides for enabling Apache and PHP on Sierra 10.12 and El Capitan 10.11 before that, this post will update the instructions for macOS Mojave 10.14.2 which comes with Apache 2.4.34 and PHP 7.1.19.

As usual, be extra careful - many changes here are done as root.

First, edit the httpd.conf configuration file as root, i.e.

sudo vi /private/etc/apache2/httpd.conf

Enable at least these two modules by uncommenting them (i.e. remove the initial #):

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so

To setup your user, find and edit:

<IfModule unixd_module>
  User [mylogin]
  Group staff

Further on, edit the DocumentRoot line and add a new Directory element. Of course you need to replace [mylogin] and [www] appropriately:

  • AllowOverride All - will allow a custom .htaccess
  • Require all granted - will give access to the virtual folder (this is new)
DocumentRoot "/Users/[mylogin]/[www]"

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


BTW, the default httpd.conf reads an external file other/php7.conf that sets up the appropriate mime types and extensions. Cool.

Next, create a default php.ini file and edit as needed:

cd /private/etc/
sudo cp php.ini.default php.ini

Finally, re-start httpd with:

sudo apachectl -k restart

To test, I create a small php file:

cd /Users/[mylogin]/[www]
echo "<?php phpinfo();" > phpinfo.php

Then, in Safari, visit localhost/phpinfo.php - you should see all sorts of PHP system info (and not an error).

Yes, PHP has been upgraded to php7. From Terminal you can confirm this with php -v:

PHP 7.1.19 (cli) (built: Aug 17 2018 20:10:18) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

And similarly for httpd, apachectl -version returns:

Server version: Apache/2.4.34 (Unix)
Server built:   Aug 17 2018 18:35:43