MacOS High Sierra: Setup Local Web Server MacOS (Homebrew + Apache + PHP + MariaDB)

Homebrew Installation

First let’s install Homebrew.

$ /usr/bin/ruby -e "$(curl -fsSL"

Homebrew can self-diagnose. Let’s see if everything works the way it should.

$ brew doctor

Add the Homebrew taps we need.

$ brew tap homebrew/core

Apache Installation

macOS comes with Apache pre-installed, stop it and prevent it from starting on boot.

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

We don’t want to use the pre-installed Apache. Instead we’ll brew and configure it to run on standard ports (80/443).

$ brew install httpd

Check installation.

$ which apachectl

Start Apache, open browser with and you should see a message saying “It works!”

$ sudo apachectl -k start

Set Apache to launch on startup.

$ sudo brew services start httpd

You can watch the Apache error log in a new Terminal tab/window during a restart to see if anything is invalid or causing a problem:

$ tail -f /usr/local/var/log/httpd/error_log

Remember useful commands.

$ sudo apachectl start
$ sudo apachectl stop
$ sudo apachectl -k restart
$ sudo apachectl configtest

PHP Installation

$ brew install [email protected]
$ brew unlink [email protected]
$ brew install [email protected]

The php.ini file can be found in: /usr/local/etc/php/7.2/php.ini.

Apache PHP Setup

You have successfully installed your PHP versions, but we need to tell Apache to use them. You will again need to edit the /usr/local/etc/httpd/httpd.conf. Modify the paths as follows, comment out all but one entry:

#LoadModule php5_module /usr/local/opt/[email protected]/lib/httpd/modules/
LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/

Edit /usr/local/etc/httpd/httpd.conf and uncomment these lines…

LoadModule rewrite_module lib/httpd/modules/
LoadModule socache_shmcb_module lib/httpd/modules/
LoadModule ssl_module lib/httpd/modules/
LoadModule userdir_module lib/httpd/modules/
LoadModule vhost_alias_module lib/httpd/modules/

Include /usr/local/etc/httpd/extra/httpd-userdir.conf
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
Include /usr/local/etc/httpd/extra/httpd-ssl.conf

Modify httpd.conf more…

Check DirectoryIndex includes index.php.

DirectoryIndex index.php index.html

User username
Group staff

DocumentRoot "/Users/username/Sites"
<Directory "/Users/username/Sites">
    AllowOverride All

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

Find Listen 8080 and change it:

Listen 80

Servername is disabled by default, set it to localhost:

ServerName localhost

Restart apache

$ sudo apachectl -k restart

PHP Switcher Script

We hard-coded Apache to use PHP 5.6, but we really want to be able to switch between versions. Luckily, some industrious individuals have already done the hard work for us and written a very handy little PHP switcher script.

We will install the sphp script into brew’s standard /usr/local/bin:

$ curl -L > /usr/local/bin/sphp
$ chmod +x /usr/local/bin/sphp

Testing the PHP Switching

After you have completed these steps, you should be able to switch your PHP version by using the command sphp followed by a two digit value for the PHP version:

$ sphp 72

MariaDB Installation

Install MariaDB with Homebrew.

brew install mariadb

After installation, start the MariaDB server with…

brew services start mariadb

After MariaDB is started, you can connect.

mysql -uroot

$ brew install mariadb
$ brew services start mariadb
$ mysql_secure_installation

SSL/Virtual Hosts

Change default 8443 ports to 443 in the SSL configuration file.

$ vi /usr/local/etc/httpd/extra/httpd-ssl.conf

Replace ‘Listen 8443’ with ‘Listen 443’.

Also update it here:

<VirtualHost _default_:8443>

General setup for the virtual host

DocumentRoot "/usr/local/var/www"

<VirtualHost _default_:443>

Save the file, open up /usr/local/etc/httpd/extra/httpd-vhosts.conf and add your own SSL based virtual hosts.

$ vi /usr/local/etc/httpd/extra/httpd-vhosts.conf

Create your virtual host entries that you want to use SSL with.

<VirtualHost *:443>
    DocumentRoot "/Users/your_user/Sites"
	SSLEngine on
	SSLCertificateFile "/usr/local/etc/httpd/server.crt"
	SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"


Generate a key and certificate.

$ cd /usr/local/etc/httpd
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt

Leave a Reply

Your email address will not be published. Required fields are marked *