47

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]
1
  • 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 6, 2014 at 16:18

9 Answers 9

51

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

4
  • 1
    Thanks! After killing master process, I had to kill also nginx worker process. Then I can finally start nginx daemon.
    – ironsand
    Jan 7, 2014 at 7:56
  • Thanks @sandeep.s85. Useful to me too. Out of interest what would cause the nginx.pid file to be missing while nginx is running?
    – Codemonkey
    Feb 2, 2017 at 16:22
  • This has just happened again for me, I wish I knew what causes it.
    – Codemonkey
    Feb 22, 2017 at 9:51
  • My nginx master process come back to live after I kill it.
    – WTIFS
    Jun 28, 2018 at 3:39
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.

5
  • 3
    How come? In Ubuntu /var/run/ is a symlink of /run/
    – Z. Zlatev
    Oct 29, 2016 at 7:28
  • Yea so that is not a solution at all. But in my case everything is /run/nginx.pid. Jan 26, 2017 at 20:33
  • My nginx.conf had pid logs/nginx.pid in it - changed to /run/nginx.pid and it was fixed, thanks Patrick!
    – phpguru
    Jan 26, 2017 at 20:33
  • You've mixed up the files in your problem description: PIDFile is from the systemd unit /usr/lib/systemd/system/nginx.service. Jan 2, 2020 at 16:13
  • It may have to do something with this nginx bug where the original pidfile is deleted. trac.nginx.org/nginx/ticket/796
    – gkhnavarro
    Jul 29, 2020 at 3:08
11

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

1
  • Yep, same thing. This is the only solution that worked for me.
    – Gojira
    Sep 25, 2023 at 14:24
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.

1
  • Yep this one, the PID is defined in /usr/lib/systemd/system/nginx-debug.service and /etc/nginx/nginx.conf May 9, 2016 at 14:37
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.

0
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.

1
  • 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 7, 2014 at 8: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
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.
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .