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

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

🌅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)”或者类似的信息。


目录
相关文章
|
8月前
|
应用服务中间件 nginx
Nginx 出现403 Forbidden 的几种解决方案【已解决】
Nginx 出现403 Forbidden 的几种解决方案【已解决】
8096 3
|
4月前
|
缓存 负载均衡 应用服务中间件
Nginx 实现一个端口代理多个前后端服务
【10月更文挑战第19天】Nginx 的强大功能不仅限于此,它还可以与其他技术和工具相结合,为我们的应用提供更强大的支持和保障。在不断发展的互联网时代,掌握 Nginx 的使用技巧将为我们的工作和生活带来更多的便利和效益。
|
7月前
|
前端开发 应用服务中间件 nginx
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
网页设计,若依项目修改(It must be done)01----若依打包位置,nginx代理前端静态资源和后端接口,就是怎样设置转载,访问固定端口,让他访问其他资料的配置文件,访问/,给你那些
|
9月前
端口被占用 --- 解决方案
端口被占用 --- 解决方案
126 0
|
9月前
|
前端开发 应用服务中间件 nginx
nginx中配置不输入端口(指定地址)访问项目的方法
nginx中配置不输入端口(指定地址)访问项目的方法
|
9月前
|
应用服务中间件 nginx Docker
docker安装nginx并暴露端口
【1月更文挑战第9天】docker安装nginx并暴露端口
430 0
|
9月前
|
安全 JavaScript 应用服务中间件
Nginx 跨域解决方案
跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器,它允许 Web 应用服务器进行跨域访问控制,从而使跨域数据传输得以安全进行。 跨域资源共享标准新增了一组 HTTP 首部字段,允许服务器声明哪些源站通过浏览器有权限访问哪些资源。另外,规范要求,对那些可能对服务器数据产生副作用的 HTTP 请求方法(特别是GET以外的 HTTP 请求
|
Java 应用服务中间件 Linux
linux安装jdk1.8 +nginx +springboot 实现通过nginx80转发8888端口访问springboot程序
linux安装jdk1.8 +nginx +springboot 实现通过nginx80转发8888端口访问springboot程序
201 0
|
存储 应用服务中间件 nginx
Nginx-504 Gateway Time-out排查及解决方案
  前段时间碰到了一个调用服务失去响应的问题,服务是采用Java开发,部署在Tomcat上,前端使用Nginx做代理,但是通过Nginx调用服务的时候一直报504 Gateway Time-out错误,跨过Nginx直接调用服务又正常,所有锁定错误和Nginx有关,后经排查发现是Nginx的日志过大,将存储盘空间占满了,导致自身日志不能正常写入。
3259 0