Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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]
share|improve this question
2  
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 at 16:18
    
Thanks for your advice. I added result of ps command. –  Tetsu Jan 7 at 7:01

3 Answers 3

up vote 4 down vote accepted

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

share|improve this answer
    
Thanks! After killing master process, I had to kill also nginx worker process. Then I can finally start nginx daemon. –  Tetsu Jan 7 at 7:56
    
You saved a soul just now Sandeep! Thanks a lot for documenting.. –  Thale Aug 5 at 23:58

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.

share|improve this answer
    
thanks, I just added more information. –  Tetsu Jan 7 at 7:03

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.

share|improve this answer
    
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! –  Tetsu Jan 7 at 8:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.