【问题标题】:Django deploy Gunicorn and NginXDjango 部署 Gunicorn 和 NginX
【发布时间】:2023-07-09 08:38:01
【问题描述】:

在尝试最终部署我的新漫游车网络应用时,出现了问题。我已经配置了 Gunicorn 并将其绑定到 0.0.0.0:80(使用 wsgi)。网站加载,但没有任何图像、css、javascript 文件(甚至“管理”站点也没有任何样式)。 Postgres 已安装并运行良好。我已经做了

python manage.py collectstatic
python manage.py migrate

我看了很多教程如何做到这一点。但是在我尝试运行之后:

systemctl status nginx.service

我得到一个错误:

Job for nginx.service failed because the control process exited with error code. 
See "systemctl status nginx.service" and "journalctl -xe" for details.


#systemctl status nginx.service output:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since śro 2018-11-28 02:57:12 CET; 1min 13s ago
  Process: 14721 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
  Process: 25481 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 25477 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 10279 (code=exited, status=0/SUCCESS)

nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.




#journalctl -xe output:

sshd[5669]: Received disconnect from 181.15.216.20 port 42140:11: Bye Bye [preauth]
sshd[5669]: Disconnected from 181.15.216.20 port 42140 [preauth]
sudo[5773]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/systemctl restart nginx
sudo[5773]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)
systemd[1]: Stopped A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has finished shutting down.
systemd[1]: Starting A high performance web server and a reverse proxy server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has begun starting up.
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address aginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
-- 
-- The result is failed.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)lready in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit nginx.service has failed.
-- 
-- The result is failed.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)

/etc/systemd/system/gunicorn.socket 文件:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

/etc/systemd/system/gunicorn.service 文件:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=djangosu
Group=djangosu
WorkingDirectory=/home/djangosu/website/rover-Project
ExecStart=/home/djangosu/website/rover-env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          rover.wsgi:application

[Install]
WantedBy=multi-user.target

站点可用的“流动站”文件(与项目名称相同)(我已经将其链接到启用站点):

server {
    listen 80;
    server_name :333 ;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/.../website/rover/rover;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

gunicorn.sock 存在于 /run/ 目录中......有什么建议吗?

发行说明:

Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04
Codename:   xenial

【问题讨论】:

  • 嗯,它告诉你已经有一些东西绑定到端口 80。是吗?例如,您是否也在运行 Apache?
  • 没有。 Apache 处于非活动状态

标签: django gunicorn nginx-config


【解决方案1】:

nginx 告诉你,它不能绑定到端口 80,因为其他进程已经在监听这个端口。您可以在此处的日志输出中看到这一点:

nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

要检查哪个进程正在侦听此端口,您可以使用 netstat 或更好的 ss

~# ss -naptu 状态监听 | grep:80 tcp 0 128 0.0.0.0:80 0.0.0.0:* 用户:(("nginx",pid=6824,fd=4),("nginx",pid=6823,fd=4)) ~# netstat -tulpen | grep:80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 136280382 6823/nginx:主

最后一列会显示进程名和监听端口的进程的pid。

【讨论】:

  • 这并不让我感到惊讶:tcp 0 128 :80 *: users:(("gunicorn",pid=23983,fd=5),("gunicorn ",pid=15125,fd=5))。但这不是类似:来自“guest”的请求 -> nginx -> unicorn -> django 模块 -> 为“guest”生成的网站。我应该禁用 gunicorn ... 吗?这没有任何意义......
  • gunicorn 应该绑定到套接字,而不是端口。你要运行两次吗?
  • 是的,你基本上是对的。您有问题,gunicorn 需要使用与您的 nginx 不同的端口。我让我的 gunicorn 使用 bind 参数监听内部接口和特定端口。您可以在 gunicorn conf 中设置它,也可以使用命令行参数(如“gunicorn -b 127.0.0.1:8080”)进行设置。您需要将您的 nginx 配置更改为代理到该地址。如果您需要更多信息,我可以更新我的原始答案。
  • 我认为@DanielRoseman 的方法更好 - 您可以使用“ps -ef | grep gunicorn”检查哪些 gunicorn 进程正在运行。
  • 我的意思是问题中发布的代码显示 gunicorn 无论如何都绑定到套接字;它根本不应该使用端口。