Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
macOS Mojave Setup: Homebrew + Apache + PHP + MariaDB (Regularly updated)

macOS Mojave Setup: Homebrew + Apache + PHP + MariaDB

This document provides help on getting your macOS development environment up and running with the latest versions of Homebrew, Apache, PHP, etc.

Homebrew Logo

Homebrew Installation

Homebrew is an excellent package manager for macOS; let's install it.

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

Add the Homebrew taps we need.

$ brew tap homebrew/core

Homebrew can self-diagnose and check your system for potential problems. Let's see if everything is working the way it should.

$ brew doctor
If successful it should display "Your system is ready to brew."

Apache Installation

macOS comes with Apache pre-installed. We don't want Apple in control of our web server so let's 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

Type the following command into your terminal:

$ mkdir ~/Sites
macOS automatically adds the compass icon to your folder.

Now, let's brew and configure our new Apache version and change it to run on standard ports (80/443).

$ brew install httpd

Check the installation path.

$ which apachectl
/usr/local/bin/apachectl

Set Apache to start now and restart at login

$ 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

Install the latest PHP version.

$ brew install php

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

Apache PHP Setup

You have successfully installed PHP, but you need to tell Apache to use it. Edit the httpd.conf file.

vi /usr/local/etc/httpd/httpd.conf 

Find Listen 8080 and change it to port 80:

Listen 80

Uncomment the following lines.

LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Add the following entry at the end of the LoadModules section:

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

Update user and group.

User username
Group staff

Servername is disabled by default, set it to localhost:

#ServerName www.example.com:8080
ServerName localhost

Modify httpd.conf a bit more.

Change DocumentRoot; it makes up the basic document tree, which will be visible from the web.

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

Check that directive DirectoryIndex includes index.php.

DirectoryIndex index.php index.html

And we need to add the FilesMatch directive so that Apache will now process PHP files.

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

Uncomment to enable User home directories, Virtual hosts and Secure (SSL/TLS) connections.

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

Restart apache.

$ sudo apachectl -k restart

Run a configuration file syntax test to verify/validate the configuration. It reports Syntax Ok or detailed information about the particular syntax error. This is equivalent to apachectl -t.

$ sudo apachectl configtest
If it says "Syntax OK" open browser using http://127.0.0.1. You should see a message saying, “It works!”

php -v should report something like...

PHP 7.3.7 (cli) (built: Jul  5 2019 12:44:05) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies

SSL/Virtual Hosts

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

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

Replace all lines that say '8443' with '443'.

ServerName www.example.com:443

<VirtualHost _default_:443>

Save the file plus 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

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.

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

<VirtualHost *:80>
    ServerName yourprojectdomain.com
    DocumentRoot "/Users/username/Sites/yourprojectname"
    ErrorLog "/usr/local/var/log/httpd/yourprojectname-error_log"
    CustomLog "/usr/local/var/log/httpd/yourprojectname-access_log" common
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "/Users/username/Sites/yourprojectname"
    ServerName yourprojectdomain.com
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/httpd/server.crt"
    SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

In Terminal, restart Apache.

$ sudo apachectl restart

MariaDB Installation

Install MariaDB with Homebrew.

$ brew install mariadb

Have MariaDB start on boot.

$ brew services start mariadb

Finally, let's improve the security of your installation and add a password.

$ mysql_secure_installation

Restart the MariaDB server.

$ brew services restart mariadb

After MariaDB Server is started, you can log in:

mysql -u root
@mnabialek

This comment has been minimized.

Copy link

commented Sep 29, 2018

Good job! I've upgraded yesterday to Mojave and my PHP stopped working. I've reinstalled Apache and PHP following those instructions and it's working fine again. Thanks!

@garybuckle

This comment has been minimized.

Copy link

commented Sep 29, 2018

Hi,
You won't get this.

  • Then open browser using http://127.0.0.1. You should see a message saying “It works!”

Until you change the port to 80 btw - it might be confusing but this article is very useful - thanks for that!

@garybuckle

This comment has been minimized.

Copy link

commented Oct 1, 2018

in the httpd.conf you will also need to uncomment -

#LoadModule ssl_module lib/httpd/modules/mod_ssl.so
#LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so

Best to launch sudo apachectl configtest after the steps to verify.

Apart from that - great article, helped me out a lot.

Many thanks!

@karlhillx

This comment has been minimized.

Copy link
Owner Author

commented Oct 8, 2018

@mnabialek

Good job! I've upgraded yesterday to Mojave and my PHP stopped working. I've reinstalled Apache and PHP following those instructions and it's working fine again. Thanks!

That's precisely why I posted this, thanks!

@karlhillx

This comment has been minimized.

Copy link
Owner Author

commented Oct 8, 2018

Apart from that - great article, helped me out a lot.

@garybuckle I made the changes, cheers.

@gwvoigt

This comment has been minimized.

Copy link

commented Jan 27, 2019

I'm running a fresh install of Mojave. By default, if I go to localhost it will show "It works!", meaning that the server is running. I'm trying to stop the server and to prevent it of rebooting with the commands provided:

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

After that, localhost stops working, which is expected. However, after a reboot, localhost works again. Wasn't it supposed to stop starting apache during boot?

I also tried:

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

@Hidduh

This comment has been minimized.

Copy link

commented Apr 25, 2019

Thank you for the great tutorial! Managed to install everything without any problems. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.