Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

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>

try this command:

sudo fuser -k 80/tcp

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

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)

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top