nginx + gunicorn + django无法正常工作

时间:2016-05-20 06:25:58

标签: python django nginx gunicorn

我正在接受以下文档http://tutos.readthedocs.io/en/latest/source/ndg.html的帮助并尝试运行服务器。我在启用网站的文件中有以下文件:

upstream test_server {
  server unix:/var/www/test/run/gunicorn.sock fail_timeout=10s;
}

# This is not neccessary - it's just commonly used
# it just redirects example.com -> www.example.com
# so it isn't treated as two separate websites
server {
        listen 80;
        server_name dehatiengineers.in;
        return 301 $scheme://www.dehatiengineers.in$request_uri;
}

server {
    listen   80;
    server_name www.dehatiengineer.in;

    client_max_body_size 4G;

    access_log /var/www/test/logs/nginx-access.log;
    error_log /var/www/test/logs/nginx-error.log warn;

    location /static/ {
        autoindex on;
        alias   /var/www/test/ourcase/static/;
    }

    location /media/ {
        autoindex on;
        alias   /var/www/test/ourcase/media/;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://test_server;
            break;
        }
    }

    #For favicon
    location  /favicon.ico {
        alias /var/www/test/test/static/img/favicon.ico;
    }
    #For robots.txt
    location  /robots.txt {
        alias /var/www/test/test/static/robots.txt ;
    }
    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /var/www/test/ourcase/static/;
    }
}

我的服务器的域名是dehatiengineers.in

gunicorn_start.sh文件是:

#!/bin/bash

NAME="ourcase"                              #Name of the application (*)
DJANGODIR=/var/www/test/ourcase             # Django project directory (*)
SOCKFILE=/var/www/test/run/gunicorn.sock        # we will communicate using this unix socket (*)
USER=root                           # the user to run as (*)
GROUP=webdata                                     # the group to run as (*)
NUM_WORKERS=1                                     # how many worker processes should Gunicorn spawn (*)
DJANGO_SETTINGS_MODULE=ourcase.settings             # which settings file should Django use (*)
DJANGO_WSGI_MODULE=ourcase.wsgi                     # WSGI module name (*)

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /var/www/test/venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /var/www/test/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE

现在,我正在运行我的nginx和gunicorn_start.sh文件。 nginx正在运行,你可以在http://www.dehatiengineers.in/看到,但是django没有连接到nginx。现在在sudo sh gunicorn_start.sh,我得到以下输出:

Starting ourcase as root
gunicorn_start.sh: 16: gunicorn_start.sh: source: not found
[2016-05-20 06:21:45 +0000] [10730] [INFO] Starting gunicorn 19.5.0
[2016-05-20 06:21:45 +0000] [10730] [INFO] Listening at: unix:/var/www/test/run/gunicorn.sock (10730)
[2016-05-20 06:21:45 +0000] [10730] [INFO] Using worker: sync
[2016-05-20 06:21:45 +0000] [10735] [INFO] Booting worker with pid: 10735

但是,在浏览器中,我只是获得nginx输出,而不是原始输出。

除此之外,我在运行gunicorn_ourcase.service文件时遇到问题:

[Unit]
Description=Ourcase gunicorn daemon

[Service]
Type=simple
User=root
ExecStart=/var/www/test/gunicorn_start.sh

[Install]
WantedBy=multi-user.target

运行问题:

$ systemctl enable gunicorn_ourcase
systemctl: command not found

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

我所做的一切都很好,除了/etc/nginx/sites-enabled/使用它的默认配置文件这一事实。我必须删除/编辑default中名为angular.module('propertyList', ['route']) .controller('PropertyController', ['$scope','$http', function ($scope, $http) { $scope.properties = [{"latitude":53.796318,"longitude":-1.536373}, {"latitude":51.507923946494,"longitude":-0.14952014363775}, {"latitude":53.81893054791,"longitude":-1.5845336370246},...] $scope.postcodes = []; for(var i=0; i<$scope.properties.length; i++){ var lat = $scope.properties[i].latitude; var lng = $scope.properties[i].longitude; var geolocation = { "geolocations" : [{ "longitude": lng, "latitude": lat, "radius": 100, "limit": 1 },] }; $http.post('https://api.postcodes.io/postcodes', geolocation) .success(function(data){ var location = angular.fromJson(data); var postcode = location.result[0].result[0].postcode; $scope.postcodes += postcode+','; }); } $scope.problem = $scope.postcodes; //this line doesn't work. }]); 的文件。编辑后,一切正常。