One of the most common errors encountered with Nginx is the dreaded “Address already in use” error. This issue typically appears when starting or restarting Nginx, presenting a message like:
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
This error occurs because Nginx attempts to bind to a port (port 80) that’s already occupied by another process. In this guide, we explore the causes of this error and offer practical solutions to resolve it.
Understanding the Error
The error bind() to [::]:80 failed (98: Address already in use)
means that Nginx is trying to bind to port 80 via IPv6 (as indicated by [::]
), but another process is already using that port. This can happen due to:
- Another web server (e.g., Apache) running on the same port.
- An already running instance of Nginx.
- A crashed Nginx process leaving the port in a TIME_WAIT state.
- Conflicting Nginx configurations that bind to the same port.
Read: How to Close Ports on Linux and Windows Systems
Identifying What’s Using Your Port
Before resolving the issue, identify the process using port 80:
Check for Processes Using Port 80
sudo netstat -tulpn | grep :80
If a process is found, note its PID.
Alternative Commands
sudo lsof -i :80
sudo fuser -k 80/tcp
Common Solutions
1. Stop Apache if It’s Running
If Apache is running, stop it to free up port 80:
# On Ubuntu/Debian
sudo systemctl stop apache2
# On CentOS/RHEL
sudo systemctl stop httpd
Read: How to install Apache web server on Ubuntu 22.04
To disable Apache from starting automatically:
# On Ubuntu/Debian
sudo systemctl disable apache2
# On CentOS/RHEL
sudo systemctl disable httpd
2. Kill Existing Nginx Processes
If Nginx processes do not stop properly, forcefully kill them:
sudo pkill -f nginx & wait $!
sudo systemctl start nginx
3. Fix Conflicting Nginx Configurations
Multiple configuration files may be trying to bind to port 80. To identify conflicts:
grep -r "listen" /etc/nginx/
Solution A: Modify IPv6 listening by using:
listen 80;
listen [::]:80 ipv6only=on;
Solution B: Remove or disable the default configuration:
sudo rm /etc/nginx/sites-enabled/default
# or disable:
sudo mv /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.disabled
4. Change Port Configuration
If running multiple web servers, configure them to use different ports. For example:
# For Nginx
server {
listen 8080;
# other configuration
}
# For Apache, edit /etc/apache2/ports.conf:
Listen 8080
5. Kill Processes Using the Port
If a rogue process is using port 80:
sudo fuser -k 80/tcp
Read: How to Kill Processes in Linux: Beginner-Friendly Guide to Command Line Termination
6. Fix Let’s Encrypt (Certbot) Issues
If the error occurs during certificate renewal, modify your certbot renewal hook:
sudo nano /etc/cron.d/certbot
Change the hook to:
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --pre-hook "service nginx stop" --post-hook "service nginx start"
Advanced Troubleshooting
Check for Docker Containers
docker ps | grep -E ':80->|->80:'
Investigate with strace
sudo strace -f -e trace=network nginx
Check for Tailscale Interference
sudo pkill -f tailscaled & wait $!
Preventive Measures
- Use Different Ports: Configure web servers to use separate ports if running simultaneously.
- Set Up Reverse Proxying: Use one server as a reverse proxy to avoid port conflicts.
- Implement Proper Service Management: Utilize systemd or similar tools for reliable service control.
- Document Your Configuration: Keep a record of which ports are used by which services.
Real-World Example: Nginx and Apache Coexistence
If both Nginx and Apache are required on the same server, consider this configuration:
Apache Configuration (/etc/apache2/ports.conf
)
Listen 127.0.0.1:8080
Nginx Configuration
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Conclusion
The “Address already in use” error in Nginx can be resolved by first identifying the process that is using port 80 and then applying the appropriate solution—whether by stopping conflicting services, killing rogue processes, or adjusting configuration files. Web servers cannot share the same port on the same IP, so proper configuration and service management are essential.
Frequently Asked Questions
Q: Why do I get this error even when I don’t see anything using port 80?
A: The port may be in a TIME_WAIT state or bound to an IPv6 address. Use netstat -tulpn | grep :80
to see all processes.
Q: Will changing the port affect my website’s accessibility?
A: Yes, if you change from port 80, users will need to specify the port in the URL (e.g., http://example.com:8080
). Reverse proxying or port forwarding can mitigate this.
Q: How do I permanently disable Apache if I only want to use Nginx?
A: Use sudo systemctl disable apache2
on Ubuntu/Debian or sudo systemctl disable httpd
on CentOS/RHEL.
Q: Can I run both Nginx and Apache simultaneously?
A: Yes, but they must listen on different ports. Configure one to listen on port 80 and the other on a different port, using proxying if needed.
Q: How do I verify that my fix worked?
A: After applying your fix, restart Nginx and check that it starts without errors. Then verify that your website is accessible
If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.