Unable to restart nginx
Mia Lopez
I need to restart the process. What do I do when I keep seeing these messages? Do you need more commands?
$ 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()
$ service nginx start
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2019-05-29 13:28:29 KST; 26s ago Process: 24508 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Process: 31955 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE) Process: 31952 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 13077 (code=exited, status=0/SUCCESS)
May 29 13:28:27 ip-172-26-12-170 nginx[31955]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 29 13:28:27 ip-172-26-12-170 nginx[31955]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 29 13:28:28 ip-172-26-12-170 nginx[31955]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 29 13:28:28 ip-172-26-12-170 nginx[31955]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 29 13:28:29 ip-172-26-12-170 nginx[31955]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
May 29 13:28:29 ip-172-26-12-170 nginx[31955]: nginx: [emerg] still could not bind()
May 29 13:28:29 ip-172-26-12-170 systemd[1]: nginx.service: Control process exited, code=exited status=1
May 29 13:28:29 ip-172-26-12-170 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
May 29 13:28:29 ip-172-26-12-170 systemd[1]: nginx.service: Unit entered failed state.
May 29 13:28:29 ip-172-26-12-170 systemd[1]: nginx.service: Failed with result 'exit-code'. 2 Answers
A application is currently listening on the on port 80. Most likely it is a other instance of nginx.
Find application that is listening on the socket
With the netstat command you can find the application which is currently listening on the socket. Also use grep to filter the output of netstat.
$ netstat -lpn | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 780/nginx: master p
tcp6 0 0 :::80 :::* LISTEN 780/nginx: master pThe last entry of the list 780/nginx tells you the PID of the process currently listening on the port 80.
Find and kill the application
With ps you can see the process structure of the process using the socket:
$ ps f -g780 PID TTY STAT TIME COMMAND 780 ? Ss 0:00 nginx: master process nginx 781 ? S 0:00 \_ nginx: worker process 782 ? S 0:00 \_ nginx: worker process 783 ? S 0:00 \_ nginx: worker process 784 ? S 0:00 \_ nginx: worker process 785 ? S 0:00 \_ nginx: worker process 786 ? S 0:00 \_ nginx: worker process 787 ? S 0:00 \_ nginx: worker process 788 ? S 0:00 \_ nginx: worker processWith the kill command you can now terminate the process which uses the socket:
$ kill 780If that does not terminate the process with -9:
$ kill -9 780Then try to restart your nginx service.
In the future, here's something that might come in useful. You can check the syntax of Nginx's configuration files by running:
nginx -t -c /etc/nginx/nginx.conf
The error output will give you a hint at what the problem is as well as the line number.
or:
debug with command:
$ service nginx configtest
Which outputs something like:
Testing nginx configuration: nginx: [emerg] unknown directive "stub_status" in /etc/nginx/sites-enabled/nginx_status:11 nginx: configuration file /etc/nginx/nginx.conf test failed
2