Python+Django+nginx+Linux+gunicorn部署

Owengoodman 2014-01-19

初次部署Django project in ubuntu server

Init env:

python -V  2.7.3

Django 1.6

Linux   ubuntu

Steps:

  • install nginx 
  • install gunicorn      sudo apt-get install gunicorn
  • create django project    django-admin.py startproject demos
  • test gunicorn is useful     cd demos && gunicorn demos.wsgi:application --bind 0.0.0.0:9090
  • config nginx for demos
upstream demos {
     server 127.0.0.1:9090 fail_timeout=0;
     #server unix:/var/www/demos/run/gunicorn.sock
}

server {

    listen 8000;
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;
    root /var/www/demos;
    server xxxx.com;
    
    location / {
         proxy_pass http://demos;
    }

    location /static {
             .....
    }

    error_page 404 500 504 /404.html;
    location = /404.html {
        root html;
    }
}

测试页面   http://127.0.0.0:8000   ,see "Congratulations on your first Django-powered page."    successfully

  • run gunicorn process backend, using supervisor
  • sudo apt-get install supervisor
  • config supervisor
  • config shell script for gunicorn
#!/bin/bash

name="demos"
project_dir=/var/www/demos
sockfile=/var/www/demos/run/gunicorn.sock
address=0.0.0.0
port=9090
num_wks=3
dj_setting_module=demos.settings
dj_wsgi_module=demos.wsgi

echo "Starting $name as `whoami`"

rundir=$(dirname $sockfile)
test -d $rundir || mkdir -p $rundir

exec gunicorn ${dj_wsgi_module}:application \
--workers $num_wks \
--log-level=debug \
--bind=$address:$port
#--bind=unix:$sockfile
  •  update supervisor.conf
[program:demos]
command = /var/www/demos/run/runserver.sh 
stdout_logfile = /var/www/demos/logs/gunicorn_supervisor.log
redirect_stderr = true
  •  start supervisorctl demos

Refs:

http://ijcdigital.com/blog/django-gunicorn-and-nginx-setup

http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/

相关推荐

yyyxxxs / 0评论 2020-05-10