Skip to content

Instantly share code, notes, and snippets.

@karlhillx
Last active February 21, 2024 09:51
Show Gist options
  • Save karlhillx/5cd68430aeb25e5e242a3e2c30f238d1 to your computer and use it in GitHub Desktop.
Save karlhillx/5cd68430aeb25e5e242a3e2c30f238d1 to your computer and use it in GitHub Desktop.
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.

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

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
Copy link

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
Copy link

garybuckle 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
Copy link

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
Copy link
Author

@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
Copy link
Author

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

@garybuckle I made the changes, cheers.

@gwvoigt
Copy link

gwvoigt 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
Copy link

Hidduh commented Apr 25, 2019

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

@derkhadim
Copy link

Great document!

@sebj54
Copy link

sebj54 commented Sep 25, 2019

Awesome resource, very detailed and easy to follow. Great work! 👍

@moghwan
Copy link

moghwan commented Feb 5, 2020

Very much appreciated! but I always get ERR_EMPTY_RESPONSE when browsing to localhost or 127.0.0.1

@Paul-Morris
Copy link

Paul-Morris commented Apr 7, 2020

TIP: If you didn't set a mysql password like me (I know bad practice) just type "mysql" in osx terminal to log into it
https://mariadb.com/kb/en/mysql-command-line-client/

@nahimanajz
Copy link

How can deal with this error ?

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.43.29. Set the 'ServerName' directive globally to suppress this message
[Mon Oct 19 17:29:06.250429 2020] [mpm_prefork:notice] [pid 7866] AH00163: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Oct 19 17:29:06.250855 2020] [core:notice] [pid 7866] AH00094: Command line: '/usr/local/opt/httpd/bin/httpd -D FOREGROUND'
[Mon Oct 19 17:31:05.740638 2020] [mpm_prefork:notice] [pid 7866] AH00169: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.43.29. Set the 'ServerName' directive globally to suppress this message
[Mon Oct 19 17:38:55.033104 2020] [mpm_prefork:notice] [pid 8360] AH00163: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Oct 19 17:38:55.033506 2020] [core:notice] [pid 8360] AH00094: Command line: '/usr/local/opt/httpd/bin/httpd -D FOREGROUND'

@kanadgodse
Copy link

There's a problem with MariaDB where you won't be able to set password for root user which can be solved by the steps here: https://stackoverflow.com/a/57481041

Also, @nahimanajz Please read carefully as karlhillx has mentioned to set the ServerName to localhost everywhere, so please follow guide carefully!

@Lo1176
Copy link

Lo1176 commented Jun 9, 2021

Hi @Karlhilx.

When I do sudo brew services start httpd
I've got this message :
Error: No such file or directory @ apply2files - /Library/LaunchDaemons/homebrew.mxcl.httpd.plist

@lucian303
Copy link

Try brew reinstall httpd then restart with sudo. @Lo1176
That file seems to have been removed for no apparent reason and from no apparent action on the user's part.

@puma69
Copy link

puma69 commented Jun 12, 2021

Thank you @lucian303, you've fixed my issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment