nginx service failed (Result: exit-code) Job for nginx.service failed - nginx

I've been trying to apply an SSL to my droplet, but the nginx gives me an error when I try to execute the certbot.
● 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 Fri 2018-10-19 13:50:49 UTC; 39s ago
Docs: man:nginx(8)
Process: 18860 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 18850 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Oct 19 13:50:47 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:47 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 19 13:50:48 All-line nginx[18860]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 19 13:50:49 All-line nginx[18860]: nginx: [emerg] still could not bind()
Oct 19 13:50:49 All-line systemd[1]: nginx.service: Control process exited, code=exited status=1
Oct 19 13:50:49 All-line systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 19 13:50:49 All-line systemd[1]: Failed to start A high performance web server and a reverse proxy server.

If You use certbot, this helped me. Updating certbot and updating of letsencrypt config files.

Related

nginx - Failed to restart process

I've got no errors in my config, as tested with nginx -t
root#proxy1:/var/www# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
However when I try running systemctl restart nginx, I get the following error.
root#proxy1:/var/www# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
Running systemctl status nginx.service returns
● 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 Sun 2022-10-09 19:43:09 CEST; 48s ago
Docs: man:nginx(8)
Process: 1439643 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 1439658 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Oct 09 19:43:08 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to 0.0.0.0:3582 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Oct 09 19:43:09 SERVERHOSTNAME.net nginx[1439658]: nginx: [emerg] still could not bind()
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: nginx.service: Failed with result 'exit-code'.
Oct 09 19:43:09 SERVERHOSTNAME.net systemd[1]: Failed to start A high performance web server and a reverse proxy server.
However all my sites running on nginx are functional.
I also get problems related to this while running certbot.
root#proxy1:/var/www# certbot
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
...
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for MYDOMAIN.net
nginx: [error] invalid PID number "" in "/run/nginx.pid"
Cleaning up challenges
nginx: [error] invalid PID number "" in "/run/nginx.pid"
nginx restart failed:
b''
b''
I'm not sure if it's a bug of Nginx, but you may try this (worked for me):
pkill -9 nginx
nginx -c /etc/nginx/nginx.conf
nginx -s reload
Source

Enabling Video Streaming with NGINX

I have just enabled nginx for Live Video Streaming. I followed the article (https://www.nginx.com/blog/video-streaming-for-remote-learning-with-nginx/). When I run
sudo nginx I am getting the warnings below
nginx: [warn] duplicate extension "ts", content type: "video/mp2t", previous content type: "video/mp2t" in /etc/nginx/nginx.conf:39
nginx: [warn] duplicate extension "html", content type: "text/html", previous content type: "text/html" in /etc/nginx/nginx.conf:40
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 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 [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 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 [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 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 [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 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 [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
nginx: [emerg] still could not bind() ```
Can someone please help me resolve this error. I have also included the config file for nginx. The article does not really give clear instructions for not so linux tech savvy people, it's missing some steps and doesn't really indicate where to put what.

How to run Nginx on a different port other than 80

I cannot run Nginx because port 80 is already listening on a docker-proxy service.
tcp6 0 0 :::80 :::* LISTEN 13110/docker-proxy
I would like to run Nginx on port 8800 instead of the default port 80.
As such, I've edited the default file as below;
sudo nano /etc/nginx/sites-available/default
listen 8800 default_server;
listen [::]:8800 default_server;
listen localhost;
However, I still cannot get it working as expected even after restart.
What am I doing wrong and how can I resolve it?
Below is the error I get;
● 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 Fri 2019-04-26 04:23:14 EDT; 13min ago
Docs: man:nginx(8)
Process: 16955 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 16944 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 80941 (code=exited, status=0/SUCCESS)
Apr 26 04:23:11 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 26 04:23:11 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:12 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:13 ubuntu nginx[16955]: nginx: [emerg] bind() to 127.0.0.1:80 failed (98: Address already in use)
Apr 26 04:23:14 ubuntu nginx[16955]: nginx: [emerg] still could not bind()
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Control process exited, code=exited status=1
Apr 26 04:23:14 ubuntu systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 26 04:23:14 ubuntu systemd[1]: Failed to start A high performance web server and a reverse proxy server.
You have to go to the /etc/nginx/sites-enabled/default
Edit that file and put (If you are willing to put "8800" as your port for nginx)
server { listen 8800; }
The start the server
sudo service nginx start
or
sudo service nginx restart if previously running.
Then access localhost:8800
Add a rule to iptables
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8800 -j ACCEPT
Restart IPtables
sudo service iptables restart;
It looks like the problem is in the line
listen localhost;
The "listen" directive can take a port number, a host,
or both. I believe in your case this line defaults
the port to 80, which would cause the problems you
are experiencing.
Also, beware if "default" is not the only file
in hosts_available. Each of these (each server block) will default
to using port 80 unless overridden there. Just
editing "default" would not be enough in that case.
I found this answer on Server Fault helpful: https://serverfault.com/questions/655067/is-it-possible-to-make-nginx-listen-to-different-ports
You can add ports to the one server block to keep things simple:
server {
listen 80;
listen 8000;
server_name example.org;
root /var/www/;
}

our nginx server is stopping on its own

We have an express app running on port 3000 using pm2 process manager. We are using nginx to reverse proxy the requests to the node app. But all of sudden nginx stops running on its own. It happened a few days ago, we did a restart and it worked then. But today it failed once again. At that time when we check nginx status it gives this output,
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 Sat 2018-09-22 12:38:52 UTC; 1h 43min ago
Process: 21571 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCES
Process: 21680 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 21676 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 8027 (code=exited, status=0/SUCCESS)
Sep 22 12:38:51 ip-172-31-27-16 nginx[21680]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Sep 22 12:38:51 ip-172-31-27-16 nginx[21680]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Sep 22 12:38:52 ip-172-31-27-16 nginx[21680]: nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
Sep 22 12:38:52 ip-172-31-27-16 nginx[21680]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Sep 22 12:38:52 ip-172-31-27-16 nginx[21680]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Sep 22 12:38:52 ip-172-31-27-16 nginx[21680]: nginx: [emerg] still could not bind()
Sep 22 12:38:52 ip-172-31-27-16 systemd[1]: nginx.service: Control process exited, code=exited status=1
Sep 22 12:38:52 ip-172-31-27-16 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Sep 22 12:38:52 ip-172-31-27-16 systemd[1]: nginx.service: Unit entered failed state.
Sep 22 12:38:52 ip-172-31-27-16 systemd[1]: nginx.service: Failed with result 'exit-code'.
And this is the nginx error.log
2018/09/22 12:38:41 [warn] 21539#21539: conflicting server name "example.com" on 0.0.0.0:80, ignored
2018/09/22 12:38:48 [warn] 21641#21641: conflicting server name "example.com" on 0.0.0.0:80, ignored
2018/09/22 12:38:48 [notice] 21641#21641: signal process started
2018/09/22 12:38:48 [error] 21641#21641: open() "/run/nginx.pid" failed (2: No such file or directory)
2018/09/22 12:38:48 [warn] 21642#21642: conflicting server name "example.com" on 0.0.0.0:80, ignored
2018/09/22 12:38:50 [warn] 21676#21676: conflicting server name "example.com" on 0.0.0.0:80, ignored
2018/09/22 12:38:50 [warn] 21680#21680: conflicting server name "example.com" on 0.0.0.0:80, ignored
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to [::]:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to [::]:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to [::]:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to [::]:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:443 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: bind() to [::]:80 failed (98: Address already in use)
2018/09/22 12:38:50 [emerg] 21680#21680: still could not bind()
2018/09/22 12:38:52 [alert] 21643#21643: unlink() "/run/nginx.pid" failed (2: No such file or directory)
Any idea why it is happening? Is something wrong with our nginx configuration or our node app is somehow causing the trouble?
Per logs it seems something is already using port 80 and 443. It could be anything. Also in some rare cases it could be nginx itself. Then i would reccomend to restart server.
i would start pin pointing what actually uses those port by:
# netstat -tulpn | grep :80
# netstat -tulpn | grep :443
Then would check nginx error logs to find out more
tail -n 500 /var/log/nginx/error.log
then if you find some other app using those ports you could check what those are:
# whatis <appName>
# whereis <appName>
If it is caused by different app then you can either turn it of or rebind it to another port.
Hope it helps :)

Change Nginx port from 80

Im trying to change the port that Nginx uses from port 80 to 5555
I've gone into nano /etc/nginx/sites-enabled/default
and edited:
server {
listen 5555;
}
But when I try to restart I get:
$ service nginx restart
Restarting nginx: 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()
Am I missing something?
Another service is the port.
Use sudo netstat -plnt to see which service is using the port and stop it, or configure it otherwise.
Also check out this and this.

Resources