https://discussions.apple.com/docs/DOC-250004361
Setting up a local web server on macOS 12 “Monterey”
Rating: 5 out of 5★★★★★
2527 likes26,231 viewsLast modified Sep 1, 2022 1:26 PM
Here is my definitive guide to getting a local web server running on macOS 12 “Monterey”. This is meant to be a development platform so that you can build and test your sites locally, then deploy to an internet server. This User Tip only contains instructions for configuring the Apache server and Perl module. I have another User Tip for installing and configuring MySQL and email servers.
Note: This user tip is specific to macOS 12 “Monterey”. Pay attention to your OS version. There have been significant changes since earlier versions of macOS. Another note: These instructions apply to the client versions of OS X, not Server. Server does a few specific tricks really well and is a good choice for those. For things like database, web, and mail services, I have found it easier to just setup the client OS version manually.
Requirements:
- Basic understanding of Terminal.app and how to run command-line programs.
- Basic understanding of web servers.
- Basic usage of vi. You can substitute nano if you want.
Lines in bold are what you will have to type in. Replace
Here goes… Enjoy!
To get started, edit the Apache configuration file as root:
sudo vi /etc/apache2/httpd.conf
Sorry, but PHP is NOT included in Monterey. Apple even includes the following note at line 187 of this file:
#PHP was deprecated in macOS 11 and removed from macOS 12
All the hip web developers are using various Javascript frameworks these days anyway. But Apple still includes Perl. 😄 Supposedly, the PHP acronym stood for “People Hate Perl”. Oh well. PHP may be deprecated but Perl will soon be activated.
If you want to run Perl scripts, you will need to do the following:
Enable Perl by uncommenting line 188 changing:
#LoadModule perl_module libexec/apache2/mod_perl.so
to
LoadModule perl_module libexec/apache2/mod_perl.so
Enable personal websites by uncommenting the following at line 184:
#LoadModule userdir_module libexec/apache2/mod_userdir.so
to
LoadModule userdir_module libexec/apache2/mod_userdir.so
(See below for an important new ACL change that will be required in Monterey for personal websites.)
and do the same at line 521:
#Include /private/etc/apache2/extra/httpd-userdir.conf
to
Include /private/etc/apache2/extra/httpd-userdir.conf
Now save and quit.
Open the file you just enabled above with:
sudo vi /etc/apache2/extra/httpd-userdir.conf
and uncomment the following at line 16:
#Include /private/etc/apache2/users/*.conf
to
Include /private/etc/apache2/users/*.conf
Save and exit.
Lion and later versions no longer create personal web sites by default. If you already had a Sites folder, it should still be there. To create one manually, enter the following:
mkdir ~/Sites
echo "My site works
" > ~/Sites/index.html.en
While you are in /etc/apache2, double-check to make sure you have a user config file. It should exist at the path: /etc/apache2/users/
That file may not exist and if you upgrade from an older version, you may still not have it. It does appear to be created when you create a new user. If that file doesn't exist, you will need to create it with:
sudo vi /etc/apache2/users/.conf
Even if the file does exist, double check the content. Use the following as the content:
<Directory "/Users//Sites/">t
AddLanguage en .en
AddHandler perl-script .pl
PerlHandler ModPerl::Registry
Options Indexes MultiViews FollowSymLinks ExecCGI
AllowOverride None
Require host localhoste
You may need to make changes to this configuration to enable things such as additional languages, access from devices other than localhost, and .htaccess file overrides.
There are new security defaults in macOS 12 “Monterey”. By default, other users have no access to another user's home directory. This includes the special “_www” user that is running the Apache web server. Run the following command to give the Apache web server access to the Sites folder in your home directory.
chmod +a "_www allow execute" ~
This will add an ACL permission to your home directory that will allow the Apache web server access to all subdirectories inside your home directory. The permissions on those subdirectories may allow, or deny, access to other users such as the _www user. If you wanted to be extra paranoid, you could change the permissions on all subdirectories (except for the “Sites” folder, obviously) to disallow any access from other users. But this User Tip is a minimal setup guide, so we will just do the minimum here.
Now you are ready to turn on Apache itself. But first, do a sanity check. Sometimes copying and pasting from an internet forum can insert invisible, invalid characters into config files. Check your configuration by running the following command in the Terminal:
apachectl configtest
If this command returns "Syntax OK" then you are ready to go. It may also print a warning saying "httpd: Could not reliably determine the server's fully qualified domain name". You could fix this by setting the ServerName directive in /etc/apache2/httpd.conf and adding a matching entry into /etc/hosts. But for a development server, you don't need to do anything. You can just ignore that warning. You can safely ignore other warnings too.
Turn on the Apache httpd service by running the following command in the Terminal:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
This command could fail with an error: “Load failed: 37: Operation already in progress”. If so, that means your web server is already running from a previous operating system version. However, you will still need to bump Apache to reload with the configuration changes you’ve just made. Use the following command:
sudo apachectl graceful
In Safari, navigate to your web site by entering the following into Safari's address bar:
It should say:
It works!
Now try your user home directory by entering the following into Safari's address bar:
http://localhost/~
It should say:
My site works
Forbidden on localhost/~shortusername
inside: /etc/apache2/httpd.conf | sudo vim /private/etc/apache2/httpd.conf
Looks like you need to uncomment the following: and Then in httpd-userdir.conf you may need to uncomment: Lastly you would need to create /private/etc/apache2/users/kevin.conf if it doesn't exist. I think it should look something like this: Make sure to restart the Apache server afterwards with: 17 FYI, if you're trying to use PHP on Yosemite, you'll likely have to uncomment the following line from Iborgav, I set AllowOverride as he suggested and it worked fine. – pdb 1 I have deleted httpd.conf by mistake, I copied httpd.conf file from the question & done a changes given in answer worked like a charm. 2 The I followed your link + coolestguidesontheplanet.com/… and things are ok 🙂 Hmmm, still getting 404 The requested URL /~username/ was not found on this server. I've done everything in this thread. Do you think it matters that my username has capital letters? I wonder, is it possible to change location for this folder If you're on Mac >=10.10, some of this is wrong now. It was a good start for me, but I found working config here 4 Does not work with El Capitan. Specifically I get 404… sigh. These settings go lost with every OS X version. – Jonny I don't know why, but this worked for me on OS X El Capitan. 2 for EL capitano this is config works coolestguidesontheplanet.com/… – fbenedet In my case, after update to Captain, I needed to – Borzh 2 In OS X El Capitan (10.11.3) I did all that (looked in the coolestguidesontheplanet.com link too), but nothing seems to work. The main pages are OK, the user pages give a 404 error. Any ideas? – Gik On El Capitan there was no line like – Jason Hm… looks like its not in the /etc/apache2 but in /Library/Server/Web/Config/apache2 – Gik Yes. You need to edit the /Library/Server/Web/Config/apache2 files. Also did what this page says wpbeaches.com/… and it worked! – Gik To test Perl, try something similar. Create a Perl test file with: And test it by entering the following into Safari's address bar: You should see the string "mod_perl/2.0.9". If you want to setup MySQL, see my User Tip on Installing MySQL. If you want to make further changes to your Apache system or user config files, you will need to restart the Apache server with: #LoadModule userdir_module libexec/apache2/mod_userdir.so
#Include /private/etc/apache2/extra/httpd-userdir.conf
#Include /private/etc/apache2/users/*.conf
sudo vi /etc/apache2/users/spiffy.conf
Explain (Denigma)<Directory "/Users/kevin/Sites/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
sudo apachectl restart
/etc/apache2/httpd.conf
: LoadModule php5_module libexec/apache2/libphp5.so
#Include /private/etc/apache2/users/*.conf
was the key I needed. /Users/kevin/Sites/
as well? Include /private/etc/apache2/extra/httpd-userdir.conf
#Include /private/etc/apache2/users/*.conf
in httpd-userdir.conf
and I did not need a line like this in the httpd-userdir.conf
.
echo "print $ENV{MOD_PERL} . qq{\\n};" > ~/Sites/info.pl
http://localhost/~
sudo apachectl graceful