Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karlhillx/791babda83495eab6fbf64f8a3270f86 to your computer and use it in GitHub Desktop.
Save karlhillx/791babda83495eab6fbf64f8a3270f86 to your computer and use it in GitHub Desktop.
macOS Big Sur 11.0 Setup: Homebrew + Apache + PHP + MariaDB (Regularly updated)

macOS Big Sur 11.0 Setup: Homebrew + Apache + PHP + MariaDB

This document helps get your macOS development environment up and running with the latest versions of Homebrew, Apache, PHP, and MariaDB.

macOS Homebrew Apache PHP MariaDB

Table of Contents

[Expand/Collapse]

  1. Homebrew Installation
  2. Apache Installation
  3. PHP Installation
  4. Apache PHP Setup
  5. MariaDB Installation
  6. SSL & Virtual Hosts
  7. License


Before we get started, let’s install XCode Command Line Tools on your system.

$ xcode-select --install

Homebrew Installation

Homebrew is an excellent package manager for macOS. Homebrew installs the stuff you need that Apple (or your Linux system) didn’t; let's install it.

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

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 the installation is successful, the output will be:

Your system is ready to brew.

Otherwise, please follow instructions to fix any potential issues.

Apache Installation

macOS 11.0 Big Sur comes with Apache 2.4 pre-installed. However, we don't want Apple to control 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 to create a new folder in your user's root directory.

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

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

$ 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
💡 Tip: You can monitor the Apache error log in a new Terminal tab/window to see if anything is invalid or causing a problem.
$ tail -f /usr/local/var/log/httpd/error_log
💡 Tip: Remember useful Apache 'brew services' commands.
$ brew services stop httpd
$ brew services start httpd
$ brew services restart httpd

PHP Installation

Install the latest PHP version.

$ brew install php

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

Apache PHP Setup

Now that we have PHP successfully installed, we still need to tell Apache to use it. Please edit the Apache httpd.conf file.

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

Find Listen 8080 and change the port to 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 php_module /usr/local/opt/php/lib/httpd/modules/libphp.so

Update User and Group where your_username is the name of your User in macOS and Group will be "staff."

User your_username
Group staff

Servername is disabled by default, set it to localhost:

#ServerName www.example.com:8080
ServerName localhost

Let's modify httpd.conf a bit more. Change DocumentRoot; it makes up the basic document tree, which will be visible from the web.

DocumentRoot "/Users/your_username/Sites"
<Directory "/Users/your_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 the following 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.

$ brew services restart httpd

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 sudo apachectl -t.

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

Then type php -v in the terminal. It should report something like the following.

PHP 8.0.6 (cli) (built: May 13 2021 05:36:01) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.6, Copyright (c), by Zend Technologies

MariaDB Installation

Install MariaDB using Homebrew.

$ brew install mariadb

Let's have MariaDB start automatically on boot.

$ brew services start mariadb

You will see something like the following.

==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)

Improve the security of the installation and change the database's root password.

$ sudo /usr/local/bin/mysql_secure_installation

You will be prompted with:

Enter current password for root (enter for none):

Press return here.

Change the root password, and continue with the prompts.

Change the root password? [Y/n]

When you get to the following prompt, enter Y to reload privileges.

Reload privilege tables now?

When finished, restart the MariaDB server.

$ brew services restart mariadb

You can now try logging into MariaDB.

mysql -u root -p

If successful, you should see the following.

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.5.8-MariaDB Homebrew

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

SSL/Virtual Hosts

The OpenSSL software library is a pre-requisite to generate a certificate. Check if OpenSSL is available by running the following command in the local environment.

$ which openssl
/usr/bin/openssl

We need to install OpenSSL if the which command does not return a path.

$ brew install openssl

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>

After the file has been saved, follow the series of prompts to generate a key and a self-signed 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 host entries.

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

<VirtualHost *:80>
    ServerName yourprojectdomain.com
    DocumentRoot "/Users/your_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/your_username/Sites/yourprojectname"
    ServerName yourprojectdomain.com
    SSLEngine on
    SSLCertificateFile "/usr/local/etc/httpd/server.crt"
    SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>

Finally, in the Terminal window, restart Apache.

$ brew services restart httpd

License

Copyright © 2020 Karl Hill.

Provided under the MIT license.

Whether you use these instructions or have learned something from them, please consider supporting me with a star ⭐ and a follow 🔥.

@karlhillx
Copy link
Author

karlhillx commented Nov 14, 2020

Need to completely uninstall a Homebrew MariaDB installation and start over?

brew services stop mariadb 
rm -rf /usr/local/Cellar/mariadb/*
rm -rf /usr/local/Cellar/mariadb
rm -rf /usr/local/etc/my*

@EduVillas
Copy link

EduVillas commented Dec 27, 2020

Why Mariadb instead mysql 5.7 or 8.0?

@karlhillx
Copy link
Author

Why Mariadb instead mysql 5.7 or 8.0?

@EduVillas That's my current preference for my development environment. Feel free to use MySQL.

@EduVillas
Copy link

EduVillas commented Dec 27, 2020

At this point, I ruin all my apache and PHP environment here. A lot of problems with brew. It's been 3 days trying to install PHP 8 without success. My apache now is in private/etc/apache2/

@gosocial2
Copy link

PHP 8 is premature at the time of this writing. If you are in for serious PRODUCTION-level development, refrain from jumping to newly released major versions.

@mijnnaamisramon
Copy link

When I try to install Mariadb on my Mac (Big Sur) I get: Error: mariadb: no bottle available! and all other efforts are futile as well. Anyone got some tips?

@bookchiq
Copy link

bookchiq commented Jan 15, 2021

When I try to install Mariadb on my Mac (Big Sur) I get: Error: mariadb: no bottle available! and all other efforts are futile as well. Anyone got some tips?

@mijnnaamisramon It looks like mariadb isn't available for the M1 chip yet. That's the issue I had with Big Sur on my M1 Mac Mini, anyway.

@nt7u
Copy link

nt7u commented Jan 20, 2021

You say: "...we don't want Apple to control our web server...". What are the disadvantages of using the stock install of Apache? I'm not a sophisticated web development guy and I just wants to play with NodeJS and be able to have HTML pages served for some engineering projects. I'm not sure if it creates a whole additional level of hell for me to deviate from the default path. Can you share any insight for a guy (or gal) at this point of their decision tree (stock Apple install of Apache vs your "replace it" approach)?

@khklatt
Copy link

khklatt commented Mar 15, 2021

An issue during install of PHP. Apple has php installed in /usr/bin of Big Sur. php -v issues:
WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Dec 21 2020 21:33:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

Attempts at removal or rename are not allowed. Path will work, not sure.

Options?

@amingomezd
Copy link

You say: "...we don't want Apple to control our web server...". What are the disadvantages of using the stock install of Apache? I'm not a sophisticated web development guy and I just wants to play with NodeJS and be able to have HTML pages served for some engineering projects. I'm not sure if it creates a whole additional level of hell for me to deviate from the default path. Can you share any insight for a guy (or gal) at this point of their decision tree (stock Apple install of Apache vs your "replace it" approach)?

i was using the stock apache server, but i gt bored that every time that you install updates, you have to configure everything all over again, because apple updates deletes all the configuration, virtual hosts, everything.

@gosocial2
Copy link

May I suggest:

Installing a local feature-rich PHP development environment for Mac Users (using Homebrew)

Installing Homebrew on macOS Big Sur

Installing Homebrew on macOS Big Sur

Watch on YouTube

Installing PHP 8 via Homebrew on macOS Big Sur

Installing PHP 8 via Homebrew on macOS Big Sur

Watch on YouTube

@khklatt
Copy link

khklatt commented Mar 16, 2021 via email

@Pasqualeperf
Copy link

If I ping my local virtual hosts it works, but in browser not...

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