Recently I realized that the latest macOS Sierra 10.12 upgrade also updated Apache and PHP to newer revisions (2.4.23 and 5.6.24 respectively), but reverted to the default configurations. My previous post on Enabling Apache and PHP on OS X 10.11 needed a slight change too.

Be careful, many changes here are done as root.

From Terminal, edit the Apache configuration as root (password is the same as your login password).

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

Uncomment these lines (remove the #) and save.

LoadModule cache_module libexec/apache2/mod_cache.so
LoadModule expires_module libexec/apache2/mod_expires.so
LoadModule headers_module libexec/apache2/mod_headers.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

By default, Apache runs as the user _www and serves pages from /Library/WebServer/Documents/. This can be changed to your user (replace [user] below with your username) and you own directory. In the same httpd.conf look for these sections and edit the contents (the first 3 items already exist, so just edit, but the last <Directory "/Users/[user]/Sites"> needs to be added):

<IfModule unixd_module>
User [user]
Group staff
</IfModule>

DocumentRoot "/Users/[user]/Sites"

<Directory />
 AllowOverride All
</Directory>

<Directory "/Users/[user]/Sites">
 AllowOverride All
</Directory>

Under macOS Sierra, this change is also needed, otherwise PHP files will not be executed:

<IfModule mime_module>
 AddType application/x-httpd-php .php
 AddType application/x-httpd-php-source .phps

Next, create the default php.ini configuration and edit it as required, e.g. opcache.enable=1

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

Create the Sites directory, and to quickly test it, create a small PHP test file. And finally, start or restart Apache, then open your browser to localhost/phpinfo.php to make sure all is hunky-dory:

cd /Users/[user]
mkdir Sites
echo "<?php phpinfo();" > Sites/phpinfo.php
sudo apachectl start

If Apache is already started, then restart it with:

sudo apachectl -k restart

If Apache is not automatically started, then you may need to run this to auto-start Apache when the machine is rebooted (although, I didn't need this):

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

To remove the startup, run the command again replacing load with unload.

Related Posts

Newer Post Older Post