Install Apache and PHP on a Mac

Apple + PHP

Overview

Apple macOS comes with Apache and PHP already installed, however Apache and using PHP with it is not enabled by default. This tutorial provides an overview of some of the key tasks related to enabling Apache and PHP; however the needed steps for each version of macOS can vary greatly so you’ll likely want to visit many pages on this topic.

Quick Tip

Because PHP is already installed you can use the PHP Built-in Web Server for local development and then there is no need for a complex Apache setup. For info on how to do this see Use Visual Studio Code for PHP Development or Use GitHub Atom Editor for PHP Development.

Recommended Apache, PHP, and MySQL Install Resources for macOS

Opening Terminal

Terminal is commonly used for Mac Development. To open it search for “terminal” in Spotlight.

Open Terminal

 

From terminal you can start Apache using the following command sudo apachectl start. Once entered you will be prompted for a password. Using this command and enabling PHP for Apache requires administrator rights. Once Apache is started you should be able to view http://localhost/ from a browser.

Start Apache

Editing Apache Config to enable PHP

As stated earlier you can use the built-in PHP Server by default however to enable PHP for Apache you have to edit the Apache Config file. The most common method of doing this is using a terminal-based editor such as nano. To edit Apache config with nano enter the command sudo nano /etc/apache2/httpd.conf.

Edit httpd.conf with nano

 

You will then see the file in terminal along with a list of command options for editing the file.

View httpd.conf with nano

 

The file will likely be over 500 lines in length so finding the lines to edit without searching can take a while. To search type [control + w] and then enter “php” and press [enter].

Search with nano

 

Depending upon the version of macOS you have installed you will see a line starting with either [#LoadModule php7_module] or [#LoadModule php5_module]. Remove the [#] character from the start of the line.

PHP Settings in httpd.conf

 

To save type [control + x] then type [y].

Save with nano

 

If you don’t want to use a terminal based editor you can use Visual Studio Code to edit the file. Visual Studio Code also provides syntax highlighting of the file. Once you save you’ll be prompted to [Retry as Sudo] and then you’ll need to enter your password.

VS Code Save-as Sudo

 

A file for each user has to be added/edited at [/etc/apache2/users/{user-name}.conf].

Apache2 User Config File

 

Depending on your environment you may also have to edit the [DocumentRoot] and [Directory] options in the [httpd.conf] file.

DocumentRoot in httpd.conf