A good chunk of what we need do web development exists in macOS 12.1 (Monterey) without having to resort to add-ons like the XAMPP stack. I have enough of my students asking me how to do this that I thought I would consolidate my notes and put them up on the web so I can just point them at that reference. This is going to pull from multiple sources and I’ll try to acknowledge every one that I can. Let me know if I missed one.
There are some things you will need to add. Recent versions of macOS no longer include PHP as part of the operating system. That’s actually a good thing as the tendency has been for the version included has tended to lag behind the current tip of development. Here’s where you find yourself using a package manager like Homebrew.
First step: Configuring Apache
The OS includes the Apache web server. It’s buried pretty deep into the system bits so you are going to having to apply some important skills:
- Understanding of working with the Terminal.app to run command-line programs to update configuration files and manage the web sever
- Know some of the internals of the Apache web server,
- And know how to open and save files in text editor like vi or nano.
,
This information is spread over the Internet but the bulk of the material is taken from the Apple tech support discussion forum at https://discussions.apple.com/docs/DOC-250004361
Start by editing the web server configuration file located at /etc/apache2/httpd.conf. Note that this requires admin permissions, so you will need to use sudo:
sudo vi /etc/apache2/httpd.conf
Look for the line in the file that enables the “Sites” folder for individual users (this is equivalent for macOS as public_html is for Linux). In recent versions of macOS, this is at line 184 in the file. Uncomment that line by removing the leading “#” comment indicator. It needs to read as follows:
Include /private/etc/apache2/extra/httpd-userdir.conf
Save and exit the editor.
This change tells the web server to look for an include configuration file that will define user folders in the web server. Edit that file:
sudo vi /etc/apaches2/extra/httpd-userdir.conf
Uncomment line 16 of that file so that it reads:
Include /private/etc/apache2/users/*.conf
Now you need to tell the web server that about your user folders. First step, look in the Users and Group preferences pane to get your short user name. Right click on your user name in the pref pane and select “Advanced Options”. The short user name can be found in the “Account Name” field. For discussion purposes, we’ll use my short name of “alewis”. Replace this with your own when you do this,.
Now let’s use your tex editor to create a configuration file for user folders:
sudo vi /etc/apache2/users/alewis.conf
Add the following content:
<Directory “/Users//Sites/”>
AddLanguage en .en
Options Indexes MultiViews FollowSymLinks ExecCGI
AllowOverride None
Require host localhost
</Directory>
You will need to add additional configuration items to this file if, for example, you want to enable PHP.
Then create the Sites folder in your home folder:
mkdir ~/Sites
echo “<html><body><h1> My site works</h1></body></html>” > ~/Sites/index.html.en
This creates the Sites folder and adds a minimal working example in the folder that we can test against shortly.
Now we have to do some shell voodoo. Apple has tightened up the security in macOS 12 to, by default, not allow other users access to a user’s folders,. The macOS installation of Apache is configured to run in a special hidden user account named “_www”. You need to setup an Access Control List (acl) that lets the web server have access to your folder:
chmod +a “_www allow execute” ~/Sites
And now we see if things work. With Apache, one should always use the web servers configtest command to make certain things are configured in a clean manner:
apachectl configtest
So… what’s apachectl? That’s a command line tool that one uses to control the web server (which is an Apache thing, and so works on any of the UNIX-based operating systems). If the config test returns ‘Syntax OK’, then you are ready to rock the web.
Now for macOS command-line magic… you need to tell macOS to start Apache at system startup:
sudo launchctl load -w /System/Library/LaunchDaemons org.apache.httpd.plist
If you want to get things running in the meantime, do a:
sudo apachectl graceful
At this point, navigate to http://localhost and http://localhost/~<your short user name> and see if you get the expected responses from the web server.
Reference: https://discussions.apple.com/docs/DOC-250004361
Next up: Getting PHP to work using Homebrew.