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;
    }
}

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值