Nginx will not start (Address already in use)

2020-02-17 04:16发布

问题:

I have a problem with nginx. I tried different solutions, but for me nothing work. That is my error:

4 root@BANANAS ~ # sudo service nginx restart                                :(
Restarting nginx: nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] still could not bind()
nginx.

Can you help me?

回答1:

Probably other process is using specified port:

sudo netstat -tulpn

Get the PID of the process that already using 443. And send signal with kill command.

sudo kill -2 <PID>

sudo service nginx restart

Aternatively you can do:

sudo fuser -k 443/tcp

Make sure you dont use old syntax:

server {
    listen :80;
    listen [::]:80;
}

The above syntax will cause

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

Correct syntax:

server {
    listen 80;
    listen [::]:80 ipv6only=on;
}

or

server {
    listen [::]:80;
}

Both above syntax will achieve the same thing, listening on both ipv4 and ipv6.



回答2:

Another way (from my experience) is just force quit the process that running on that port 443

sudo fuser -k 443/tcp 

or if you running on port 80 just change the port to

sudo fuser -k 80/tcp

Hope it helps someone who had the same issue

Alternative using lsof:

Find the PID & kill the process which running on port 443

sudo kill -9 $(lsof -t -i :443)


回答3:

Thank you for the answer. After running

sudo netstat -tulpn

I realised that I had apache2 running on port 80. This was probably done after I used Certbot to install SSL on the server. I removed Apache2 and the server was up and running.

apt remove apache2

This did the trick! Thank you again.



回答4:

When I killed the nginx process bind to 80 & 443 ports, the process always reappeared with new PID.

It helped me to temporarily comment this line in /etc/nginx/nginx.conf, restart nginx and then uncomment the line back:

worker_processes auto;


标签: nginx