首页 > 解决方案 > Gunicorn-Supervisor Django 设置问题

问题描述

我正在尝试设置一个 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

标签: djangobashgunicorndigital-ocean

解决方案


您可能在 gunicorn_start 脚本上有语法错误。我的建议是直接在主管 .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

推荐阅读