Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save karlhillx/e17906609d5b9c955ae996f2ecbb6df8 to your computer and use it in GitHub Desktop.
Save karlhillx/e17906609d5b9c955ae996f2ecbb6df8 to your computer and use it in GitHub Desktop.

macOS Catalina 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, and MariaDB.

Before we get started we need to make sure XCode Command Line Tools is installed on your system.

$ xcode-select --install

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)"

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. 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

$ 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. Username will be your username and Group will be "staff."

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.

# User home directories
Include /usr/local/etc/httpd/extra/httpd-userdir.conf

# Virtual hosts
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

# Secure (SSL/TLS) connections
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.4.15 (cli) (built: Feb  4 2021 12:11:40) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.15, Copyright (c), 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

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 🔥.

@madamadami
Copy link

madamadami commented Sep 30, 2020

Under section "Update user and group" you wrote: "User username" … should I replace username by my actual or should I literally write username? While staff is an existing group in macOS, username seems to be a placeholder …

@karlhillx
Copy link
Author

karlhillx commented Oct 20, 2020

@madamadami

Under section "Update user and group" you wrote: "User username" … should I replace username by my actual or should I literally write username? While staff is an existing group in macOS, username seems to be a placeholder …

Replace username with your user name on your Mac and use "staff" for the group.

@madamadami
Copy link

Thanks! I think about updating from Catalina to Big Sur — which workflow do you suggest to update an environment that has been set up under Catalina with your great gist?

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