django+uwsgi+nginx+gevent部署

項目工程目錄以下:
圖片.pngjavascript

第一步,測試runserver
cd django3demo1/mysite
python manage.py runserver 0.0.0.0:8002
http://127.0.0.1:8002/polls/ 訪問正常css

第二步, 配置uwsgi啓動
配置文件uwsgi.inijava

[uwsgi1]
ini=:base
uwsgi-socket=0.0.0.0:8101
stats=0.0.0.0:8201
pidfile=/mnt/run/django3demo1.pid
;使用supervisor管理進程時不能設置daemonize模式
daemonize = /mnt/logs/uwsgi.log
;logger=file:$(LOG_ROOT)uwsgi1.log
;env=LOG_FILE_SUFFIX=default_01
;代碼更改自動鏈式重啓,也可手動touch /mnt/run/reload_lobby1
;touch-chain-reload=/mnt/run/reload_lobby1
enable-threads=true
single-interpreter=true

[base]
;加載指定的python WSGI模塊
module=mysite.wsgi
;開啓主進程模式
master=true
;設置socket的監聽隊列大小(默認:100)
listen=100
;設置工做進程的數量,默承認以設置爲cpu核數*2
processes=2
;定義日誌格式
;logformat=%(addr) - [%(ctime)] %(method) %(uri) %(proto) %(status) %(size) [%(msecs) msecs] [pid:%(pid)] [rss:%(rssM)M] [vsz:%(vszM)M] "%(uagent)"
logformat=%(addr) - [%(ctime)] %(method) %(uri) %(proto) %(status) %(size) %(msecs) msecs "%(referer)" "%(uagent)"
;通過N秒不活躍狀態後銷燬全部工做進程(這時就進入了cheap模式),只會剩下主進程
;idle=30
;爲每一個工做進程設置請求數的上限,超過上限後工做進程就會被回收
max-requests=50000
;順序重載每一個工做進程,優先加載完的工做進程當即開始工做,須要配合touch-chain-reload參數使用才能生效
lazy-apps=true
;開啓內存使用狀況報告
memory-report=true
;每一個工做進程的線程數
threads=1
;請求超過harakiri指定的秒數以後,會丟棄這個請求,且當前woker會被回收(即重啓woker)
harakiri=45
;當一個請求是「harakiri」殺死的,會記錄信息到uwsgi日誌裏。打開這個選項會輸出額外的信息
harakiri-verbose=true

啓動uwsgi
uwsgi --ini mysite/uwsgi.ini:uwsgi1
uwsgi --stop /mnt/run/django3demo1.pid
uwsgi --reload /mnt/run/django3demo1.pidpython

第三步, nginx配置反向代理到uwsginginx

upstream mysite_app {
    server 127.0.0.1:7101 weight=5;
}

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

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/json;
    #gzip_vary on;

    location / {
        include uwsgi_params;
        uwsgi_pass mysite_app;
    }
}

http://127.0.0.1:8000/polls/ 訪問正常shell

第四步, uwsgi啓動配置gevent(可選)
uwsgi --gevent 100 --gevent-monkey-patch --ini mysite/uwsgi.ini:uwsgi1django

第五步, shell方法啓動uwsgi
改爲以人類能直觀看懂的方式,配置文件bin/h~~~~ttp_server.shjson

#!/bin/bash
function usage()
{
cat <<EOF
Usage: bin/http_server.sh {start|reload|stop|gevent-start|restart}

Options:
    [-h|--help]                          : show this help
EOF
}

SCRIPT_PATH=$0
echo $SCRIPT_PATH
WORK_PATH=$(dirname $(dirname $SCRIPT_PATH))
echo $WORK_PATH

while [ "$#" -gt 0 ] 
do
    case "$1" in
    -h|--help)
        usage
        exit 1
        ;;  
    start) 
        count=ps auxf|grep uwsgi.ini|grep -v grep |wc -l`
        if [ $count -gt 0 ]; then
            echo "http server already start ......"
            exit 1
        fi
        uwsgi --ini mysite/uwsgi.ini:uwsgi1
        exit 1
        ;;  
    restart) 
        echo "killing servers ... "
        ps auxf|grep uwsgi.ini|grep -v grep|awk {'print $2'}|sudo xargs kill -9
        echo "wait for http server to exit...."
        sleep 3
        uwsgi --ini mysite/uwsgi.ini:uwsgi1
        exit 1
        ;;  
    gevent-start) 
        uwsgi --ini mysite/uwsgi.ini:uwsgi1
        exit 1
        ;;  
    stop) 
        uwsgi --stop /mnt/run/django3demo1.pid
        exit 1
        ;;  
    reload) 
        uwsgi --reload /mnt/run/django3demo1.pid
        exit 1
        ;;  
    esac
    shift 
done

啓動uwsgi
sh bin/http_server.sh startbash

第六步, 配置supervisor啓動uwsgi
配置文件: mysite_supervisor.confapp

[program:uwsgi1]
directory = /mnt/hgfs/vmshare/study/python/django3demo1/mysite
environment = PYTHONPATH=./
command = /mnt/venv/python38/bin/uwsgi --ini mysite/supervisor_uwsgi.ini:uwsgi
autostart = true
;startsecs = 30
autorestart = true
startretries = 3
user = zyb
redirect_stderr = True
stdout_logfile = /mnt/logs/supervisor.log
stdout_logfile_maxbytes = 0
stdout_logfile_backups = 0
stdout_capture_maxbytes = 10MB
priority = 31
stopsignal=QUIT

pip3 install supervisorsudo supervisorctl updatesudo supervisorctl restart allsupervisor的具體使用方式就不具體介紹了

相關文章
相關標籤/搜索