Nginx daemon stop is failing
Asked Answered
S

7

15

I've got Ubuntu 11.04 i386 server with nginx 1.0.11 installed. Also, I'm using this init.d script, the only one I've found in several different places. It starts the server nicely, however, on stop/reset it says

* Stopping Nginx Server...      [fail]

Of course, the daemon is not stopped, and upon restart the configuration is not reloaded.

How can I repair this?

Surrey answered 13/1, 2012 at 21:18 Comment(0)
C
30

It's likely that it can't kill the process.

Open up the nginx sysvinit script located in /etc/init.d/ (or /etc/rc.d/) and find where nginx.pid is thought to be. It'll be something like "/var/run/nginx.pid".

If the pid file isn't there, open nginx.conf and look for the pid setting. If it is a mismatch - set the conf value to where the script thinks it should be, e.g.

# pid of nginx process
pid /var/run/nginx.pid;
Crutcher answered 26/2, 2012 at 19:5 Comment(2)
for me, it was /run/nginx.pid in nginx.conf that i replaced with /var/run/nginx.pid and it worked. thanks!Aylmar
Thanks - I was seeing the pid file in both dirs, but changing the init and nginx.conf to point to the 'var' dir instead solved the same problem on my stop/restart script.Terza
R
15

sudo service nginx restart will give you something like this:

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()

then kill the process manually by their port:

sudo fuser -k 80/tcp (or use whatever port you are using)

alternatively, kill the processes by their ID:

ps -ef |grep nginx
kill <pid>
Ratchford answered 6/10, 2014 at 11:45 Comment(0)
S
3

try this command:

sudo fuser -k 80/tcp
Syntax answered 9/7, 2015 at 21:56 Comment(0)
G
2

Cause of this issue

  • When you start nginx, it writes the pid to the file defined in nginx.conf
  • When you run service nginx stop, it stops the process whose PID is defined according to /usr/lib/systemd/system/nginx.service (your path may be different)
    • This means that the process is not actually stopped.
  • When you try to start it again, it tries to bind to the same port and throws an error.

Basically, you are writing the PID at location A and trying to read it from location B. This inconsistency seems to be the root cause of this apparently common issue.

Solution

Proper fix:

We need to ensure that both the read and write locations are same.

How?: The pid file defined in nginx.conf and nginx.service should be the same.

Hotfix:

  1. lsof -i | grep nginx

This will give you the pid. In my case, there were two of them.

  1. kill -9 pid1
  2. kill -9 pid2 (may not be required)
Ginger answered 17/6, 2019 at 9:26 Comment(0)
S
1

I had faced similar issues.

Generally I use apache/apache2.

The following might help you:

sudo nginx -s stop | ps -ef | grep nginx | awk {'print $2'} | xargs sudo kill -9 | sudo service apache2 start

For docs please refer this Github Gist

Stab answered 18/10, 2014 at 9:4 Comment(0)
C
0

Here's an nginx init script that I modified (based on the outdated offical init script) that works with many debian-based distros, including Ubuntu 11.04:

https://github.com/hulihanapplications/nginx-init-debian

This works pretty well on my ubuntu servers. Hope this helps.

Chrysler answered 17/1, 2012 at 21:51 Comment(0)
L
0

Here's an nginx init script that I modified (based on the official init script), I make the script's variable

lockfile=$pidfile 

and the variable

pidfile="/var/run/nginx.pid"

Then I modified the config file (nginx.conf), I make the variable

pid="/var/run/nginx.pid" 

too.

Finally I can use all the nginx commands normally.

Lichee answered 12/10, 2019 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.