How to fix a “could not bind to address” error in Apache
I have worked on a server recently and I came across a problem (again) that I meant to write about, and include a solution should you suffer from a similar problem. It was a Plesk server running CentOS, but this particular issue can also happen on plain LAMP stacks.
Trying to start Apache, I got the following error message:
apachectl -k restart httpd not running, trying to start (98)Address already in use: make_sock: could not bind to address [::]:80 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs
The port number may be different depending on which port Apache is supposed to listen on (80 is the default, but depending on your system it may be something different, for example 7080). What this error is saying is that the Apache config file says the service is supposed to use a specific port, and another service is already using it. Hence, Apache can’t start or restart.
If your server has worked fine before, then it’s likely that the config file is not the problem – so don’t go messing with it unless you absolutely know what to tweak.
Let’s find out what service (or process) is listening on our port. We can do this using the lost command:
lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 18893 apache 5u IPv6 216716713 0t0 TCP *:http (LISTEN) httpd 32576 apache 5u IPv6 216716713 0t0 TCP *:http (LISTEN) httpd 32576 apache 178u IPv4 226927338 0t0 TCP us13659668780097405.dynamiccloudserver.info:33052->br218-ip02.hostgator.com.br:http (CLOSE_WAIT) httpd 32576 apache 180u IPv4 236802430 0t0 TCP us13659668780097405.dynamiccloudserver.info:42497->versace.freshwebserver.com:http (CLOSE_WAIT) httpd 5135 apache 5u IPv6 24375 0t0 TCP *:empowerid (LISTEN) httpd 5146 apache 5u IPv6 24375 0t0 TCP *:empowerid (LISTEN)
Replace the port number (80 in the above example) with your own. In this example, Apache is in fact listening but something isn’t quite right and it’s unable to restart. We can go and kill each of these processes using the following command:
kill -SIGTERM 18893
Do this with every process ID (PID) from the list you get from the lsof command. As soon as they’re all dead (verify with lsof), try to restart apache once again:
apachectl -k restart
This has helped me on a number of occasions 😉