SoFunction
Updated on 2025-04-08

Nginx startup failed: Solution to the problem of port 80 occupied

introduction

When deploying Nginx on a Linux server, you may encounter Nginx startup failure, especially the error message bind() to 0.0.0.0:80 failed (98: Address already in use). This problem is usually caused by port 80 being occupied by other processes. This article will analyze the causes of this problem in detail and provide a variety of solutions to help you quickly restore normal operation of Nginx.

Problem description

When we try to start Nginx, we may see the following error message:

[root@20250220-instance ~]# systemctl status 
●  - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2025-02-24 00:02:25 CST; 9s ago
  Process: 10211 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 10207 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 10205 ExecStartPre=/usr/bin/rm -f /run/ (code=exited, status=0/SUCCESS)

Feb 24 00:02:23 20250220-instance nginx[10211]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Feb 24 00:02:24 20250220-instance nginx[10211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Feb 24 00:02:24 20250220-instance nginx[10211]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Feb 24 00:02:24 20250220-instance nginx[10211]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Feb 24 00:02:24 20250220-instance nginx[10211]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Feb 24 00:02:25 20250220-instance nginx[10211]: nginx: [emerg] still could not bind()
Feb 24 00:02:25 20250220-instance systemd[1]: : control process exited, code=exited status=1
Feb 24 00:02:25 20250220-instance systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Feb 24 00:02:25 20250220-instance systemd[1]: Unit  entered failed state.
Feb 24 00:02:25 20250220-instance systemd[1]:  failed.

From the error message, we can see that the reason why Nginx startup fails is the port.80Has been occupied by other processes.

Problem analysis

Port80It is the default port for HTTP service. When Nginx tries to bind to the port80, if the port has been occupied by other processes, Nginx will not be able to start. We need to find the occupied port80and take corresponding measures.

Solution

1. Check the process that occupies port 80

First, we need to confirm which process occupies the port.80. The following commands can be used:

Use the netstat command

sudo netstat -tuln | grep :80

Output example:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     

Use the ss command

ssCommand comparisonnetstatMore powerful, can display more information:

sudo ss -tulnp | grep :80

Output example:

LISTEN  0  128  0.0.0.0:80  0.0.0.0:*  users:(("nginx",pid=1234,fd=6))

Use the lsof command

ifssThe command does not display process information, you can try to uselsofOrder:

sudo lsof -i :80

Output example:

COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME
nginx    1234 root  6u  IPv4 123456  0t0     TCP *:http (LISTEN)

Through these commands, we can find the occupied port80The process ID (PID) and process name.

2. Terminate the process that occupies port 80

Find the occupied port80After the process, you can usekillCommand terminates it. For example, if the process ID is1234, run the following command:

sudo kill -9 1234

If the process is Nginx itself, it may be because there are multiple Nginx instances running. You can try to stop all Nginx processes:

sudo systemctl stop nginx
sudo pkill nginx

3. Restart Nginx

Terminate the occupied port80After the process, restart Nginx:

sudo systemctl start nginx

Then check the status of Nginx:

sudo systemctl status nginx

4. Check the Nginx configuration file

If Nginx still fails to start, it may be a problem with the configuration file. You can check the syntax of an Nginx configuration file using the following command:

sudo nginx -t

If there is an error, fix the configuration file as prompted and restart Nginx.

5. Check whether other services occupy port 80

If the port80If occupied by other services (such as Apache, httpd, or other web servers), you can stop these services:

sudo systemctl stop apache2  # If it is Apachesudo systemctl stop httpd    # If it is httpd

6. Check the firewall settings

Ensure that the firewall allows ports80The traffic through:

sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload

7. Check SELinux settings

If SELinux is enabled, Nginx binding ports may be blocked. SELinux can be temporarily disabled to test whether SELinux is causing the problem:

sudo setenforce 0

If the problem is resolved, you can adjust the SELinux policy to allow Nginx to bind ports.

Summarize

Through the above steps, we can effectively solve the problem of Nginx startup failure. The key is to find and terminate the process that occupies port 80, while ensuring that Nginx's configuration files and system settings are not problematic. If you still have problems, you can check the error log for Nginx for more information:

sudo tail -n 50 /var/log/nginx/

I hope this article can help you solve the problem of Nginx startup failure.

This is the article about Nginx startup failure: solution to the problem of port 80 occupied. For more related content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!