【问题标题】:Gunicorn-Supervisor Django Setup IssuesGunicorn-Supervisor Django 设置问题
【发布时间】:2019-03-19 17:53:47
【问题描述】:

我正在尝试设置一个 DigitalOcean Droplet 来保存一个 Django 应用程序,我正在关注这个概述:https://simpleisbetterthancomplex.com/tutorial/2016/10/14/how-to-deploy-to-digital-ocean.html

当我通过以下方式执行应用程序时,应用程序运行良好:python manage.py runserver 0.0.0.0:8000

但是,一旦通过sudo supervisorctl restart all 启动应用程序并运行sudo supervisorctl status,我就会明白这一点,但是当我到达正确的 URL 时应用程序无法运行:

exactestate@ExactEstateDroplet:~$  sudo supervisorctl status ExactEstate
ExactEstate                      RUNNING   pid 3071, uptime 0:00:19

有人可以帮忙吗?

这是我的目录结构:

exactestate@ExactEstateDroplet:~$ cd ../
exactestate@ExactEstateDroplet:/home$ cd ../
exactestate@ExactEstateDroplet:/$ ls
bin   dev  home        initrd.img.old  lib64       media  opt   root  sbin  srv  tmp  var      vmlinuz.old
boot  etc  initrd.img  lib             lost+found  mnt    proc  run   snap  sys  usr  vmlinuz
exactestate@ExactEstateDroplet:/$ ^C
exactestate@ExactEstateDroplet:/$ cd home
exactestate@ExactEstateDroplet:/home$ ls
exactestate
exactestate@ExactEstateDroplet:/home$ cd exactestate
exactestate@ExactEstateDroplet:~$ ls
ExactEstate  bin  include  lib  local  logs  run  share
exactestate@ExactEstateDroplet:~$ cd bin
exactestate@ExactEstateDroplet:~/bin$ ls
activate      activate.fish     easy_install      gunicorn_start  pip2    python         python2    wheel
activate.csh  activate_this.py  easy_install-2.7  pip             pip2.7  python-config  python2.7
exactestate@ExactEstateDroplet:~/bin$ cd ../
exactestate@ExactEstateDroplet:~$ cd ExactEstate
exactestate@ExactEstateDroplet:~/ExactEstate$ ls
ExactEstate  app.yaml   forms            interface_login        interface_management  interface_resident  interface_security  objects         requirements.txt  utils
README.md    cron.yaml  interface_admin  interface_maintenance  interface_onsite      interface_root      manage.py           objects_client  templates
exactestate@ExactEstateDroplet:~/ExactEstate$ ExactEstate is my Django Project Directory

这是我的 gunicorn_start 文件

#!/bin/bash

NAME="ExactEstate"
DIR=/home/exactestate/ExactEstate
USER=exactestate
GROUP=exactestate
WORKERS=3
BIND=unix:/home/exactestate/run/gunicorn.sock
DJANGO_SETTINGS_MODULE=ExactEstate.settings
DJANGO_WSGI_MODULE=ExactEstate.wsgi
LOG_LEVEL=error

cd $DIR
source ../bin/activate

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DIR:$PYTHONPATH


exec ../bin/gunicorn_start ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $WORKERS \
  --user=$USER \
  --group=$GROUP \
  --bind=$BIND \
  --log-level=$LOG_LEVEL \
  --log-file=-

这是我的主管配置

[program:ExactEstate]
command=/home/exactestate/bin/gunicorn_start
user=exactestate
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/exactestate/logs/gunicorn-error.log

Nginix 配置

upstream app_server {
    server unix:/home/exactestate/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;

    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    server_name 157.230.230.54;

    keepalive_timeout 5;
    client_max_body_size 4G;

    access_log /home/exactestate/logs/nginx-access.log;
    error_log /home/exactestate/logs/nginx-error.log;

    location /static/ {
        alias /home/exactestate/static/;
    }

    # checks for static file, if not found proxy to app
    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}

最新的 gunicorn-error.log

/home/exactestate/ExactEstate/../bin/gunicorn_start: line 16: 
/home/exactestate/ExactEstate/../bin/gunicorn_start: Argument list too long
/home/exactestate/ExactEstate/../bin/gunicorn_start: line 16: / 
/home/exactestate/ExactEstate/../bin/gunicorn_start: Success

【问题讨论】:

  • 好吧,正如您的目录结构所示,bin 目录中没有任何名为 gunicorn 的文件。你安装了吗?它在您的 requirements.txt 文件中吗?
  • 你也可以试试这个教程:digitalocean.com/community/tutorials/…
  • @DanielRoseman,是的,我已经安装了它。我修复了我最初遇到的问题,该行是我将其更改为 gunicorn_start 的问题,现在我可以启动服务器,但是当我运行命令启动它时我无法访问我的站点,请参阅更新的问题。
  • 你有什么记录在 /home/exactestate/logs/gunicorn-error.log 吗?你的 nginx 配置怎么样?
  • @Josir,gunicorn-error.log 现在发布在我的更新中,它在声明Argument list too longSuccess 之间切换

标签: django bash gunicorn digital-ocean


【解决方案1】:

我不知道你为什么有这么复杂的设置,这是我的帐户网站配置,就像一个魅力:

[group:accounting]
programs=accounting_web

[program:accounting_web]
command = /home/web/.virtualenvs/accounting/bin/gunicorn accounting.wsgi --workers=1 --threads=4 -b unix:/tmp/gunicorn_accounting.sock --log-level=DEBUG --timeout=120
directory = /home/web/accounting
user = web
environment=PATH="/home/web/.virtualenvs/accounting/bin"

【讨论】:

  • 您介意给我发消息并帮助我了解您设置我所要求的 DO Droplet 的过程吗?我是 DO 新手,真的需要帮助,我已经完成了几乎所有可以在网上找到的教程,无论我做什么,如果不手动使用 runserver,我就无法连接到我的应用程序,这自然不会为了生产而削减它。
  • 我的也在 DO 液滴上运行。我建议你只使用像我这样更简单的主管配置。
【解决方案2】:

您的 gunicorn_start 脚本可能存在语法错误。我的建议是直接在 supervisor .conf 上使用较小的命令行。

在 nginx.conf 上:

1) 尝试使用有效的 URL 更改 Servername 参数,例如:

exactestate.com

2) 将 sock 配置改为 TCP:

upstream gunicorn_panel {
    # For a TCP configuration:
    server 127.0.0.1:9000 fail_timeout=0; }

server {
   listen 8080;

在 supervisor.conf 上,不要使用 gunicorn_start 文件,而是尝试将所有命令直接写入命令变量:

[program:powerpanel]
command=/home/web/.virtualenvs/accounting/bin/gunicorn powerpanel.wsgi -b 127.0.0.1:9000 -w1 --pythonpath=/home/exactestate/ExactEstate --error-logfile=/home/exactestate/logs/gunicorn-error.log
user=webapp
autostart=true
autorestart=unexpected
startsecs=1
startretries=3
redirect_stderr=true

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2015-10-18
    • 2013-10-05
    • 2013-03-06
    • 2020-12-26
    • 1970-01-01
    • 2015-08-01
    • 2014-09-14
    • 2016-07-24
    相关资源
    最近更新 更多