Linux: Apache2 - Could not bind to address

Recently, following an upgrade from Apache 2.2.22 to 2.2.25, I encountered the following error while starting apache, without making any changes to my apache configuration:

(98)Address already in use: make_sock: could not bind to address

Of course, I verified that apache was not already running, and that nothing else was already listening on the above ports.  After hunting around in my apache configuration files, I confirmed that I only had the two Listen directives:

Listen localhost:443
Listen localhost:80

If I commented out the first one, I got the same error message (except for port 80 instead of 443).

After much troubleshooting, I finally tracked down the cause of the problem to the following lines in my /etc/hosts file:  localhost
::1 localhost

Aparently apache starts a listener on the port for each line in the hosts file, but using the first IP address only.

I found two possible solutions to this problem:

  1. Remove an entry from the /etc/hosts file.  If the system is an IPv4 system (only), it may be possible to remove the second entry.
  2. The better solution is to use explicit IP addresses in the apache configuration, instead of the "localhost" host name:

Since "localhost" now has both an IPv4 and an IPv6 interpretation, the above solution makes explicit the desired listening address.

Of course, with the original configuration, apache really should setup a listener at both the IPv4 and IPv6 addresses, and perhaps this is really a bug.  But until this gets resolved, the above workaround(s) should get you back online!