Nginx: Job for nginx.service failed because the control process exited

daliborsb picture daliborsb · Mar 8, 2016 · Viewed 164.3k times · Source

I got a problem which I have been trying to fix for a few days now and I don't know what to do, have been looking for answers but all of those I found didn't help me.

I am kinda new here and I really hope that someone can help me. You can tell me which informations I need to give in hope for finding a solution.

$ systemctl status nginx.service
nginx.service - Startup script for nginx service
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Tue 2016-03-08 13:23:35 GMT; 2min 20s ago

Mar 08 13:23:33 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ------------ f...e)
Mar 08 13:23:33 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:34 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:34 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:35 startdedicated.com nginx[8315]: nginx: [emerg] bind() to ----- f...e)
Mar 08 13:23:35 .startdedicated.com nginx[8315]: nginx: [emerg] still could not bind()
Mar 08 13:23:35 startdedicated.com systemd[1]: nginx.service: control process exited, code=...=1
Mar 08 13:23:35 startdedicated.com systemd[1]: Failed to start Startup script for nginx service.
Mar 08 13:23:35 startdedicated.com systemd[1]: Unit nginx.service entered failed state.
Mar 08 13:23:35 startdedicated.com systemd[1]: nginx.service failed.

Answer

Abhishek R picture Abhishek R · Aug 4, 2018

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.