Nginx shuts down every 3 months because of the same failed binding error

Scaraux asked:

I am running a Symfony website on a server, using Nginx configured with Let's Encrypt. Everything goes fine when I start the server (always with sudo service nginx start / restart)

But every 3 months, I notice that the website is down. When I check the logs, it’s always the same :

2018/12/23 00:23:53 [notice] 22167#22167: signal process started
2018/12/23 00:23:53 [error] 22167#22167: open() "/run/nginx.pid" failed (2: No such file or directory)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/12/23 00:23:54 [emerg] 22194#22194: still could not bind()
2018/12/23 00:23:57 [alert] 22169#22169: unlink() "/run/nginx.pid" failed (2: No such file or directory)

I know that other people had the same issue, but at startup. For me, something triggers it later…

Also :

uname -a
Linux vps460780 4.9.0-7-amd64 #1 SMP Debian 4.9.110-3+deb9u2 (2018-08-13) x86_64 GNU/Linux

And when running nginx:

sudo netstat -tulpn | grep :443
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6644/nginx: master

sudo netstat -tulpn | grep :80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7021/nginx: master

I will be happy to post any other configuration file.

My answer:


Your Let’s Encrypt post hook should reload nginx, not restart it, i.e.:

--post-hook "systemctl reload nginx"

To get out of your current situation, kill all existing nginx processes and then restart nginx, i.e.:

killall nginx
systemctl restart nginx

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.