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)"
Homebrew can self-diagnose and check your system for potential problems. Let's see if everything is working the the way it should.
$ brew doctor
Add the Homebrew taps we need.
$ brew tap homebrew/core
macOS comes with Apache pre-installed. We don't want Apple in control of our web server os 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 to run on standard ports (80/443).
$ brew install httpd
Check the installation.
$ which apachectl
/usr/local/bin/apachectl
Start Apache...
$ sudo apachectl -k start
Then open browser using http://127.0.0.1. You should see a message saying “It works!”
Set Apache to launch on startup.
$ 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
$ brew install php@7.2
The php.ini file can be found in: /usr/local/etc/php/7.2/php.ini.
You have successfully installed PHP, but you need to tell Apache to use it. Edit the /usr/local/etc/httpd/httpd.conf file.
vi /usr/local/etc/httpd/httpd.conf
Add the following entry at the end of the LoadModules section:
LoadModule php7_module /usr/local/opt/php@7.2/lib/httpd/modules/libphp7.so
Uncomment these lines...
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
LoadModule userdir_module lib/httpd/modules/mod_userdir.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
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
Modify httpd.conf even more...
Check that DirectoryIndex includes index.php.
DirectoryIndex index.php index.html
Update user and group...
User username
Group staff
DocumentRoot and Directory... DocumentRoot "/Users/username/Sites" <Directory "/Users/username/Sites"> AllowOverride All
And we need to add these lines so that Apache will process PHP files correctly.
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
Find Listen 8080 and change it:
Listen 80
Servername is disabled by default, set it to localhost:
#ServerName www.example.com:8080
ServerName localhost
Restart apache
$ sudo apachectl -k restart
Install MariaDB with Homebrew.
$ brew install mariadb
Have MariaDB start on boot.
$ brew services start mariadb
Add a password and secure your installation.
$ mysql_secure_installation
Change default 8443 ports to 443 in the SSL configuration file.
$ vi /usr/local/etc/httpd/extra/httpd-ssl.conf
Replace 'Listen 8443' with 'Listen 443'.
Also update it here:
<VirtualHost _default_:8443>
DocumentRoot "/usr/local/var/www"
ServerName www.example.com:443
<VirtualHost _default_:443>
Save the file, 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 that you want to use SSL with.
<VirtualHost *:443>
DocumentRoot "/Users/your_user/Sites"
ServerName yourdomain.com
SSLEngine on
SSLCertificateFile "/usr/local/etc/httpd/server.crt"
SSLCertificateKeyFile "/usr/local/etc/httpd/server.key"
</VirtualHost>
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