How to Install Apache, MySQL and PHP using Homebrew on macOS

Introduction

This guide will help you set up your own web server within minutes. Of course, you could use the likes of MAMP or XAMP to have this entire process automated in a form of an app, but in this case we will manually install everything one by one.  

Requirements

Installation

PART 1 - APACHE

Once you have both Xcode and Homebrew installed you can proceed with Apache.

First we need to stop the system installation of Apache from starting on boot:

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

We then have to stop the system installation of Apache, in case it is running in the background. We are going to install Apache using Homebrew which will allow us to control the version of Apache unlike the system version:

sudo apachectl stop

It is very unlikely that Apache is running on your server unless you have activated it previously therefore expect a notification that it ‘could not find specified service’.

Now we can proceed with the installation of Apache 2.4:

brew install httpd

Once the process finishes you can check if your installation was successful. Be aware of the PATH used during installation of Homebrew-Cask as you may have changed it:

which apachectl /usr/local/bin/apachectl

You can also check with:

httpd -v

This command will display the version of your server and its build.

At this stage you should be ready to start Apache:

sudo apachectl start

Your website will be located under the IP address of your machine, for example http://207.254.1.1. Hopefully you’ll get a satisfying message saying “It Works!”

No we have to make sure that Apache starts automatically on startup in case you decide to reboot your machine:

sudo brew services start httpd

In the latest version of Brew, the default port used for Apache is 8080, you should change it to 80 using the Terminal App:

open -e /usr/local/etc/httpd/httpd.conf

Simply find the line that says Listen 8080 and change it to Listen 80

Look for AllowOverride in your file, we will need to change it to:

# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   AllowOverride FileInfoAuthConfig Limit
#
AllowOverride All

We also have to enable mod_rewrite

Search for the following line:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

And simply remove the # (hash) just before LoadModule

At last we need to edit our ServerName which would look like this in your file:

#ServerName www.example.com:8080 

Replace it with your IP Address or domain:

ServerName 207.254.1.1

You can now restart apache:

sudo apachectl -k restart 

PART 2 – PHP

First of all you need to decide which version of PHP you require ranging from 5.6 to 7.2, in this tutorial we’ll use 7.2 but installation of any other version would be much alike.

Install PHP 7.2 using brew.

brew install php@7.2

You can simply replace the 7.2 with another version according to your needs, it can be 5.6, 7.0, 7.1.

Once the installation finishes you can begin editing files, beginning with php.ini. It is the default configuration file for running applications.

You can decide to change the memory_limit, upload_max_filesize or max_execution_time. You can also activate safe mode.

Now you need to go back to your httpd.conf file which we edited during the Apache installation process and add a line which will connect PHP with Apache once again use:

open -e /usr/local/etc/httpd/httpd.conf

Add libphp somewhere below the last edit we made by uncommenting LoadModule:

LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so

At your discretion depending on the project you’re working on, you can also edit an option which will allow you to load index.php or index.html

Look for:

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

 And replace it with:

<IfModule dir_module>
   DirectoryIndex index.htmlindex.htm index.php
</IfModule>

Now you can stop and start apache once again to validate the changes.

PART 3 – MYSQL

At last you can install mysql and the process is very straightforward. You can even use the Native Packages to run the whole installation process.

For this example, we will go through the process of installing it with Homebrew:

brew install mysql

Now you can load and start services:

brew services start mysql

It will tell you to secure your installation, run the following command:

mysql_secure_installation

Set a new root password, remove anonymous users and other options to limit access to localhost only.

You can now restart the MySQL server:

brew services restart mysql

You’re all set.