Install Apache, MySQL and PHP on Mac OSX via Homebrew- Guide for Absolute Beginners

Iznet Franklin

Install Homebrew

Homebrew is a software package manager and is the recommended tool for developers to install software packages on Mac OSX. If you don't yet have it, install Homebrew is just an easy step by issuing one single command. Open the Terminal utility either by double click on file located at /Applications/Utilities/Terminalor search for terminal in LaunchPad):

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

After a few minutes, you get Homebrew installed on your Mac. Checking its version by running this command:

$ brew --version

Run this command to update Homebrew packages (or just brews):

$ brew update

Install Apache

Since most of the recent version of Mac OS shipped with a default version of Apache web server, therefore to avoid any conflict may causes headache later on, we will stop this version of Apache from running on our computer.

First stop the server by running this command:

$ sudo apachectl stop

Next, remove Apache from list of services that automatically get started at login:

$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

The above command doesn't remove the plist file (what this file does is basically add Apache to the list of services that automatically get started on login). It just disables the service in this file (Apache) to automatically start at login. You can enable it again if you want.

If you really want to remove this version of Apache completely then run this command:

$ which httpd

And then remove the file return by the above command.

Now, you're ready to install Apache via Homebrew:

$ brew tap homebrew/apache && brew install httpd24 --with-privileged-ports --with-http2

After that start Apache web server:

$ sudo apachectl restart

Open your web browser and go to http://localhost to check Apache working correctly.

If you want to make Apache automatically start at login, then copy the plist file to LaunchDaemons folder:

$ sudo cp -v /usr/local/Cellar/httpd/2.4.*/homebrew.mxcl.httpd.plist /Library/LaunchDaemons

Change permission and owner of this file:

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist
sudo chmod 644 /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

Finally, we'll need to tell LaunchCtrl to load this plist file:

$ sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.httpd24.plist

Install MySQL

First, check to see there's any version of MySQL install on your Mac:

which mysql

If the above command doesn't return anything you're good to go. Hoever, if it returns a path to MySQL binary file (for example /usr/local/bin/mysql) then you should check to see if there's any instance of MySQL server is running and shutdown this server and uninstall the current version of MySQL server on your Mac.

Install MySQL using Homebrew run the following command:

$  brew install mysql

If you prefer a specific version of mysql then run the following command:

$ brew search mysql

And look for the version that you want to install.

Install PHP

To install PHP via Homebrew run the following command:

$ brew install php56

Or if you want you can also search Homebrew packages for a specific version of PHP:

$ brew search php

Then install PHP (following command will install version 5.6 of PHP):

$ brew install php56 --with-httpd2

After that check version of PHP installed on your Mac:

$ php -v

Configure Apache to Use PHP

In order to make PHP works with Apache web server, we need to make some modifications to the Apache configuration file to load the PHP module (this module is already installed when you install PHP with the option --with-httpd in the previous command).

Depending which version of PHP you installed the module file location may differ. To check this location run the following command:

echo $(brew --prefix homebrew/php/php56)

The above command return the location of folder that contains a libexec folder. Inside the libexec folder you'll see a folder named apache2 and inside this folder you'll see a find named libphp5.so. This is the PHP module for Apache. In my case, this file is located at /usr/local/opt/php56/libexec/apache2/libphp5.so.

To see where the Apache configuration file located:

$ brew info apache2

Or:

$ brew info httpd

You'll see something similar to the following:

Apache HTTP server
https://httpd.apache.org/
/usr/local/Cellar/httpd/2.4.28 (1,622 files, 26.0MB) *
  Poured from bottle on 2017-10-14 at 13:12:16
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpd.rb
==> Dependencies
Required: apr ✔, apr-util ✔, nghttp2 ✔, openssl ✔, pcre ✔
==> Caveats
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don't want/need a background service you can just run:
  apachectl start

Note the location of Apache configuration file from the above result is: /usr/local/etc/httpd/httpd.conf. Open this file using a text editor such as Vim or SublimeText and add find fow the following text:

LoadModule php5_module

If you find it then uncomment that line (by removing the hash # in front of it), if you don't then just add it to some where near the LoadModule section (sure you can add it to anywhere but place it to the LoadModule will help you easily scan for it later on).

Next, we need to tell Apache to use the PHP Module we just added above to interpret PHP scripts inside the files that end with .php extension. To do so in the Apache configuration file search for the following text:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

Then replace with:

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Restart Apache:

$ sudo apachectl -k restart

Now if you add an index.php file to the Apache document root folder (usually is /var/www/html, or you can also seach for the DocumentRootin configuration file for exact value) with the following content:

<?php echo "Hello World!"; ?>

You should see the text Hello World showing in browser when visiting the localhost address on browser.

Reply