gunicorn 端口在重启后已被使用

问题描述 投票:0回答:0

我是堆栈溢出、Django、gunicorn 和很多东西的新手……对于任何可能违反规则的行为,我深表歉意。

我正在尝试让 gunicorn(在 Django 上运行)服务在重启服务器(ubuntu 18.04)后自动重启。

当我重新启动计算机时,gunicorn 已启用并正在运行,但问题是端口 8000 已被使用,如下所述:

Django 服务器错误:端口已被使用

我可以手动终止进程并重新启动 gunicorn(如上述链接的解决方案中所建议),但我想要一个解决方案来自动执行此过程。任何建议,将不胜感激。谢谢。

我想我需要更改一些配置文件,但不确定该怎么做。我没有开发 Django Web 服务器。有人为我创建,我只是在使用它,所以我不知道如何解决这个问题。

编辑:(感谢代码学徒的评论以澄清设置)

我正在使用 Django、Nginx 和 Gunicorn 运行一个网站。据我了解,启用了 nginx 和 gunicorn 服务。我的意思是,一旦 ubuntu 重新启动,它们就会启动。

当我重新启动 ubuntu 并输入

sudo systemctl status nginx

它返回以下消息:

● nginx.service - A high performance web server and a reverse proxy server    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)    Active: active (running) since Tue 2023-03-14 13:10:19 KST; 36s ago
     Docs: man:nginx(8)   Process: 2741 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)   Process: 2622 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)  Main PID: 2751 (nginx)
    Tasks: 73 (limit: 11059)    CGroup: /system.slice/nginx.service
           ├─2751 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─2754 nginx: worker process
           ├─2756 nginx: worker process
           ├─2758 nginx: worker process
           ...

监控gunicorn状态时返回:

● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2023-03-14 13:10:28 KST; 14min ago
  Process: 2605 ExecStart=/data/webmaster/anaconda3/bin/gunicorn --workers 3 --bind 0.0.0.0:8000 cmlab_site.wsgi:application (code=exited, status=1/FAILURE)
 Main PID: 2605 (code=exited, status=1/FAILURE)

 3월 14 13:10:24 ubuntu gunicorn[2605]: [2023-03-14 13:10:24 +0900] [2605] [ERROR] Retrying in 1 second.
 3월 14 13:10:25 ubuntu gunicorn[2605]: [2023-03-14 13:10:25 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
 3월 14 13:10:25 ubuntu gunicorn[2605]: [2023-03-14 13:10:25 +0900] [2605] [ERROR] Retrying in 1 second.
 3월 14 13:10:26 ubuntu gunicorn[2605]: [2023-03-14 13:10:26 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
 3월 14 13:10:26 ubuntu gunicorn[2605]: [2023-03-14 13:10:26 +0900] [2605] [ERROR] Retrying in 1 second.
 3월 14 13:10:27 ubuntu gunicorn[2605]: [2023-03-14 13:10:27 +0900] [2605] [ERROR] Connection in use: ('0.0.0.0', 8000)
 3월 14 13:10:27 ubuntu gunicorn[2605]: [2023-03-14 13:10:27 +0900] [2605] [ERROR] Retrying in 1 second.
 3월 14 13:10:28 ubuntu gunicorn[2605]: [2023-03-14 13:10:28 +0900] [2605] [ERROR] Can't connect to ('0.0.0.0', 8000)
 3월 14 13:10:28 ubuntu systemd[1]: gunicorn.service: Main process exited, code=exited, status=1/FAILURE
 3월 14 13:10:28 ubuntu systemd[1]: gunicorn.service: Failed with result 'exit-code'.

为了弄清楚什么进程正在使用端口 8000,我检查了

sudo lsof -i :8000

得到:

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 1914 root    8u  IPv4  69539      0t0  TCP url_of_my_website:46512->ubuntu:8000 (SYN_SENT)
node    2730 root   18u  IPv4  67787      0t0  TCP *:8000 (LISTEN)
django nginx gunicorn
© www.soinside.com 2019 - 2024. All rights reserved.