Apple released the latest macOS 12 Monterey in 2021 on October 25, 2021. That’s it again this year, and if you upgraded from macOS 11 Big Sur or older, you may need to reconfigure macOS.
Alternatively, if you have a brand new Mac with macOS 12 Monterey, the following configuration steps should help you set up a local web server on the macOS platform.
On macOS 12 Monterey, Apache comes built-in. However, when you look at Apache’s built-in httpd.conf, you’ll notice a line that says:
#PHP was deprecated in macOS 11 and removed from macOS 12
Starting with macOS 12 Monterey, since there is no built-in PHP, if you want to set up a MAMP (macOS, Apache, MySQL, PHP) stack, I recommend using the Homebrew version of Apache along with PHP.
Disable macOS built-in Apache
Since we will not be using the macOS 12 built-in version of Apache, if your built-in Apache is running, issue the following command to stop the Apache service.
Open the terminal Terminal
and enter:
sudo apachectl stop
Install Homebrew
The first thing you need to do is install Homebrew to your macOS 12 Monterey system (if you don’t have Homebrew installed yet).
Check if you have Homebrew installed or know the version of Homebrew installed
Open Terminal
and enter brew -v
Homebrew 3.3.0 Homebrew/homebrew-core (git revision 359f9f16171; last commit 2021-10-25)
This means you already have Homebrew installed. If you don’t see the above, follow the steps below to install Homebrew on your system.
Go to https://brew.sh/
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Open Terminal
and paste the above command.
Note: If you do not have the Command Line Tools for Xcode installed, the Homebrew installer will first download and install the Command Line Tools for Xcode, and then it will proceed to complete the installation of Homebrew to your system.
Export environment path
When you try to issue the brew command, you may receive the following error.
zsh: command not found: brew
If you get the above error, you have to export the path by issuing the following command.
export PATH="/opt/homebrew/bin:$PATH"
Open Terminal
and type the following command, then press Enter.
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> $HOME/.zshrc
Then type the following command and press Enter.
Close Terminal
and reopen a new Terminal
window.
Install Apache via Homebrew
After installing Homebrew and setting up your exports correctly, the next step is to install Apache using Homebrew.
Open Terminal
Enter brew install httpd
Configuring the Homebrew version of Apache (httpd.conf file)
Open Terminal
Enter cd /opt/homebrew/etc/httpd/
Type sudo cp httpd.conf httpd.conf.bak
and press Enter (This step is optional if you want to keep a copy of the original configuration file.)
Enter sudo nano httpd.conf to edit
Enable modules and configuration
You need to enable (uncomment the line) and modify/update the configuration in the httpd.conf
file.
To enable a module, first, you need to find the module you want to enable.
Use control + w
to bring up the search function, find the following modules and make sure to uncomment them. (Remove #
in front of each line.)
LoadModule authn_core_module lib/httpd/modules/mod_authn_core.so LoadModule authz_host_module lib/httpd/modules/mod_authz_host.so LoadModule userdir_module lib/httpd/modules/mod_userdir.so LoadModule include_module lib/httpd/modules/mod_include.so LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so Include /opt/homebrew/etc/httpd/extra/httpd-userdir.conf
Modify DocumentRoot
Use control + w
and search for DocumentRoot
. Comment out (put in front of each line below #
.
DocumentRoot "/Users/mymac/work/" <Directory "/Users/mymac/work/"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options Indexes FollowSymLinks Multiviews MultiviewsMatch Any # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # Require all granted </Directory>ed
Modify the listening port
By default, it will listen on port 8080
. If you want to listen to port 80
, change Listen 8080
to Listen 80
.
Use Control + w
and search for Listen 8080
and replace with Listen 80
Create a site folder under your username
Since we define DocumentRoot as /Users/mymac/work/
. We need Sites
to create this ” ” folder under the user ” developer
“.
Open Finder
and navigate to the user’s folder.
Create a new folder and name it “work
“
Install PHP via Homebrew
Open Terminal
Enter brew install PHP
Wait for the installation to complete.
Modify httpd.conf to enable PHP on Apache
Open terminal
Enter cd /opt/homebrew/etc/httpd/
Enter sudo nano httpd.conf edit
Add the following lines.
LoadModule php7_module /usr/local/Cellar/[email protected]/7.1.33_4/lib/httpd/modules/libphp7.so<br># Note that this depends on your specific php path. If it is 7.x, the middle is php7_module. If it is 8.x, then php_module is followed by libphp.so. <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Use Control + w
and search for DirectoryIndex
, then add index.php
Create phpinfo.php file
The phpinfo.php file will be used to check if PHP is working properly.
Open Terminal
Enter cd ~/work
Type sudo nano phpinfo.php
After opening the nano application, add the following lines.
<?php phpinfo(); ?>
Then press control + o
to save and control + x
to exit the nano application.
Start the Apache server
After completing the configuration of Apache and PHP, let us start the Apache server.
Open Terminal
Enter brew services restart httpd
To check if the Apache service is running, type brew services list
You should see something similar:
httpd launch developer /Users/developer/Library/LaunchAgents/homebrew.mxcl.httpd.plist<br>// Note: If php displays none, it means php is not enabled. How to enable it: <code>brew services restart [email protected] Adjust according to your own version number, close stop and restart restart</code>
If you see the “Started” status, it means that the Apache server is up and running.