This document provides help on getting your macOS development environment up and running with the latest versions of Homebrew, Apache, PHP, etc.
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."
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
Install the latest PHP version.
$ brew install php
The php.ini file can be found in: /usr/local/etc/php/7.x/php.ini
.
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
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
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
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!