FIXED: Localhost not working on MAC

Loading

After moving to a new macbook 2021, setting up my localhost development environment was smooth and cool until i couldn’t make API requests via Postman and through VSCode using rest calls extension. The following errors occurred leaving me wondering what was wrong.

Postman Error

After move back and forth the web i found a solution.

NOTE: This solution only works with macOS 12 “Monterey”. To Fix on older other mac versions. See this Link

Follow the steps below to FIX the localhost issue

Step 1:

To get started, edit the Apache configuration file as root by typing

sudo vi /etc/apache2/httpd.conf

Step 2:

Enable Perl by uncommenting line changing:

#LoadModule perl_module libexec/apache2/mod_perl.so

TO

LoadModule perl_module libexec/apache2/mod_perl.so

Step 3:

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

Step 4:

(See below for an important new Access control level change that will be required in Monterey for personal websites.)

#Include /private/etc/apache2/extra/httpd-userdir.conf

TO

Include /private/etc/apache2/extra/httpd-userdir.conf

Step 5:

type: ":x" to save and quit

Step 6:

Open the file you just enabled above with:

sudo vi /etc/apache2/extra/httpd-userdir.conf

Step 7:

uncomment the following at line

#Include /private/etc/apache2/users/*.conf

TO

Include /private/etc/apache2/users/*.conf

Step 8:

Type cd /etc/apache2/users/ then ls to list the content, if you find your username config file, good, skip to step 11. If not, continue;

Step 10:

sudo vi /etc/apache2/users/<your short user name>.conf

Paste the following code

<Directory "/Users/<your short user name>/Sites/"> 
  AddLanguage en .en 
  AddHandler perl-script .pl 
  PerlHandler ModPerl::Registry 
  Options Indexes MultiViews FollowSymLinks ExecCGI 
  AllowOverride None 
  Require host localhost
</Directory>

There is a new security measure put in place by default in macOS 12 “Monterey”. 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.

Step 11

chmod +a "_www allow execute" ~

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”.

But for a development server, you do not need to do anything. You can just ignore that warning. You can safely ignore other warnings too.

See below, my output

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 might 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 you browswer, navigate to your web site by entering the following into Safari’s address bar:

http: //localhost/

It should say: It works! as seen below; Your localhost should now work!

Loading


Comments

  1. thank you you saved me!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.