How to install Apache, PHP (with xdebug) and MySQL on a Mac using Homebrew, a software package manager for MacOS X. The list of available software packages can be found at https://formulae.brew.sh, although there are more "taps" available at other repositories.
The Mac already comes with Apache and PHP installed, but if you want to be able to upgrade to more recent versions you can use Homebrew to install these. Also, starting with the OS Sierra, the built-in Apache is more difficult to use due to some missing scripts. MacOS X doesn't include MySQL.
Before Installing Homebrew
- Remove MacPorts or Fink, if these are installed
- Macports
Copy to clipboardsudo port -f uninstall installed sudo rm -rf /opt/local sudo rm -rf /Applications/DarwinPorts
- Fink (untested)
Copy to clipboardfink remove --recursive daemonic xinitrc sudo rm -rf /sw
- Macports
- Make sure Xcode and the Command Line Tools of Xcode are installed
- Depending on your version, command line tools can be installed in two ways:
- In Xcode, go to Preferences > Downloads > Command Line Tools and click install
- In Terminal, type
xcode-select --install
and then click install on the pop-up
- Depending on your version, command line tools can be installed in two ways:
Install Homebrew
- Type the following in Terminal to install Homebrew:
Copy to clipboard/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Run the command to check your installation:
Copy to clipboardbrew doctor
- There may be several errors but Homebrew has pretty good explanations of how to fix them
- Make sure brew is up to date (if brew was already installed):
Copy to clipboardbrew update
Install Apache
Enter the following in Terminal:
- Turn off built-in Apache
Copy to clipboardsudo apachectl stop sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
- Install Homebrew Apache
Copy to clipboardbrew install httpd
- Set up Apache to autostart upon reboot
Copy to clipboardbrew services start httpd
- Test that Apache is working by pointing your browser to
http://localhost:8080
. You should see a simple header that says "It works!".
- Troubleshooting tips if the browser can't connect
Copy to clipboard#check if server is up ps -aef | grep httpd #restart Apache brew services restart httpd # watch Apache log tail -f /usr/local/var/log/httpd/error_log
Configure Apache
Make the following changes in /usr/local/etc/httpd/httpd.conf
:
- Change the listen port from the brew default of
Listen 8080
toListen 80
- Change the document root
Copy to clipboard#if using the global root DocumentRoot "/Library/WebServer/Documents" #if using the user home directory DocumentRoot "/Users/username/Sites"
- Set the Directory tag to the same root
Copy to clipboard#if using the global root <Directory "/Library/WebServer/Documents"> #if using the user home directory <Directory "/Users/username/Sites">
- Set AllowOverride within the same Directory tag to All
Copy to clipboardAllowOverride All
- Enable rewrite module (disabled by default)
- Uncomment the line that says
LoadModule rewrite_module libexec/mod_rewrite.so
- Uncomment the line that says
- Change User and Group
- By default these are set to
daemon
. Change to_www
or actual user and group names to avoid permission problems.
- By default these are set to
- Set the server name:
ServerName localhost
Install PHP
- In Terminal, enter the following commands:
Copy to clipboard# install php (dependencies will also be installed) # this will install latest version # use @ to install a specific version, like php@8.1 brew install php # if using imagick php extension, first install imagemagick itself using homebrew, then install the php extension using pecl brew install imagemagick pecl uninstall -r imagick && pecl install imagick # see below regarding installing xdebug
Deprecated versions of PHP may not be available in the Homebrew core formulae, in which case an external "tap" may be used as follows:Copy to clipboard# Access the tap brew tap shivammathur/php # then install the desired php version brew install shivammathur/php/php@7.1 # to see a list of available php versions with installed versions highlighted (optional) brew search php
- Make changes to Apache configuration at
/usr/local/etc/httpd/httpd.conf
to use newly installed PHP versionCopy to clipboard# Add php load module command LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so # Set directory indexes to PHP by changing the DirectoryIndex options <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> # Add FilesMatch directive <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Install Xdebug PHP Extension
This section assumes you've installed PHP in the previous step.
- In Terminal, enter the following command:
Copy to clipboardpecl uninstall -r xdebug && pecl install xdebug
- In the php.ini file, add the following lines:
(look at phpinfo to see where the file is located)Copy to clipboardxdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.remote_handler="dbgp" xdebug.remote_host="localhost"
- Restart Apache by entering the following command in Terminal (you may be prompted for a password):
Copy to clipboardbrew services restart httpd
Install MySQL
- In Terminal, enter the following command:
Copy to clipboardbrew install mysql
- Follow Homebrew instructions for launching
- Enter
brew services restart mysql
following the other Homebrew instructions (but before setting runningmysql_secure_installation
): - If you have problems restarting the mysql server, try restarting your computer.
- Enter
- Set a password to avoid errors. Enter the following and follow the prompts:
Copy to clipboardmysql_secure_installation
Check versions
- In Terminal, enter one of the following commands (lines beginning with # are just descriptions):
Copy to clipboard#list versions of all programs installed by home-brew brew list --versions or #show the version of a single program brew list --versions mysql
Update programs
- In Terminal, enter the following command choices (the purpose for each command follows the # symbol - you only need to type the part before the #):
Copy to clipboardbrew update #first update homebrew itself brew outdated #list what is outdated brew upgrade #upgrade everything brew upgrade mysql #or upgrade a specific program
Remove old versions of programs
- By default Homebrew leaves the old version intact when updating program. To remove them, type the following in Terminal:
Copy to clipboard# to see what would be removed (without removing anything) brew cleanup -n # to remove all old versions of all programs brew cleanup # to remove the old versions for just one program brew cleanup mysql
Uninstall programs
- For php, each new version branch (moving from 7.0 to 7.1, for example) is a separate program in homebrew. Unlinking the program as shown above still keeps it on your system so you need to perform an uninstall to remove it
Copy to clipboardbrew uninstall php@7.0