Python实战 - Django 部署

使用 runserver 启动
$ python3 manage.py runserver 0.0.0.0:8080 &
使用 fastcgi 命令启动 + shell 脚本 + nginx
#!/bin/bash

PROJDIR="/data/github/awesome-platform/app"
PIDFILE="$PROJDIR/app.awesome-platform.pid"
ERRORLOG="/data/logs/cms_app_error.log"
OUTLOG="/data/logs/cms_app_std_out.log"
APP_EXEC="python manage.py runfcgi method=prefork daemonize=true host=127.0.0.1"
NUM_PROC=12000
NUM_PROCS=4
MIN_SPARE=2
MAX_SPARE=4

cd $PROJDIR


function start_server() {
    cd $PROJDIR
    ulimit -n 65535
    $APP_EXEC port=$NUM_PROC minspare=$MIN_SPARE maxspare=$MAX_SPARE maxchildren=$NUM_PROCS  outlog=$OUTLOG errlog=$ERRORLOG
}

function stop_server() {
    ps aux | grep "$APP_EXEC" | grep "$NUM_PROC" | awk {print $2} | xargs kill -9
}

case "$1" in
start)
    start_server
;;
stop)
    stop_server
;;
restart)
    stop_server
    start_server
;;
*)
    echo Usage: app.sh [start|stop|restart]
esac
    nginx.conf
upstream cms_app {
    server 127.0.0.1:12000 fail_timeout=0;
}

server {
    listen 8000;
    server_name 118.25.81.114;
    charset utf-8;

    location  /static/ {
        alias /data/github/awesome-platform/app/statics/;
        expires 15m;
    }


    location / {
        fastcgi_pass cms_app;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param SERVER_NAME $server_name;
        fastcgi_param SERVER_PORT $server_port;
        fastcgi_param SERVER_PROTOCOL $server_protocol;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }

}
使用 uwsgi 命令启动
$ uwsgi --http :8001 --chdir /data/github/django-cms --module cms.wsgi
uwsgi 命令 + supervisord.conf
[program:cms1]
command=uwsgi --http :8001 --chdir /data/github/django-cms --module cms.wsgi
directory=/data/github/django-cms
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
uwsgi.ini + supervisord.conf + nginx.conf

推荐

    uwsgi.ini
[uwsgi]
socket = /home/ubuntu/cms.sock
chdir = /data/github/django-cms
wsgi-file = cms/wsgi.py
touch-reload = /home/ubuntu/reload
 
processes = 2
threads = 4
 
chmod-socket = 664
#chown-socket = ubuntu:www-data
 
vacuum = true

配置好了,可以测试

$ uwsgi uwsgi.ini --http :8002
    supervisord.conf
[program:cms_app]
command=uwsgi --ini /home/ubuntu/uwsgi.ini
directory=/data/github/django-cms
startsecs=0
    nginx.conf
server {
    listen      8080;
    server_name 118.25.81.114;
    charset     utf-8;

    client_max_body_size 75M;

    location /media  {
        alias /data/github/django-cms/media;
    }

    ### method1 for static conf
    location /static {
        alias /data/github/django-cms/static;
    }

    ### method2 for static conf
    #location  ~/static/  {
    #    root   /data/github/django-cms;
    #    break;
    #}

    location / {
        uwsgi_pass  unix:///home/ubuntu/cms.sock;
        include     /data/nginx/conf/uwsgi_params;
    }
}
参考资料
经验分享 程序员 微信小程序 职场和发展