Install Apache, PHP & MySQL on macOS Sierra

Setup a MAMP local development environment on the latest macOS


MAMP refers to a Mac, Apache, MySQL & PHP stack that is typically used for local web development.

Apache and PHP are both bundled with macOS and can easily be enabled by editing a few files. MacOS however does not ship preinstalled with MySQL but setup is easy. Follow along and you’ll have a MAMP local server environment running on your Mac in no time!

TLDR: If you are impatient or would like a quicker solution without touching the command line, a MAMP GUI installer program can be found here: https://www.mamp.info/en/


Let's Get Started!

First Xcode and it's command line tools will need to be installed, it can be found on the App Store here: https://itunes.apple.com/us/app/xcode/id497799835?mt=12

Once installed open terminal and run the following command to install the Xcode command line tools:

$ xcode-select --install

Apache and PHP have both been included with Mac OS but we will be using the Homebrew package manager so that we don't overwrite any system packages that will be lost upon operating system upgrade.

Let's make sure the default version of Apache is off and will not start automatically at startup.

$ sudo apachectl stop

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

Open terminal and run the following command to install Homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

The installation process will take a few minutes to finish. After Homebrew is complete, we're going to install the latest version of PHP and Apache. PHP comes pre-installed on MacOS. As of OS X Sierra, the php build included is PHP 5.6.x El Capitan ships with PHP 5.5.x and OSX 10.8 Mountain Lion ships with PHP version 5.3.

brew tap homebrew/php

brew install php71 --with-httpd24


Note: $ sudo su - will allow commands to be run in the root environment so you will not have to type your password after every command. You will be prompted to enter your user account password, after typing your password, submit by hitting enter. This step is not required if you would rather, just prefix some of the commands below with sudo and enter your password when prompted.


Sites Folder

Apache by default runs out of the /Library/WebServer/Documents/ directory. Ideally we want to store our websites in the 'Sites' folder under the user account. OSX Lion and later versions do not come with a default Sites folder, the Sites folder disappeared (along with the web sharing toggle in the System Preferences pane) beginning with OS X 10.8 Mountain Lion. If you need to create the sites folder manually run the following command mkdir ~/Sites or create the folder in finder under your user directory.

Apache Config

We will need to make some changes to Apache's settings so that it works well with this setup. We can do this by opening Apache's configuration file. Feel free to use vim here if you are 1337 h@x0r.

Otherwise, we'll use nano here for simplicity, open the apache config file with the following: $ nano /usr/local/etc/apache2/2.4/httpd.conf

In the examples below replace billyshall with your username.

Find DocumentRoot "/Library/WebServer/Documents" and change to DocumentRoot "/Users/billyshall/Sites"

Find <Directory "/Library/WebServer/Documents"> and change to <Directory "/Users/billyshall/Sites">

In the same block change AllowOverride None to AllowOverride All this will allow the use of .htaccess within the web server to allow overriding of the Apache config on a per directory basis.

Next we'll make sure our .php files are used as a directory index, find the following:

DirectoryIndex index.html

and change to:

DirectoryIndex index.php index.html

We'll also need to change the default user and group, if left default we will get an error: "Forbidden You don't have permission to access / on this server."

Find the following:

User _www
Group _www

and change to:

User billyshall
Group staff

Done! Save the config file with control O then press enter. Apache should be ready now, let's start it. sudo apachectl start

Now visit http://localhost to verify apache is working correctly. you should get a "It Works!" message.

Apache and PHP are now up and running!

Start Apache and PHP Automatically on Boot

brew services start homebrew/apache/httpd24

brew services start homebrew/php/php71


MySQL

MySQL no longer comes pre-installed on MacOS as of OS X Mountain Lion (10.8) as it did with previous versions or OS X

Download the latest stable version of mysql from: http://dev.mysql.com/downloads/mysql/

MySQL on MacOS has a GUI installer that walks you through the setup process.

After installation you will now have a new area in system preferences, here you can stop and start mySQL as well as choose to start mysql at startup.

Finished! You now have a MAMP environment setup on your Mac!


I build stuff, like Narien, the Useragent API and the Email API