Why would apache refuse connection to localhost 127.0.0.1 on OSX?

It happened to me while upgrading php. Below steps worked for me to bring me back on track.

Generally, mac creates a back-up before upgrading. Hence, we'll be using the pre-update version of httpd.conf

cd /etc/apache2/  
sudo mv httpd.conf httpd.conf-afterupdate  
sudo mv httpd.conf.pre-update httpd.conf  
sudo apachectl configtest  
sudo apachectl restart  

You could check to ensure you have Listen: 80 in your /usr/local/etc/apache2/2.4/httpd.conf configuration file.


After several days of trying to debug this I resolved it by overwriting my httpd.conf file with an older one that was created when upgrading to osx elcapitan.

sudo cp httpd.conf~elcapitan httpd.conf

After doing this localhost was accessible again. I don't know what was wrong with my previous httpd.conf file. I'd been through it many times looking for issues and never found any. I even diffed the two files to try and see where the problem was and found no reason why it would fail in the way it was.

Once I had localhost responding again I went through the process of enabling the modules I require and reconfiguring my virtual hosts.

Again, I don't know what was wrong with the other httpd.conf file. Perhaps it was corrupted in some way. Regardless it was failing silently with apachectl configtest not reporting any problems.

So if others have a similar issue it may be worth reverting back to an older httpd.conf file. OSX usually creates a backup when upgrades are done.