This document helps get your macOS development environment up and running with the latest versions of Homebrew, Apache, PHP, and MariaDB.
[Expand/Collapse]
Before we get started, let’s install XCode Command Line Tools on your system.
$ xcode-select --install
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.
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
Install the latest PHP version.
$ brew install php
The php.ini file can be found in: /usr/local/etc/php/8.x/php.ini
.
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
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)]>
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
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 🔥.
Need to completely uninstall a Homebrew MariaDB installation and start over?