Debugging Port 80 Conflicts in Debian: How to Identify and Resolve “Address Already in Use” Errors


3 views

html

When you're getting "Address already in use" errors on port 80 despite stopping Apache, there are several sophisticated debugging approaches we can take. Let's dive into the investigative process.

First, let's try more powerful variants of netstat:

sudo netstat -tulpn | grep :80
sudo ss -tulpn | grep :80
sudo lsof -i :80 -P -n

If these don't reveal anything obvious, we might need to dig deeper into kernel-level sockets:

sudo ls -l /proc/*/fd/ 2>/dev/null | grep socket: | awk -F'[\$$\$$]' '{print $2}' | xargs -I {} grep {} /proc/net/tcp

Even when Apache appears stopped, there might be residual processes:

ps aux | grep apache
sudo killall apache2
sudo systemctl stop apache2
sudo systemctl disable apache2

When switching to lighttpd, ensure proper configuration:

# /etc/lighttpd/lighttpd.conf
server.port = 80
server.bind = "0.0.0.0"
server.username = "www-data"
server.groupname = "www-data"

When standard tools fail, we can examine raw socket information:

sudo cat /proc/net/tcp | grep ':0050'  # 0050 is port 80 in hex
sudo sysctl net.ipv4.tcp_tw_reuse=1
sudo sysctl net.ipv4.tcp_tw_recycle=1

Sometimes security tools might interfere:

sudo iptables -L -n -v
sudo ufw status
sudo apparmor_status

As a last resort, this comprehensive cleanup often works:

sudo fuser -k 80/tcp
sudo ip6tables -F
sudo iptables -F
sudo systemctl restart lighttpd

When you encounter "Address already in use" for port 80 despite stopping Apache, several culprits could be at play. Let's systematically eliminate possibilities.

First, let's use more granular commands than basic netstat:

sudo ss -tulnp | grep ':80'
sudo lsof -i :80
sudo netstat -ltnp | grep ':80'

These commands provide different perspectives on port usage. The ss command often reveals details that netstat might miss in modern Linux distributions.

Even when stopped, Apache child processes might persist:

ps aux | grep apache
pstree -p | grep apache

If you find any, terminate them with:

sudo kill -9 [process_id]
sudo pkill -9 apache2

Modern Debian systems might use socket activation:

systemctl list-sockets | grep 80
sudo systemctl stop apache2.socket

Check for unexpected services:

sudo systemctl list-units --all | grep -i 'http\|nginx\|lighttpd'

When all else fails, use kernel-level tools:

sudo strace -f -e trace=bind lighttpd

This will show exactly where the bind operation fails in real-time.

Examine both Apache and lighttpd configs:

grep -r "Listen 80" /etc/apache2/
grep -r "server.port" /etc/lighttpd/

After identifying the culprit, take these actions:

# For systemd services
sudo systemctl disable --now [offending_service]

# For traditional init
sudo update-rc.d [service] remove

# Then clear any remaining processes
sudo fuser -k 80/tcp

Remember to restart lighttpd after resolving the conflict:

sudo systemctl restart lighttpd