3.1 Install Apache with Homebrew

The latest macOS 10.12 Sierra comes with Apache 2.4 pre-installed, however, it is no longer a simple task to use this version with Homebrew because Apple has removed some required scripts in this release.

However, the solution is to install Apache 2.4 via Homebrew and then configure it to run on the standard ports (80/443).

Shutdown, and remove any auto-loading scripts for the built-in Apache.

sudo apachectl stop
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install Apache from Homebrew

brew install httpd24 --with-privileged-ports --with-http2

This step takes a little while as it builds Apache from source. Upon completion you should see a message like:

🍺  /usr/local/Cellar/httpd24/2.4.23_2: 212 files, 4.4M, built in 1 minute 45 seconds

Copy this path and use in the next step.

In this example the path was /usr/local/Cellar/httpd24/2.4.23_2. If you get a newer version, simply use that path in the next line:

sudo cp -v /usr/local/Cellar/httpd24/2.4.23_2/homebrew.mxcl.httpd24.plist /Library/LaunchDaemons
sudo chown -v root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo chmod -v 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

You now have installed Homebrew's Apache, and configured it to auto-start with a privileged account. It should already be running, so you can try to reach your server in a browser by pointing it at your localhost, you should see a simple header that says "It works!".

3.2 Troubleshooting Tips

If you get a message that the browser can't connect to the server, first check to ensure the server is up.

ps -aef | grep httpd

You should see a few httpd processes if Apache is up and running.

Try to restart Apache with:

sudo apachectl -k restart

You can watch the Apache error log in a new Terminal tab/window during a restart to see if anything is invalid or causing a problem:

tail -f /usr/local/var/log/apache2/error_log

If that doesn't work, check to ensure you have Listen: 80 in your /usr/local/etc/apache2/2.4/httpd.conf configuration file.

sublime /usr/local/etc/apache2/2.4/httpd.conf

Apache is controlled via the apachectl command so some useful commands to use are:

sudo apachectl start
sudo apachectl stop
sudo apachectl -k restart

The -k will force a restart immediately rather than asking politely to restart when apache is good and ready

3.3 Alias Apache Config

To make life easier, alias opening the apache folder with your favourite editor.

@ bashrc

# Edit apache in the "httpd.conf" file
alias config_apache="sublime /usr/local/etc/apache2/2.4/"

Reload bash

source ~/.bashrc

To quickly get to the apache folder

config_apache

3.4 Apache Configuration

Now make some configuration changes so Apache works better as a local development server.

Set the document root for Apache. This is the folder where Apache looks for files to serve.

By default, the document root is configured as /Library/WebServer/Documents. As this is a development machine, let's assume we want to change the document root to point to a folder in our own home directory.

To do this, we will need to edit Apache's configuration file.

config_apache

3.4.1 Document Root

Search for the term DocumentRoot, and you should see the following line:

DocumentRoot "/usr/local/var/www/htdocs"

Change this to point to your user directory where your_user is the name of your user account:

DocumentRoot /Users/your_user/Sites

3.4.2 Directory

You also need to change the <Directory> tag reference right below the DocumentRoot line. This should also be changed to point to your new document root also:

<Directory /Users/your_user/Sites>

Note: We removed the optional quotes around the directory paths as TextEdit will probably try to convert those to smart-quotes and that will result in a Syntax error when you try to restart Apache. Even if you edit around the quotes and leave them where they are, saving the document may result in their conversion and cause an error.

In that same <Directory> block you will find an AllowOverride setting, this should be changed as follows:

#AllowOverride controls what directives may be placed in .htaccess files.
#It can be "All", "None", or any combination of the keywords:
  #AllowOverride FileInfo AuthConfig Limit
 
AllowOverride All

Also we should now enable mod_rewrite which is commented out by default. Search for mod_rewrite.so and uncomment the line by removing the leading #:

LoadModule rewrite_module libexec/mod_rewrite.so

3.4.3 User & Group

Now we have the Apache configuration pointing to a Sites folder in our home directory. One problem still exists, however. By default, apache runs as the user daemon and group daemon.

This will cause permission problems when trying to access files in our home directory. About a third of the way down the httpd.conf file there are two settings to set the User and Group Apache will run under.

Change these to match your user account (replace your_user with your real username), with a group of staff:

User your_user
Group staff

3.4.4 Sites Folder

Now, you need to create a Sites folder in the root of your home directory.

mkdir ~/Sites

In this new Sites folder create a simple index.html and put some dummy content in it like:

echo "<h1>My User Web Root</h1>" > ~/Sites/index.html

Restart apache to ensure your configuration changes have taken effect:

sudo apachectl -k restart

If you receive an error upon restarting Apache, try removing the quotes around the DocumentRoot and Directory designations we set up earlier.

Any error involving apr_sockaddr_info_get() may be resolved by searching your httpd.conf file for the line ServerName localhost and removing the #.

Once you have done this, try resetting Apache again.

Pointing your browser to http://localhost should display your new message.

If you have that working, we can move on!