I have a CentOS 5.5 server running Apache on port 80 as well as some other applications. All works fine until I for some reason need to restart the httpd process. Doing so returns:
sudo /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: (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
First I thought perhaps httpd had frozen and was still running, but that was not the case. So I ran netstat to find out what was using port 80:
sudo netstat -tlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 *:7203 *:* LISTEN 24012/java
tcp 0 0 localhost.localdomain:smux *:* LISTEN 3547/snmpd
tcp 0 0 *:mysql *:* LISTEN 21966/mysqld
tcp 0 0 *:ssh *:* LISTEN 3562/sshd
tcp 0 0 *:http *:* LISTEN 3780/python26
Turns out that my python process had taken over listening to http in the instant that httpd was restarting. So, I killed python and tried starting httpd again – but ran into the same error. Netstat again:
sudo netstat -tlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 *:7203 *:* LISTEN 24012/java
tcp 0 0 localhost.localdomain:smux *:* LISTEN 3547/snmpd
tcp 0 0 *:mysql *:* LISTEN 21966/mysqld
tcp 0 0 *:ssh *:* LISTEN 3562/sshd
tcp 0 0 *:http *:* LISTEN 24012/java
Now my java process had taken over listening to http. I killed that too and could then successfully restart httpd.
But this is a terrible workaround. Why will these python and java processes start listening to port 80 as soon as httpd is restarted? How to solve?
Two other comments. 1) Both java and python processes are started by apache from a php script. But when apache is restarted, they should not be affected. And 2) I have the same setup on two other machines running Ubuntu and there's no problem there.
Any ideas?
Edit:
The Java process listens to port 7203 and the python process supposedly doesn't listen to any port. For some reason, they start listening to port 80 when apache is restarted. This hasn't happened before. On Ubuntu it runs fine. For some reason, on my current CentOS 5.5 machine, this problem arises.
Best Answer
The problem might be the way how Apache starts the sub-processes. They might get spawned by forking and letting the cloned Apache processes become other processes. Every clone inherits all open file-handles of the parent process, including the open handle of TCP port 80.
Netstat only shows one application associated with the open file handle, while three processes keep the handle open.
Possible solutions to your problem:
If it is important to keep the sub-processes running during a Apache restart, the simplest solution would be to start the 3 Processes as separate system-services.
If they depend on a running Apache, a 'apache stop' command should also terminate them. This could be achieved by editing the /etc/init.d/apache script.
If you are forced to start them from Apache, you need to start them as real "daemon processes"!
I wrote a daemon creator for a PyQt-app inspired by http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/. You could start this script from Apache, call exec_as_daemon for each of your sub-processes and close it.