【Nginx】Nginx启动显示80端口占用问题的解决方案

目录

🌅1. 问题描述

🌊2. 解决方案


🌅1. 问题描述

在启动nginx服务的时候显示内容如下:

sudo systemctl status nginx

问题出现原因:

根据日志显示,Nginx 服务启动失败,主要原因是无法绑定到端口 80。这通常是由于该端口已被

其他进程占用而导致的。


🌊2. 解决方案

要解决此问题,可以执行以下步骤:

确认端口 80 是否被其他进程占用。可以使用以下命令检查:

sudo netstat -tuln | grep :80

该命令会列出正在监听端口 80 的进程。如果有其他进程在使用该端口,显示如下:

打开配置文件:可以将80端口【默认端口】修改为 8080 端口【当然也可以是其他的,不过要记得去防火墙添加规则(即添加端口)】

比如我添加的是 8080 端口,则添加规则如下(红框内容):

可以使用以下命令打开配置文件:

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

我的配置文件内容如下【版本不同当然配置文件不同】:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

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

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

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

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# 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 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

将里面的代码模块

server {
    listen 80 default_server;
    listen [::]:80 default_server;

修改成

server {
    listen 8080 default_server;
    listen [::]:8080 default_server;

完成修改!【如果其他地方还有 80 的修改成 8080 即可】。

启动Nginx服务

sudo systemctl start nginx

设置Nginx服务自启动:

sudo systemctl enable nginx

验证Nginx是否运行:

sudo systemctl status nginx

如果一切正常,输出应该是“Active: active (running)”或者类似的信息。

### 解决 Windows 中 Nginx 80 端口占用的方法 当遇到 Nginx80 端口被其他应用程序占用的情况时,可以采取以下措施来解决问题。 #### 方法一:查找并关闭占用端口的应用程序 如果怀疑有其他服务占用80 端口,则可以通过命令提示符查询当前哪个进程正在使用该端口: ```cmd netstat -ano | findstr :80 ``` 上述命令会显示所有监听于 80 端口的任务及其 PID (Process ID),之后可以根据此 PID 来终止相应的进程: ```cmd taskkill /PID <pid_number> /F ``` 请注意,在执行 `taskkill` 命令前需确认目标进程确实是可以安全停止的服务[^1]。 #### 方法二:更改 IIS 或者其他 Web Server 配置 有时可能是由于安装了 Internet Information Services (IIS) 导致冲突。此时可以选择卸载不需要的组件或是调整其配置文件中的绑定设置以释放默认 HTTP(S) 所使用的 80 和 443 端口号[^2]。 对于非 IIS 应用,同样适用修改对应服务器软件的配置文档,使其不再监听这些敏感端口。 #### 方法三:编辑 Nginx 配置文件绕过 80 端口 另一种解决方案是在不改变现有环境的前提下让 Nginx 使用不同的端口工作。打开位于 C:\nginx\conf 下面的 nginx.conf 文件,并找到 server{} 指令内部有关 listen 参数的地方,将其更改为未被占用的一个高编号端口(例如 8080),如下所示: ```nginx server { ... listen 8080; server_name localhost; location / { root html; index index.html index.htm; } } ``` 保存更改后的配置文件后重启 Nginx 即可生效。
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SarPro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值