178

nginx.service failed because the control process exited

$ 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.
Nimantha
  • 6,405
  • 6
  • 28
  • 69
daliborsb
  • 1,781
  • 2
  • 10
  • 4
  • 3
    Please, post your nginx.conf and: "nginx -t" output... – user2925795 Mar 08 '16 at 13:42
  • http://prntscr.com/acoayr Here is it. I can not post the code so I had to take a screenshot. – daliborsb Mar 08 '16 at 13:50
  • Please, post your server block config and: "nginx -t" output... Have you tried to restart nginx? "service nginx restart" – user2925795 Mar 08 '16 at 13:54
  • nginx -t output nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful I tried restarting and this is what I get: falcon170:~# service nginx restart Restarting nginx (via systemctl): Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. [FAILED] – daliborsb Mar 08 '16 at 13:58
  • look at /var/log/nginx/errors.log. There is all information about fail. – Adizbek Ergashev Mar 23 '17 at 17:08

32 Answers32

321

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Abhishek R
  • 4,087
  • 1
  • 17
  • 21
  • 3
    tnx i just try 'sudo fuser -k 80/tcp' and 'sudo fuser -k 443/tcp' and nginx work for me – Mohmmad Ebrahimi Aval Nov 21 '18 at 17:35
  • In my case, I was formally using node/expressjs to handle my server. Http was set to run on port 80, while https set to run port 443. I removed them as should be the case and nginx worked as expected. I guess the error means another service is using port 8080. – xplorer1 Jun 18 '19 at 22:57
  • I get the weirdest problem. When I do `sudo netstat -tulpn` I actually see `tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24873/nginx: master `, but when I try to start the service or see it's status, I get this `nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)` Bizarre. Any idea why? – Newskooler Jul 18 '19 at 20:17
  • when I run ```sudo nano /var/log/nginx/error.log```, ```2022/02/15 02:15:45 [emerg] 29102#29102: bind() to 0.0.0.0:80 failed (13: Permission denied) 2022/02/15 02:45:48 [emerg] 29792#29792: bind() to 0.0.0.0:80 failed (13: Permission denied) 2022/02/15 05:08:03 [emerg] 1050#1050: bind() to 0.0.0.0:80 failed (13: Permission denied) 2022/02/15 05:08:55 [emerg] 1085#1085: bind() to 0.0.0.0:80 failed (13: Permission denied)``` what do I do? – Gargee Suresh Feb 15 '22 at 13:12
  • you are a gem buddy – Arsh Arora Feb 23 '23 at 10:26
126

In my case, it's because of apache server is running somehow. So I stop apache then restart nginx. Work like a charm!

sudo /etc/init.d/apache2 stop
sudo systemctl restart nginx
Tánh
  • 1,261
  • 1
  • 8
  • 3
75

May come in handy to check syntax of Nginx's configuration files by running:

nginx -t -c /etc/nginx/nginx.conf
Dinesh Sunny
  • 4,663
  • 3
  • 30
  • 29
  • In my case it works with `sudo /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf` or simply `sudo /usr/local/nginx/sbin/nginx -t` – Pathros Jan 15 '18 at 00:13
  • 1
    The above command displays the errors in your config files added to the `nginx/sites-enabled` and once those errors are resolved all other commands run peacefully. – Sachin Kumar Jul 13 '20 at 06:36
  • Nice! It also prints validation errors when executing the command to start or restart or check status of nginx – Tadele Ayelegn Jul 10 '22 at 02:14
  • Very helpful. I could debug the error in syntax of my .conf file. Very helpful. – Ashish Deora Oct 09 '22 at 16:48
30

The cause of the issue is this, I already had Apache web server installed and actively listening on port 80 on my local machine.

Apache and Nginx are the two major open-source high-performance web servers capable of handling diverse workloads to satisfy the needs of modern web demands. However, Apache serves primarily as a HTTP server whereas Nginx is a high-performance asynchronous web server and reverse proxy server.

The inability of Nginx to start was because Apache was already listening on port 80 as its default port, which is also the default port for Nginx.

One quick workaround would be to stop Apache server by running the command below

systemctl stop apache2
systemctl status apache2

And then starting up Nginx server by running the command below

systemctl stop nginx
systemctl status nginx

However, this same issue will arise again when we try to start Apache server again, since they both use port 80 as their default port.

Here's how I fixed it:

Run the command below to open the default configuration file of Nginx in Nano editor

sudo nano /etc/nginx/sites-available/default

When the file opens in Nano editor, scroll down and change the default server port to any port of your choice. For me, I chose to change it to port 85

# Default server configuration
#
server {
       listen 85 default_server;
       listen [::]:85 default_server;

Also, scroll down and change the virtual host port to any port of your choice. For me, I also chose to change it to port 85

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
# server {
#        listen 85;
#        listen [::]:85;

Then save and exit the file by pressing on your keyboard:

Ctrl + S
Ctrl + X

You may still be prompted to press Y on your keyboard to save your changes.

Finally, confirm that your configuration is correct and restart the Nginx server:

sudo nginx -t
sudo systemctl restart nginx

You can now navigate to localhost:nginx-port (localhost:85) on your browser to confirm the changes.

Displaying the default Nginx start page

If you want the default Nginx start page to show when you navigate to localhost:nginx-port (localhost:85) on your browser, then follow these steps:

Examine the directory /var/www/html/ which is the default root directory for both Apache and Nginx by listing its contents:

cd ~
ls /var/www/html/

You will 2 files listed in the directory:

index.html                # Apache default start page
index.nginx-debian.html   # Nginx default start page

Run the command below to open the default configuration file of Nginx in Nano editor:

cd ~
sudo nano /etc/nginx/sites-available/default

Change the order of the index files in the root directory from this:

root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

to this (putting the default Nginx start page - index.nginx-debian.html in the 2nd position immediately after index):

root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.nginx-debian.html index.html index.htm;

Then save and exit the file by pressing on your keyboard:

Ctrl + S
Ctrl + X

You may still be prompted to press Y on your keyboard to save your changes.

Finally, confirm that your configuration is correct and restart the Nginx server:

sudo nginx -t
sudo systemctl restart nginx

You can now navigate to localhost:nginx-port (localhost:85) on your browser to confirm the changes.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • 2
    I don't know but for some reason apache was installed. I didn't install it. It probably came with the VPS I was working on so I ignored this answer and followed all the others, at the end, tried this, and thank you so much. Now, I can sleep. – Mujeeb Ishaque Jul 15 '20 at 20:42
  • change order of index: `index index.nginx-debian.html index.html index.htm;` in file `/etc/nginx/sites-available/default` saved me, thanks. – fudu Aug 03 '23 at 03:06
18

Try to 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

And fix those warnings

Then restart nginx

itsnikolay
  • 17,415
  • 4
  • 65
  • 64
18

Jan, 2022 Update:

The easiest way is kill all nginx processes:

sudo killall nginx

Then:

sudo nginx

Or:

sudo service nginx start
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
10
  • use these command to

    1. sudo service apache2 stop

    2. sudo apt-get purge apache2

    3. sudo apt-get update

    4. sudo apt-get install nginx

    5. sudo service nginx restart

Deepak Kr
  • 381
  • 3
  • 5
10

When something cannot bind to a port, it's 5% because it's not started by root (sticky suid bit, sudo) and 94% because another application is already bound to that port.

Make sure nginx is really shutdown and you don't try to start it twice by accident.

Make sure you don't have Apache or other services running that use port 80.

Utilize netstat -a | grep tcp to find out more.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
8

Change the port may help as 80 port is already using somewhere

vi /etc/nginx/sites-available/default

Change the port:

listen 8080 default_server;
listen [::]:8080 default_server;

And then restart the nginx server

nginx -t
service nginx restart
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
dushyant
  • 349
  • 3
  • 7
6
$ ps ax | grep nginx<br>
$ kill -9 PIDs
$ service nginx start



or set back /etc/nginx/sites-available/default

location / {
     # First attempt to serve request as file, then
     # as directory, then fall back to displaying a 404.

     try_files $uri $uri/ =404;
}

Mohammad Kholghi
  • 533
  • 2
  • 7
  • 21
spitfir33
  • 61
  • 1
  • 2
6

For my case, I need to run

sudo nginx -t

It will check if Nginx configuration is correct or not, if not, it will show you which configuration causes the error.

Then you need to go to /etc/nginx/sites-available to fix the broken configuration.

After that, you can restart Nginx without any problem.

sudo systemctl restart nginx
0xh8h
  • 3,271
  • 4
  • 34
  • 55
6

Well, this may be a bit late but here goes... I observed new instances on AWS have Apache2 already installed and it uses port 80 as with Nginx. I ran into this issue. This is how I solved it.

I removed the apache service first, then restarted the nginx service.

 sudo apt install nginx -y
 sudo ufw allow 'Nginx HTTP'
 sudo ufw allow OpenSSH
 sudo ufw enable
 sudo systemctl start nginx
 sudo service apache2 stop
 sudo apt-get purge apache2 apache2-utils apache2-bin apache2.2-common -y
 sudo apt-get autoremove
 sudo rm -rf /etc/apache2
 sudo systemctl restart nginx
Akhile Ehis
  • 103
  • 1
  • 5
  • It worked for me. One hint towards this, when I was going to my custom domain and the apache2 default page was loading. Should have realized. Thanks – pizzaisdavid Sep 06 '22 at 15:22
4

Try set a user in nginx.conf, maybe that's why he can not start the service:

User www-data;
user2925795
  • 400
  • 10
  • 29
4

In my case, nginx was not able to open the log file which is located here /var/log/nginx/error.log

This was because I had deleted the log directory to free up space in root (which turned out to be stupid)

I then created a log folder in var and an nginx folder in log.

Then simply run sudo service nginx start

4

Some other process is already running and bound to ports 80/443, thus systemd cannot start nginx. This is almost always because the process was started manually instead of via systemd, or because you tried to start two different web servers at the same time.

To resolve the problem, kill the process yourself before restarting it via systemd.

For me apache web server was already running, so killing it did the trick !

sudo killall apache2
Harshil
  • 1,230
  • 17
  • 22
4

Turns out I had a file under sites-enabled that I forgot to unlink, yet its corresponding file in sites-available was no longer existing. If you have such a file, deleted it and run;

sudo service nginx restart

To unlink a file use;

sudo unlink /etc/nginx/sites-enabled/file-name
Nelson Sammy
  • 404
  • 2
  • 7
  • 16
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/31599070) – Amit Gajera Apr 28 '22 at 12:15
3

I had the same problem when I used Vesta, which uses nginx with apache. The problem was that after applying all updates Apache started listening to 443 for https. The solution was just to comment out the 443 stuff in ports.conf. This is because nginx also uses this port.

Aleks
  • 93
  • 4
3

This worked for me

sudo fuser -k 80/tcp

sudo fuser -k 443/tcp

then

sudo  -H apt-get  purge  nginx-common  nginx-full

sudo  -H apt-get  install  nginx-common  nginx-full


sudo systemctl restart nginx
3

It is because your apache server is running

Try to stop your apache server with this command

sudo /etc/init.d/apache2 stop

Then restart nginx server

sudo systemctl restart nginx
Mochi
  • 123
  • 8
3

I faced the same problem and first I ran sudo nginx -t

then I notice there are some issues in my site configuration file and then I navigate to /etc/nginx/sites-available and fix the broken configuration.

After that, you can restart Nginx without any problem.

sudo systemctl restart nginx

2

Check df -h if you are under centOS system

Lulu
  • 173
  • 1
  • 8
2

This worked for me:

First, go to

cd /etc/nginx

and make the changes in nginx.conf and make the default port to listen from 80 to any of your choice 85 or something.

Then use this command to bind that port type for nginx to use it:

semanage port -a -t PORT_TYPE -p tcp 85

where PORT_TYPE is one of the following: http_cache_port_t, http_port_t, jboss_management_port_t, jboss_messaging_port_t, ntop_port_t, puppet_port_t.

Then run:

sudo systemctl start nginx; #sudo systemctl status nginx

[you should see active status]; #sudo systemctl enable nginx

Community
  • 1
  • 1
tushar1092
  • 21
  • 3
  • This worked for me. I thought I had disabled SELinux but apparently not, so I just disabled it with `setenforce 0`. (I hate SELinux, BTW). – Aquarelle Jun 11 '22 at 22:57
2

First stop your apache2. sudo /etc/init.d/apache2 stop Second restart your Nginx. sudo systemctl restart nginx

Imran
  • 57
  • 2
1

Had this issue when provisioning a new site for VVV in vvv-config.yml with a faulty syntax, vagrant up would throw the error. Deleting and reverting to old configuration, running vagrant provision helped

user3615851
  • 999
  • 1
  • 15
  • 40
1

In my case, it was due to the invalid syntax in my /etc/nginx/nginx.conf file.

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

So I ran the following command for details.
> systemctl status nginx.service
invalid URL prefix in /etc/nginx/nginx.conf:48
which said that nginx.conf had an invalid syntax.

I fixed it referring to this.

SKO
  • 32
  • 5
1

The right answer on CentOS is setenforce 0

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 14:23
1

autorestart on failures for nginx on both the prod servers, staging server and dev server. Server name as:

dev staging prod

I modified "/lib/systemd/system/nginx.service" and added the following line to [Service] section at the end -> Restart=on-failure

after changing the configuration we did, we have to execute the command ' systemctl daemon-reload ' for the auto-restart configuration to take effect.

This should restart the Nginx when it crashes

CHAVDA MEET
  • 777
  • 8
  • 14
0

reinstalling nginx has worked for me

uninstall nginx:

sudo apt-get remove nginx nginx-common
sudo apt-get autoremove

install nginx:

sudo apt update
sudo apt install nginx
ARGVN
  • 41
  • 5
0

In my case, apache2 was the problem. It was running.

First, let's take a look at apache2 status:

sudo service apache2 status

If it's running, then stop it:

sudo systemctl stop apache2.service

Then restart nginx:

sudo systemctl restart nginx.service

That's all I have to do to make nginx up and running.

protanvir993
  • 2,759
  • 1
  • 20
  • 17
0

I mistakenly added a line at the top of this file. /etc/nginx/sites-enabled/default Once I modified it started working.

-1

I'm using RHEL 7.4 with NGINX 1.13.8 and if I do the same with sudo, it works Ok:

sudo systemctl status nginx.service

Just make sure whoever wants to use nginx.service has execute permissions to it.

Ostati
  • 4,623
  • 3
  • 44
  • 48
-1

In my case, I missed a semicolon in the default file that's the reason this error appars.

MD SHAYON
  • 7,001
  • 45
  • 38