Set up Apache and PHP, MySQL and phpMyAdmin with Homebrew on macOS
TABLE OF CONTENTS
- Install Homebrew
- Install Apache and PHP
- Edit hosts file
- Edit httpd files
- Create a SSL sertificate
- Test it
- Install mysql and phpmyadmin
- Setup MySQL
- Setup phpMyAdmin
- Conclusion
Looking to run your PHP webpage locally on your Mac? Look now further. In this post I will guide you through setting up a local server with SSL. To do it all, I will use the package manager Homebrew.
Install Homebrew
Open Terminal in macOS and paste the following code from brew.sh to install Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Apache and PHP
Now that we have Homebrew. We can install Apache (httpd) and PHP (php) simultaneously with one simple command in Terminal.
brew install httpd php
Great. Now we will configure some files. This is where some of your paths might differ, because Intel Macs and Apple silicon Macs operates with a different folder structure for the relevant files.
This guide will focus on the Apple silicon setup. I can confirm that it also works with Mac, having setup this on both types.
Edit hosts file
Navigate to the folder /private/etc/
. In Finder you will be able to find this as a subfolder to Macintosh HD
. Press Shift + Cmd + .
to show hidden files in Finder.
In there you will find the file hosts, without a file extension. Open that in any text editor.
After 127.0.0.1 localhost, type in the domains you want to setup development for. You can always add more later. I will stick with two: domain.local and second.local.
Also uncomment the last line ::1 localhost.
Save the hosts
file.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost domain.local second.local
255.255.255.255 broadcasthost
# ::1 localhost
Edit httpd files
Navigate to the folder /opt/Homebrew/etc/httpd/. For Intel Mac users, the folder is /usr/local/etc/httpd/.
Open httpd.conf in a text editor. Find Listen 8080 (probably on line 52) and change it to:
Listen80
Uncomment the inclusion of virtual hosts settings (line 506) so that it looks like this:
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf
Uncomment the inclusion of mod_rewrite (line 181):
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
We will include secure SSL/TLS connections. Uncomment the inclusion of ssl settings (line 523):
Include /usr/local/etc/httpd/extra/httpd-ssl.conf
Also, uncomment the inclusion of mod_ssl (line 150):
LoadModule ssl_module lib/httpd/modules/mod_ssl.so
Inside the IfModule unixd_module tag. Change user and group (line 193 and 194) to your username on your Mac, and to staff. Such as:
User MyMacUserName
Group staff
Uncomment the following ServerName option (line 223) to silence some annoying warnings that might else appear. And change example.com:8080 to:
ServerName www.example.com:80
Uncomment the socache_shmcd_module (line 92):
LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so
Uncomment the vhost_alias_module (line 174):
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
Add inclusion of libphp.so (on line 183, also add a new empty line below, to keep up with the line numbers in this guide). For Intel Macs, replace /opt/homebrew/ with /usr/local/. Here goes:
LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so
Add the inclusion of a PHP setting configuration file (line 509 and 510). For Intel Macs, replace /opt/homebrew/ with /usr/local/. As follows:
# PHP settingsInclude /opt/homebrew/etc/httpd/extra/httpd-php.conf
Go to the folder /opt/homebrew/etc/httpd/extra/ (for Intel Mac: /usr/local/etc/httpd/extra/). Create a new empty file called httpd-php.conf.
Open it in a text editor and add:
<IfModule php_module>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
</IfModule>
Save the file httpd-php.conf and close it.
Also in the folder /opt/homebrew/etc/httpd/extra/ (for Intel Mac: /usr/local/etc/httpd/extra/). Open the file called httpd-vhosts.conf in a text editor. Here are a couple of VirtualHost examples which we will remove. In it place, we will add two new virtual hosts.
A note to some customization you will need to do yourself here. Insert your own username from your Mac. Also note that I have placed my webpage in a folder called Sites (located in /Users/MyMacUserName/).
I am pointing to a SSL Certificate we have yet to create as well. From line 23 and on inhttpd-vhosts.conf, insert this:
<VirtualHost *:443>
ServerName domain.local
DocumentRoot /Users/MyMacUserName/Sites/domain
SSLEngine on
SSLCertificateFile "/opt/homebrew/etc/httpd/server.crt"
SSLCertificateKeyFile "/opt/homebrew/etc/httpd/server.key"
<Directory /Users/MyMacUserName/Sites/domain>
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName second.local
DocumentRoot /Users/MyMacUserName/Sites/second
SSLEngine on
SSLCertificateFile "/opt/homebrew/etc/httpd/server.crt"
SSLCertificateKeyFile "/opt/homebrew/etc/httpd/server.key"
<Directory /Users/MyMacUserName/Sites/second>
Require all granted
AllowOverride All
</Directory>
</VirtualHost>
Go to the folder /opt/homebrew/etc/httpd/extra/ (for Intel Mac: /usr/local/etc/httpd/extra/). Open the file called httpd-ssl.conf in a text editor. Here you will find a mention of Listen 8443 (line 36). Change it to:
Listen443
Farther down you below the SSL Virtual Host Context settings, you will find another mention of 8443, change it to 443 here as well:
<VirtualHost _default_:443>
Create a SSL sertificate
In Terminal, type in this:
openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \
-keyout server.key -out server.crt -subj "/CN=domain.local" \
-addext"subjectAltName=DNS:domain.local,DNS:second.local"
This will create a server.key file and a server.crt file in the current folder where you are located. Move these files to /opt/homebrew/etc/httpd/ (or /usr/local/etc/httpd/ on an Intel Mac).
Then add the server.crt to your login keychain in the macOS app Keychain. Open the sertificate in Keychain, and set the options sure you always trust it. When closing the window.
Test it
Now is the time to test if your Apache server supporting PHP is up and running. First, lets start the server. We will run a command that also makes the server start up with you Mac in the future, so that your development server always will be accessible. In Terminal, type:
sudo brew services start httpd
You will get some warnings. But don't mind.
Go to the folder you created for domain.local in Finder. For me it is /Users/MyMacUserName/Sites/domain/ where the settings in httpd-vhosts.conf was pointing to. Create an index.php file with some content.
Then enter https://domain.local (don't forget the https) in your browser and you are set to go.
Install mysql and phpmyadmin
Next we will install MySQL and phpMyAdmin as well. To get a complete environment up and running that can support systems like ProcessWire (or WordPress).
Open Terminal and type:
brew install mysql phpmyadmin
Setup MySQL
Start by restarting MySQL, using this command in Terminal:
brew services restart mysql
The we will run another command to secure the MySQL setup. Type in the following command in Terminal:
mysql_secure_installation
Answer like this:
- Y for yes: Would you like to setup VALIDATE PASSWORD component?
- 2 for strong: Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG
- Type in a secure password that you will remember or store in a password manager: New password
- Y for yes: Do you wish to continue with the password provided?
- Y for yes: Remove anonymous users?
- Y for yes: Disallow root login remotely?
- Y for yes: Remove test database and access to it
- Y for yes: Reload privilege tables now?
Setup phpMyAdmin
Go to /opt/homebrew/etc/httpd/ (/usr/local/etc/httpd/ on Intel Mac) and open httpd.conf in a text editor. At the bottom of the file, add:
Alias /phpmyadmin /usr/local/share/phpmyadmin
<Directory /usr/local/share/phpmyadmin/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
Restart httpd using:
sudo brew services restart httpd
Now you can open phpMyAdmin in your browser, go to http://localhost/phpmyadmin (http, not https this time).
And there you will have phpMyAdmin up and running. Login using root as username, and your newly created password from the mysql_secure_installation setup.
Conclusion
This worked for me. And I hope it will work for you two. I messed it up quite a few times the first time around, but it was mostly due to typos and not remembering.
Also, different users on different Macs might have their own setup. Meaning you can run into issues I did not.
Good luck!