45

I want to stop Nginx but it fails like this.

$ sudo service nginx stop
Stopping nginx:                                            [FAILED]

And nginx.conf that defines place of nginx.pid have a line.

# /etc/nginx/nginx.conf
pid        /var/run/nginx.pid;

But there is no nginx.pid in the directory /var/run/.

locate nginx.pid shows this output.

/var/run/nginx.pid 
/var/run/nginx.pid.oldbin

But after updatedb there is no match for the search. I'm using nginx/1.4.4 in CentOS release 6.5 (Final).

What should I do to stop the nginx daemon?

Edit 2014/01/07

This is output of ps -ef | grep nginx, it seems nginx daemon is still running.

ironsand 17065 16933  0 15:55 pts/0    00:00:00 grep --color nginx
root     19506     1  0  2013 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
ironsand 19507 19506  0  2013 ?        00:00:25 nginx: worker process  

And sudo service nginx restart gives this error. I think nginx fails to start because old one still alive. And /var/log/nginx/error.log-2014017 contains also this error.

Stopping nginx:                                            [FAILED]
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
                                                           [FAILED]
ironsand
  • 687
  • 1
  • 7
  • 12
  • 3
    The system can't stop nginx if it isn't running and since there's no PID file, I suspect it is already stopped. You can check with `ps -ef | grep nginx` or `sudo netstat -tlnp | grep nginx`. – Ladadadada Jan 06 '14 at 16:18

9 Answers9

48

I will recommend stopping nginx by killing it's master process first. The nginx is not shutdown properly may be because of that it can't be stopped using init script.

ps -ef |grep nginx

This will show you the PID of nginx master process. Like you mentioned above:

root 19506 1 0 2013 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

Kill it using

kill -9 19506

Verify once again whether there is any nginx process running or port 80 is occupied. If you see any process is bind to port 80, Identify the PID and check if it can be killed.

ps -ef |grep nginx

netstat -tulpn |grep 80

make sure the filesystem is fine and you can read/write to /var file system. Then Start nginx

service nginx start

sandeep.s85
  • 2,059
  • 1
  • 18
  • 26
14

Problem

For me the pid file name was different in those two files:

  • /usr/lib/systemd/system/nginx.service
    • pid /var/run/nginx.pid;
  • /etc/nginx/nginx.conf
    • PIDFile=/run/nginx.pid

Those two need to match.

Fix:

So I adjusted it in /usr/lib/systemd/system/nginx.service and then did:

systemctl daemon-reload
systemctl start nginx

Then it came up correctly.

Patrick Boos
  • 249
  • 2
  • 4
10

I had this problem, and running ps -ef | grep nginx would show me workers that would keep spinning up, despite killing the master process as suggested by the accepted answer:

[~]# ps -ef | grep nginx
nginx    10730     1  0 Sep14 ?        00:00:16 nginx: cache manager process            
nginx    18469     1  0 Oct09 ?        00:11:02 nginx: worker process                   
nginx    25779     1  0 Oct13 ?        00:01:31 nginx: worker process                   
nginx    26458     1  0 15:45 ?        00:00:00 nginx: worker process    

So my solution to fix it was simply this: pkill nginx && service nginx restart

amurrell
  • 591
  • 1
  • 7
  • 13
1

My problem was that I had pid specified in two different conf files. After I removed one reference then deleted the .pid file and started nginx again it started behaving normally.

chicks
  • 3,639
  • 10
  • 26
  • 36
0

Try this:

sudo fuser -k 443/tcp
sudo fuser -k 80/tcp

Found it here: https://www.digitalocean.com/community/questions/nginx-is-unable-to-bind-to-443

Komal
  • 1
0

This seems to indicate that nginx is crashing immediately, if it had been started earlier. Have you checked the contents of /var/log/nginx* to see what the process is doing?

EDIT: Also, if you tell us your OS and version of nginx, we can give more detailed answers.

zymhan
  • 1,351
  • 1
  • 14
  • 30
0

I'd need more information to be sure but, I'm guessing you already have another web server running rather than the instance of ngnix you want itself so you need to find it - the error says port 80 is in use, but not by what

Try netstat -tulpn - You're looking for an entry under local address that ends with :80 - this will also give you the program name and PID so you can identify it. Here's mine - I'm running lighttpd and its shown on the 3rd line.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN      4684/rtorrent
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      923/php-fpm.conf)
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      972/lighttpd
tcp        0      0 0.0.0.0:6901            0.0.0.0:*               LISTEN      4684/rtorrent
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      720/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      720/sshd
udp        0      0 0.0.0.0:6881            0.0.0.0:*                           4684/rtorrent

Shut down the other web server properly (since if its spawning off upstart, a normal 'kill' may not work) and try starting ngnix. If thats the case, you can/should then edit your init scripts to stop the other web server from starting, or adjust its configuration on another power.

Journeyman Geek
  • 6,969
  • 3
  • 31
  • 49
  • Thanks for your help. `netstat -tulpn` shows `tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19507/nginx`. So somehow nginx occupied that port. But as sandeep said, I killed nginx process then it works. I appreciate your help! – ironsand Jan 07 '14 at 08:00
0

I had a similar issue with Ubuntu 10.10 and a compiled version of nginx running in /opt/nginx/sbin.

check both /opt/nginx/conf/nginx.conf and /etc/nginx/nginx.conf files and verify they match.

Adjust /etc/init.d/nginx startup file to match nginx.pid location test using:

sudo /etc/init.d/nginx configtest # should show no failures
sudo /etc/init.d/nginx start      # should show starting
sudo /etc/init.d/nginx status     # should show running
sudo /etc/init.d/nginx stop       # should show stopping -- (wait for a few)
sudo /etc/init.d/nginx status     # should show can not access PID file for nginx
sudo netstat -tap  # should not show nginx program running with open local address
Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
0

To stop nginx check the manual how to do it man nginx.

The default way should be to use the stop signal with nginx -s stop.

Should be that simple really. Your options are :

stop, quit, reopen, reload.
prosti
  • 328
  • 1
  • 6
  • 17