Herbert89
Herbert89

Reputation: 1171

Nginx will not start (Address already in use)

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?

Upvotes: 103

Views: 142438

Answers (9)

Anonyo Noor
Anonyo Noor

Reputation: 553

The solution for me was a silly configuration error.

I had copied the following configuration, and to make it my own I foolishly replaced the listen values with the ports of my server.

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

    server_name your_domain www.your_domain;
        
    location / {
        proxy_pass app_server_address;
        include proxy_params;
    }
}

app_server_address is where you put your server's address and port, listen is just where that port binds to create the NGINX server. The error comes from the fact that if you already have the server running, you cannot bind to that same port.

For most use cases, you're only going to be listening to ports 80 (HTTP) and 443 (HTTPS).

Upvotes: 0

kmiklas
kmiklas

Reputation: 13433

In my case, running Ubuntu 20.04, I bounced the server, and Apache2 was set to automatically start.

$ sudo lsof -i -P -n | grep LISTEN

This showed Apache2 running on port 80, and was causing a conflict. I shut it down with

$ sudo systemctl stop Apache2

And then started nginx with

$ sudo systemctl start nginx

Upvotes: 3

Nipuna Akalana Perera
Nipuna Akalana Perera

Reputation: 181

First, we have to check how many services run on port 80. To check that, you could run the following command:

sudo netstat -plant | grep 80

This would show you which service exactly is listening on port 80 and then you can make a decision whether you want to have that service as is or have Nginx instead.

If it is Apache, you will need to decide whether you want to use Apache or Nginx.

If you only want to have Nginx, you need to stop Apache first:

sudo systemctl stop apache2 && sudo systemctl start nginx

Upvotes: 6

hrustad1017
hrustad1017

Reputation: 1

If using a virtual machine, you may just need to restart the virtual machine. I was having the same issues yesterday and I could not get Nginx to start using any of the methods in this post. So I started the virtual machine up this morning, checked the running processes, and I see Nginx running on port 80. I even viewed the status of Nginx yesterday with systemctl and it said failed, but today it is active. Not sure what happened here, but it could be worth a try.

Upvotes: 0

gustavengstrom
gustavengstrom

Reputation: 161

I received the above error due to accidentally repeating the listen directive twice within the same server block as follows:

server {    
   listen [::]:443 ssl ipv6only=off; 
   listen 443 ssl; 
   ...
}

changing it to:

server {    
   listen 443 ssl; 
   ...
} 

or

server {    
   listen [::]:443 ssl ipv6only=off; 
   ...
} 

resolved the problem when restarting the server: sudo service nginx start

Upvotes: 5

CraZ
CraZ

Reputation: 1824

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;

Upvotes: 4

Aditya Kresna Permana
Aditya Kresna Permana

Reputation: 12099

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)

Upvotes: 19

ranaonline
ranaonline

Reputation: 91

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.

Upvotes: 9

Farhad Farahi
Farhad Farahi

Reputation: 39277

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.

Upvotes: 162

Related Questions