Hubert OG
Hubert OG

Reputation: 19544

Nginx daemon stop is failing

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?

Upvotes: 15

Views: 26788

Answers (7)

user12205063
user12205063

Reputation: 1

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.

Upvotes: 0

pragmar
pragmar

Reputation: 1034

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;

Upvotes: 30

Ojasvi Monga
Ojasvi Monga

Reputation: 5129

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)

Upvotes: 2

WiredIn
WiredIn

Reputation: 4227

try this command:

sudo fuser -k 80/tcp

Upvotes: 3

suryakrupa
suryakrupa

Reputation: 4112

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

Upvotes: 1

Roman
Roman

Reputation: 9441

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>

Upvotes: 15

dhulihan
dhulihan

Reputation: 11293

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.

Upvotes: 0

Related Questions