I am unable to do service nginx start
due to:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
But if I then do service nginx status
I get:
* nginx is not running
This is with nginx 1.8 on ubuntu 14.04, and using stop does not work (as it doesn't think it is running)
I know I can just kill the process running on that port, but I need a more graceful way to fix the problem.
I have confirmed that it is nginx listening on port 80 using netstat -tulpn
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9787/nginx.conf
When running sudo /usr/sbin/nginx -s reload
nginx: [error] open() "/run/nginx.pid" failed (2: No such file or directory)
I am using AWS OpsWorks (Chef) to manage the infrastructure.
To install nginx I am using the https://github.com/miketheman/nginx/tree/2.7.x cookbook, and installing via the repo. Run list = [nginx::repo, nginx::default]
The first time boot works absolutely fine, but a subsequent setup call causes a setup failure.
Results from ps aux | grep nginx
root 9791 0.0 0.0 31504 2184 ? Ss 10:12 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
www-data 14571 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14572 0.0 0.0 32036 2224 ? S 10:13 0:00 nginx: worker process
www-data 14573 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14574 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14575 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14576 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14577 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14578 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14579 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
www-data 14580 0.0 0.0 32036 1976 ? S 10:13 0:00 nginx: worker process
If I run echo '9791' | sudo tee /run/nginx.pid
nginx reports that is now running. Previously the pid file was empty.
So the root problem appears to be that the pid file is getting truncated.
Best Answer
Run
sudo fuser -k 80/tcp
This kills the nginx worker listening on port 80, allowing you to run nginx.