History: Mac Development System With Homebrew
Source of version: 68 (current)
Copy to clipboard
How to install Apache, PHP (with xdebug) and MySQL on a Mac using [http://brew.sh|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 "[https://docs.brew.sh/Interesting-Taps-and-Forks|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.
{REMARKSBOX()}These instructions are based on the excellent blog post at [https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions]. That page also has useful instructions on how to install and switch between multiple PHP versions.{REMARKSBOX}
!! Before Installing Homebrew
# Remove MacPorts or Fink, if these are installed
** Macports
++{CODE(colors="shell")}sudo port -f uninstall installed
sudo rm -rf /opt/local
sudo rm -rf /Applications/DarwinPorts{CODE}
++%%%
** Fink (untested)
++{CODE(colors="shell")}fink remove --recursive daemonic xinitrc
sudo rm -rf /sw{CODE}
+%%%
# 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
!! Install Homebrew
# Type the following in Terminal to install Homebrew:
+{CODE(colors="shell" theme="default")}/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"{CODE}
+%%%
# Run the command to check your installation:
+{CODE(colors="shell")}brew doctor{CODE}
** 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):
+{CODE(colors="shell")}brew update{CODE}
!! Install Apache
Enter the following in Terminal:
# Turn off built-in Apache
+{CODE(colors="shell")}sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null{CODE}
+%%%
# Install Homebrew Apache
+{CODE(colors="shell" theme="default")}brew install httpd{CODE}
+%%%
# Set up Apache to autostart upon reboot
+{CODE(colors="shell" theme="default")}brew services start httpd{CODE}
+%%%
# 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
+{CODE(colors="shell" theme="default")}#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{CODE}
+%%%
!!! Configure Apache
Make the following changes in -+/usr/local/etc/httpd/httpd.conf+- :
# Change the listen port from the brew default of -+Listen 8080+- to -+Listen 80+-
+%%%
# Change the document root
+{CODE(colors="shell")}#if using the global root
DocumentRoot "/Library/WebServer/Documents"
#if using the user home directory
DocumentRoot "/Users/username/Sites"{CODE}
+%%%
# Set the Directory tag to the same root
+{CODE(colors="shell")}#if using the global root
<Directory "/Library/WebServer/Documents">
#if using the user home directory
<Directory "/Users/username/Sites">{CODE}
+%%%
# Set AllowOverride within the same Directory tag to All
+{CODE(colors="shell")}AllowOverride All{CODE}
+%%%
# Enable rewrite module (disabled by default)
** Uncomment the line that says -+LoadModule rewrite_module libexec/mod_rewrite.so+-
+%%%
# Change User and Group
** By default these are set to -+daemon+-. Change to -+_www+- or actual user and group names to avoid permission problems.
+%%%
# Set the server name: -+ServerName localhost+-
!! Install PHP
# In Terminal, enter the following commands:
+{CODE(colors="shell" theme="default")}# 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
{CODE}
+Deprecated versions of PHP may not be available in the Homebrew core formulae, in which case an external "tap" may be used as follows:
+{CODE(colors="shell" theme="default")}# 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
{CODE}
+%%%
# Make changes to Apache configuration at -+/usr/local/etc/httpd/httpd.conf +- to use newly installed PHP version
+{CODE(colors="shell" theme="default")}# 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>{CODE}
!! Install Xdebug PHP Extension
''This section assumes you've installed PHP in the previous step.''
# In Terminal, enter the following command:
+{CODE(colors="shell" theme="default")}pecl uninstall -r xdebug && pecl install xdebug{CODE}
+%%%
# In the php.ini file, add the following lines:
+(look at phpinfo to see where the file is located)
+{CODE(colors="shell")}xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
xdebug.remote_host="localhost"{CODE}
+%%%
# Restart Apache by entering the following command in Terminal (you may be prompted for a password):
+{CODE(colors="shell" theme="default")}brew services restart httpd{CODE}
!! Install MySQL
# In Terminal, enter the following command:
+{CODE(colors="shell")}brew install mysql{CODE}
+
# Follow Homebrew instructions for launching
** Enter -+brew services restart mysql+- following the other Homebrew instructions (but before setting running -+ mysql_secure_installation+- ):
** If you have problems restarting the mysql server, try restarting your computer.
++ %%%
# Set a password to avoid errors. Enter the following and follow the prompts:
+{CODE(colors="shell")} mysql_secure_installation{CODE}
!! Check versions
# In Terminal, enter one of the following commands (lines beginning with # are just descriptions):
+{CODE(colors="shell")}#list versions of all programs installed by home-brew
brew list --versions
or
#show the version of a single program
brew list --versions mysql{CODE}
!! 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 #):
+{CODE(colors="shell")}brew update #first update homebrew itself
brew outdated #list what is outdated
brew upgrade #upgrade everything
brew upgrade mysql #or upgrade a specific program{CODE}
!! Remove old versions of programs
# By default Homebrew leaves the old version intact when updating program. To remove them, type the following in Terminal:
+{CODE(colors="shell")}# 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{CODE}
!! 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
+{CODE(colors="shell" theme="default")}brew uninstall php@7.0 {CODE}