You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hostinger VPS部署Django项目遇端口占用问题求助

Hostinger VPS部署Django站点故障排查与解决

问题详情

  • 部署Django项目后,启动Nginx无法通过域名访问站点
  • Gunicorn日志报错8000端口被占用:
gunicorn[313208]: [2024-08-14 05:56:29 +0000] [313208] [ERROR] Connection in use: ('0.0.0.0', 8000)
  • 执行sudo lsof -i :80发现Nginx进程占用80端口:
COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   316452     root    6u  IPv4 1073432      0t0  TCP *:http (LISTEN)
nginx   316452     root    7u  IPv6 1073433      0t0  TCP *:http (LISTEN)
nginx   316453 www-data    6u  IPv4 1073432      0t0  TCP *:http (LISTEN)
nginx   316453 www-data    7u  IPv6 1073433      0t0  TCP *:http (LISTEN)
  • 用sudo kill -9 316452 316453强制终止Nginx进程后,进程会自动重启

已尝试操作

  • 查看Gunicorn错误日志
  • 执行sudo lsof -i :80排查端口占用
  • 执行sudo kill -9强制终止Nginx进程

解决步骤

1. 修复Gunicorn的8000端口占用

Gunicorn报错的是8000端口被占用,和Nginx占用的80端口是两个独立问题,优先解决此问题:

  • 定位占用8000端口的进程:
    sudo lsof -i :8000
    
  • 终止该进程:
    sudo kill -9 <找到的进程PID>
    
  • 重启Gunicorn:
    sudo systemctl restart gunicorn  # 若用systemd管理Gunicorn
    # 或直接启动:gunicorn --bind 0.0.0.0:8000 你的项目名.wsgi:application
    

2. 彻底停止Nginx自动重启

直接kill进程后Nginx重启是因为它被系统服务管理器托管,需通过服务命令操作:

  • 停止Nginx服务:
    sudo systemctl stop nginx
    
  • (可选)禁用开机自启:
    sudo systemctl disable nginx
    
  • 确认停止状态:
    sudo systemctl status nginx
    

3. 配置Nginx反向代理到Gunicorn

确保Nginx配置正确转发请求到Gunicorn的8000端口,编辑站点配置文件(通常在/etc/nginx/sites-available/目录下):

server {
    listen 80;
    server_name 你的域名;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  • 测试配置语法:
    sudo nginx -t
    
  • 重启Nginx服务:
    sudo systemctl restart nginx
    

内容的提问来源于stack exchange,提问作者Kalayarasantitus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:07:10