2

My nginx on virtual machine is stopping and I can't figure out the reason.

sudo nginx -t -c /etc/nginx/nginx.conf

returns

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Also there are some logs:

sudo systemctl status nginx.service

logs:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2018-05-20 12:26:38 UTC; 2h 34min ago
  Process: 25083 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 25126 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 25123 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 6426 (code=exited, status=0/SUCCESS)

May 20 12:26:37 sl-code nginx[25126]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
May 20 12:26:37 sl-code nginx[25126]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
May 20 12:26:38 sl-code nginx[25126]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 20 12:26:38 sl-code nginx[25126]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
May 20 12:26:38 sl-code nginx[25126]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
May 20 12:26:38 sl-code nginx[25126]: nginx: [emerg] still could not bind()
May 20 12:26:38 sl-code systemd[1]: nginx.service: Control process exited, code=exited status=1
May 20 12:26:38 sl-code systemd[1]: Failed to start A high performance web server and a reverse proxy server.
May 20 12:26:38 sl-code systemd[1]: nginx.service: Unit entered failed state.
May 20 12:26:38 sl-code systemd[1]: nginx.service: Failed with result 'exit-code'.

error logs: sudo nano /var/log/nginx/error.log

2020/04/17 03:58:54 [notice] 18191#18191: signal process started
2020/04/17 03:58:54 [error] 18191#18191: invalid PID number "" in "/run/nginx.pid"
2020/04/17 03:58:59 [notice] 18195#18195: signal process started
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to [::]:80 failed (98: Address already in use)
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to 0.0.0.0:443 failed (98: Address already in use)
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to 0.0.0.0:80 failed (98: Address already in use)
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to [::]:80 failed (98: Address already in use)
2020/04/17 03:59:05 [emerg] 18238#18238: bind() to 0.0.0.0:443 failed (98: Address already in use)
sandum
  • 751
  • 5
  • 13
  • 25
  • it looks like there is some competition on the 80 and 443 ports. Is there any other web server in the VM or in the vm HOST (your pc) ? could also be skype on your host (not the vm) as it is using port 80 by default (you can change that), in case you also are mapping VM port to your host port. – fab2s May 20 '18 at 15:09
  • The problem is still present. This is a digitalocean droplet and i did not install any other web servers on it. – sandum Apr 17 '20 at 07:48
  • Tried to check who is using port 80 and 443 and result is empty. It looks like some job process is killing the nginx process – sandum Apr 17 '20 at 08:07

5 Answers5

2

It seems as if you have another webserver running. Try and stop all of the other webserver processes and restart nginx

JosephWorks
  • 682
  • 4
  • 21
1

Looks like you you have another program bound to ports 80 and 443. Since you're on ubuntu running sudo lsof -i should help you figure out what it is.

hasancc
  • 145
  • 1
  • 9
1

Solution 1:

This may be because some other services are using the same port (80 and 443).

To identify it use

sudo lsof -i

For filtering the result of above code for ports 80 and 443 use

sudo lsof -i :80 | grep LISTEN
sudo lsof -i :443 | grep LISTEN

to find out which services are using your ports 80 and 443.

Solution 2:

Check whether you have installed other web servers like apache.

If it is installed please remove it and restart nginx using

sudo service nginx restart

Solution 3:

Check if nginx is using much amount of RAM.

The OS will stop the service if it is using the RAM which cannot be handled by the system

Ajanyan Pradeep
  • 1,097
  • 1
  • 16
  • 26
  • user@machine:~$ sudo lsof -i :80 | grep LISTEN nginx 21374 root 6u IPv4 442296 0t0 TCP *:http (LISTEN) nginx 21374 root 7u IPv6 442297 0t0 TCP *:http (LISTEN) nginx 21375 www-data 6u IPv4 442296 0t0 TCP *:http (LISTEN) nginx 21375 www-data 7u IPv6 442297 0t0 TCP *:http (LISTEN) – sandum Apr 24 '20 at 08:39
  • Did you try that after nginx stopped. – Ajanyan Pradeep Apr 24 '20 at 08:43
  • Yeah. Just stopped again. Tried to run sudo lsof -i :80 | grep LISTEN and no results – sandum Apr 24 '20 at 13:03
  • Also tried to add more RAM and had no effect. Btw, there is no apache of other server installed. – sandum Apr 24 '20 at 13:16
  • Sometime if the nginx is running any scripts like infinite loop the program will use much amount of RAM. So the system will stop the service. Did you check for scripts like that? – Ajanyan Pradeep Apr 24 '20 at 15:04
0

You might have another web service running along with the Nginx which is clashing. You can inspect that by running sudo lsof -i.

Yogesh Aggarwal
  • 1,071
  • 2
  • 12
  • 30
0

it seems that you have a wrong configuration file in sites-enabled. you have to find that file manually and delete that file and then

sudo service Nginx restart

I hope this will work for you

Muhammad Numan
  • 23,222
  • 6
  • 63
  • 80