2
votes

In console it showing like this:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

I tried following commands:

sudo nginx -t -c /etc/nginx/nginx.conf

and

sudo nginx -t

It is showing success message but nginx server is not restarting.

sudo 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 2018-07-18 18:06:51 IST; 11min ago
Process: 28271 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 28259 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 5236 (code=exited, status=0/SUCCESS)

nginx error log have this errors

bind() to 0.0.0.0:443 failed (98: Address already in use) 2018/07/18 18:06:49 [emerg] 28271#28271: bind() to [::]:443 failed (98: Address already in use) 2018/07/18 18:06:49 [emerg] 28271#28271: bind() to 0.0.0.0:80 failed (98: Address already in use)

2018/07/18 18:06:49 [emerg] 28271#28271: still could not bind()

2
Did you try systemctl status nginx.service and journalctl -xe?Thomas Sablik
Do you have anything in your error.log?Shawn C.
Is nginx running? Did you stop it?Thomas Sablik
You already have a program listening on the ports.Thomas Sablik
you can check this link chrisjean.com/…uday214125

2 Answers

3
votes

You already have a program listening on the ports. Stop the other program or choose other ports for nginx.

If it's another nginx instance, you can search for it with ps aux | grep nginx and then kill it with kill PID

1
votes

Try to run the following two commands:

sudo fuser -k 80/tcp

sudo fuser -k 443/tcp

Then execute

sudo service nginx restart

If that worked, your hosting provider might be installing Apache on your server by default during a fresh install, so keep reading for a more permenant fix. If that didn't work, keep reading to identify the issue.

Run nginx -t and if it doesn't return anything, I would verify Nginx error log. By default, it should be located in /var/log/nginx/error.log.

You can open it with any text editor: sudo nano /var/log/nginx/error.log

Can you find something suspicious there?

The second log you can check is the following

sudo nano /var/log/syslog

When I had this issue, it was because my hosting provider was automatically installing Apache during a clean install. It was blocking port 80.

When I executed sudo nano /var/log/nginx/error.log I got the following as the error log:

2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to [::]:80 failed (98: Address already in use)
2018/08/04 06:17:33 [emerg] 634#0: bind() to 0.0.0.0:80 failed (98: Address already in use)

What the above error is telling is that it was not able to bind nginx to port 80 because it was already in use.

To fix this, you need to run the following:

yum install net-tools

sudo netstat -tulpn

When you execute the above you will get something like the following:

Proto Recv-Q Send-Q Local Address           Foreign Address         State    PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1762/httpd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1224/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1528/sendmail:acce
tcp6       0      0 :::22                   :::*                    LISTEN      1224/sshd

You can see that port 80 is blocked by httpd (Apache). This could also be port 443 if you are using SSL.

Get the PID of the process that uses port 80 or 443. And send the kill command changing the <PID> value:

sudo kill -2 <PID>

Note in my example the PID value of Apache was 1762 so I would execute sudo kill -2 1762

Aternatively you can execute the following:

sudo fuser -k 80/tcp

sudo fuser -k 443/tcp

Now that port 80 or 443 is clear, you can start Nginx by running the following:

sudo service nginx restart

It is also advisable to remove whatever was previously blocking port 80 & 443. This will avoid any conflict in the future. Since Apache (httpd) was blocking my ports I removed it by running the following:

yum remove httpd httpd-devel httpd-manual httpd-tools mod_auth_kerb mod_auth_mysql mod_auth_pgsql mod_authz_ldap mod_dav_svn mod_dnssd mod_nss mod_perl mod_revocator mod_ssl mod_wsgi

Hope this helps.