Troubleshooting Nginx: How to Force Stop When PID File is Missing and Port 80 is Occupied


2 views

When working with Nginx on CentOS 6.5, you might encounter a situation where the service fails to stop properly, leaving the process running without a PID file. The standard service command fails:

$ sudo service nginx stop
Stopping nginx: [FAILED]

First, verify that Nginx processes are actually running using:

$ ps -ef | grep 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

The absence of the PID file (/var/run/nginx.pid) while processes are still running indicates:

  1. The service management system can't properly stop Nginx without the PID file
  2. Subsequent restart attempts fail because port 80 remains occupied

When normal stop methods fail, you need to kill the processes manually:

$ sudo kill -9 19506 19507

Alternatively, you can use pkill to terminate all Nginx processes:

$ sudo pkill -9 nginx

After termination, verify port 80 is available:

$ sudo netstat -tulnp | grep :80

No output means the port is free. If something still occupies it, you might need to investigate other services.

To avoid this situation:

  1. Ensure proper shutdown scripts exist in /etc/init.d/nginx
  2. Check the PID file location in nginx.conf matches your system
  3. Consider adding a fallback kill mechanism in your init script

After resolving the issue, perform a clean restart:

$ sudo service nginx start
$ sudo service nginx status

You should now see Nginx running properly with a new PID file created.


When attempting to stop Nginx via sudo service nginx stop, you might encounter a failure because the system can't locate the PID file (/var/run/nginx.pid) which normally contains the process ID of the Nginx master process. This becomes particularly problematic when you need to restart Nginx, as you'll see port binding errors indicating the port is still occupied.

First, verify if Nginx is actually running:

ps -ef | grep nginx

Typical output shows both master and worker processes:

root     19506     1  0  2013 ?        00:00:00 nginx: master process /usr/sbin/nginx
ironsand 19507 19506  0  2013 ?        00:00:25 nginx: worker process

When the service command fails, you can manually kill the processes:

sudo kill -9 19506  # Using the master process PID found earlier
sudo pkill -9 nginx # Alternative: kill all nginx processes

After stopping Nginx, you might still encounter port 80 conflicts. Verify port usage:

sudo netstat -tulnp | grep :80

If Nginx is truly stopped but the port appears occupied, check for other web servers:

sudo lsof -i :80

When dealing with stubborn cases, use this sequence:

sudo killall -9 nginx
sudo service nginx start

Ensure your nginx.conf has these critical settings:

pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;

Verify permissions on the PID directory:

sudo chown -R root:nginx /var/run/
sudo chmod 775 /var/run/

When seeing bind() errors, check both the error log and current network status:

tail -n 50 /var/log/nginx/error.log
sudo ss -tulnp | grep :80

For newer systems using systemd:

sudo systemctl stop nginx
sudo systemctl reset-failed nginx
sudo systemctl start nginx

After all operations, verify Nginx status:

sudo service nginx status
# Or for systemd:
sudo systemctl status nginx