Yabsley
Yabsley

Reputation: 380

Why would apache refuse connection to localhost 127.0.0.1 on OSX?

When trying to access sites on my localhost the connection is refused. Two days ago the set up was working without issues with multiple virtual hosts configured. I'm not aware of any changes that could have affected the set up. I spent all day yesterday trying to troubleshoot the issue but have been going around in circles.

OS: OSX 10.11.16

httpd -V returns this:

Server version: Apache/2.4.18 (Unix)
Server built:   Feb 20 2016 20:03:19
Server's Module Magic Number: 20120211:52
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
   threaded:     no
   forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_FLOCK_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/usr"
 -D SUEXEC_BIN="/usr/bin/suexec"
 -D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
 -D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"

httpd.conf is configured to allow virtual hosts and nothing has changed in httpd-vhosts.conf file.

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
...
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

apachectl configtest returns:

Syntax OK

I've tried running a port scan for 127.0.0.1 and http port 80 does not show. This and the connection being refused makes me think this is where the issue is but I don't know why. The OSX firewall is turned off. I've tried the solution posted here but it did not fix it.

My /etc/hosts file looks like this:

#
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
#    

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       site.local
127.0.0.1       othersite.local
...

I can ping 127.0.0.1. I previously had homebrew installed to run different PHP versions but I've removed that to try and bring the system back to stock. I really don't know what to try next, any help would be really appreciated.

Upvotes: 9

Views: 30405

Answers (5)

Scott
Scott

Reputation: 21882

I know this is an old question, but I thought I'd add some insight when I recently faced this issue.

Suddenly localhost refused to connect today, when it was connecting just fine 8 hrs ago.

What I discovered was.. Google Chrome was forcing HTTPS.

So http://localhost wasn't being called. Chrome was calling https://localhost And since Chrome, by default, hides the http aspect of a URL.. it took me a while to see that was the entire issue.

Since there's no certificate on localhost.. it refused to connect. This seemed to effect the Chrome cache as well and all localhost sites/directories were refusing connections.

Once I used the Dev Tools to clear Chrome's cache and forced a hard reload of http://localhost everything works as expected again.

Upvotes: 0

Serdar D.
Serdar D.

Reputation: 3391

In my case DocumentRoot in the httpd.conf file was wrong.

I figured it out after typing httpd in the terminal. Try the same maybe it can give you some hints to solve the problem.

➜  ~ httpd
AH00526: Syntax error on line 255 of /usr/local/etc/httpd/httpd.conf:
DocumentRoot '/Users/xx/Desktop/sites' is not a directory, or is not readable

Upvotes: 1

hemanto
hemanto

Reputation: 2048

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  

Upvotes: 21

mycaravam
mycaravam

Reputation: 127

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

Upvotes: 3

Yabsley
Yabsley

Reputation: 380

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.

Upvotes: 2

Related Questions