uwsgi生产环境部署flask项目

项目目录结构

 

开发调试环境

     开发环境中启动的web服务是由flask内置的web服务器来提供的,所以服务启动的时候监听的网卡和端口是在项目中的app.py中进行指定

from flask import Flask,render_template,request,redirect,session
from flask import jsonify,abort,json,make_response,send_from_directory,send_file
from flask_bootstrap import Bootstrap
from main import userhandler
from main import msgconfig as m
import os
from urllib.parse import unquote,quote


app = Flask(__name__)
bootstrap = Bootstrap(app)


@app.route('/index',methods=['GET'])
def index():
    return send_file("static/pages/sitetable.html")

@app.route('/')
def main():
    return send_file("static/pages/sitetable.html")

@app.route('/login',methods=['POST'])
def login():
    loginame = request.form.get('loginuser')
    loginpwd = request.form.get('loginpwd')
    result = userhandler.checkuser(loginame, loginpwd)
    if result:
        session['user'] = {'user': loginame}
        m.loginmsg=""
        return redirect("/index")
    m.loginmsg="error"
    return redirect("/")

@app.route('/site',methods=['GET'])
def get_sites():
    sites = userhandler.get_sitesexcelfile()
    return jsonify({'sites': sites})

@app.route('/phone/site',methods=['GET'])
def get_phonesites():
    sites = userhandler.get_sitesyamlfile()
    return jsonify({'sites': sites})


@app.route('/upload',methods=['POST'])
def upload():
    f = request.files['file']
    userhandler.upload_excelfile(f)
    return "success"


@app.route('/downsite',methods=['GET'])
def downtxt():
    return send_from_directory('static/files/','sites.xlsx', as_attachment=True)


@app.route('/initsites',methods=['POST'])
def uploadtxt():
    f = request.files.get('myfile')
    if f:
        userhandler.upload_sitesexcel(f)
        return send_file("static/pages/sitetable.html")
    else:
        return "error"


@app.route('/initdir',methods=['GET'])
def initexecledir():
    userhandler.initexceldir()
    return "success"


@app.route('/excellist',methods=['GET'])
def getexcellist():
    sitename = request.args.get("sitename")
    templateName = request.args.get("templateName")
    sitename= unquote(unquote(sitename)).encode('utf-8').decode('utf-8-sig')
    filelist = userhandler.getexcellist(sitename, templateName)
    return jsonify(filelist)

@app.route('/uploadexcel',methods=['POST'])
def uploadexcel():
    f = request.files.get('myfile')
    sitename = request.form.get('sitename')
    templateName = request.form.get("templateName")
    userhandler.upload_excelfile(f, sitename, templateName)
    return send_file("static/pages/exceltable.html")

@app.route('/deletefile',methods=['GET'])
def deletefile():
    sitename = unquote(unquote(request.args.get("sitename")))
    filename = unquote(unquote(request.args.get("filename")))
    templateName = request.args.get("templateName")
    userhandler.deletefile(sitename, filename, templateName)
    return "success"


@app.route("/renamebase",methods=['GET'])
def renamefile():
    sitename =  unquote(unquote(request.args.get("sitename")))
    filename =  unquote(unquote(request.args.get("filename")))
    templateName = request.args.get("templateName")
    userhandler.renamefile(sitename, filename, templateName)
    return "success"

@app.route("/baksite",methods=['GET'])
def baksite():
    userhandler.sitebak()
    return "success"


@app.route('/phone/save',methods=['POST'])
def phonesaveexcel():
    sitename = request.form.get('sitename')
    sitename = unquote(unquote(sitename))
    data = request.form.get("data")
    userhandler.save_phoneexcel(sitename, data)
    return "success"


@app.route('/phone/basedata',methods=['GET'])
def getbasedatabyexcel():
    sitename = request.args.get("sitename")
    sitename = unquote(unquote(sitename))
    return userhandler.get_phonebasedata(sitename)


@app.route('/phone/savect',methods=['POST'])
def phonesavect():
    sitename = request.form.get('sitename')
    sitename = unquote(unquote(sitename))
    data = request.form.get("data")
    userhandler.save_phoneexcelct(sitename, data)
    return "success"




@app.before_request
def process_request():
    pass

@app.after_request
def process_response(response):
    response.headers['Access-Control-Allow-Origin'] = "http://192.168.0.1:81"
    response.headers["Access-Control-Allow-Credentials"]="true";
    return response


app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000, debug=True)
app.py

 启动调试服务器  直接使用python app.py即可

 

生产环境

[uwsgi]
socket=172.17.51.235:5050
processes=4
threads=2
master=true
pythonpath=/root/jiankong/appserver
module=manager
callable=app
memory-report=true
buffer-size=32768
static-map =/static=/root/jiankong/appserver/static
daemonize = /root/jiankong/appserver/log/uwsgi.log
uwsgi.ini
from app import app 

if __name__ == "__main__":
    app.run("0.0.0.0")
manager.py
from flask import Flask,render_template,request,redirect,session
from flask import jsonify,abort,json,make_response,send_from_directory,send_file
from flask_bootstrap import Bootstrap
from main import userhandler
from main import msgconfig as m
import os
from urllib.parse import unquote,quote


app = Flask(__name__)
bootstrap = Bootstrap(app)


@app.route('/index',methods=['GET'])
def index():
    return send_file("static/pages/sitetable.html")

@app.route('/')
def main():
    return send_file("static/pages/sitetable.html")

@app.route('/login',methods=['POST'])
def login():
    loginame = request.form.get('loginuser')
    loginpwd = request.form.get('loginpwd')
    result = userhandler.checkuser(loginame, loginpwd)
    if result:
        session['user'] = {'user': loginame}
        m.loginmsg=""
        return redirect("/index")
    m.loginmsg="error"
    return redirect("/")

@app.route('/site',methods=['GET'])
def get_sites():
    sites = userhandler.get_sitesexcelfile()
    return jsonify({'sites': sites})

@app.route('/phone/site',methods=['GET'])
def get_phonesites():
    sites = userhandler.get_sitesyamlfile()
    return jsonify({'sites': sites})


@app.route('/upload',methods=['POST'])
def upload():
    f = request.files['file']
    userhandler.upload_excelfile(f)
    return "success"


@app.route('/downsite',methods=['GET'])
def downtxt():
    return send_from_directory('static/files/','sites.xlsx', as_attachment=True)


@app.route('/initsites',methods=['POST'])
def uploadtxt():
    f = request.files.get('myfile')
    if f:
        userhandler.upload_sitesexcel(f)
        return send_file("static/pages/sitetable.html")
    else:
        return "error"


@app.route('/initdir',methods=['GET'])
def initexecledir():
    userhandler.initexceldir()
    return "success"


@app.route('/excellist',methods=['GET'])
def getexcellist():
    sitename = request.args.get("sitename")
    templateName = request.args.get("templateName")
    sitename= unquote(unquote(sitename)).encode('utf-8').decode('utf-8-sig')
    filelist = userhandler.getexcellist(sitename, templateName)
    return jsonify(filelist)

@app.route('/uploadexcel',methods=['POST'])
def uploadexcel():
    f = request.files.get('myfile')
    sitename = request.form.get('sitename')
    templateName = request.form.get("templateName")
    userhandler.upload_excelfile(f, sitename, templateName)
    return send_file("static/pages/exceltable.html")

@app.route('/deletefile',methods=['GET'])
def deletefile():
    sitename = unquote(unquote(request.args.get("sitename")))
    filename = unquote(unquote(request.args.get("filename")))
    templateName = request.args.get("templateName")
    userhandler.deletefile(sitename, filename, templateName)
    return "success"


@app.route("/renamebase",methods=['GET'])
def renamefile():
    sitename =  unquote(unquote(request.args.get("sitename")))
    filename =  unquote(unquote(request.args.get("filename")))
    templateName = request.args.get("templateName")
    userhandler.renamefile(sitename, filename, templateName)
    return "success"

@app.route("/baksite",methods=['GET'])
def baksite():
    userhandler.sitebak()
    return "success"


@app.route('/phone/save',methods=['POST'])
def phonesaveexcel():
    sitename = request.form.get('sitename')
    sitename = unquote(unquote(sitename))
    data = request.form.get("data")
    userhandler.save_phoneexcel(sitename, data)
    return "success"


@app.route('/phone/basedata',methods=['GET'])
def getbasedatabyexcel():
    sitename = request.args.get("sitename")
    sitename = unquote(unquote(sitename))
    return userhandler.get_phonebasedata(sitename)


@app.route('/phone/savect',methods=['POST'])
def phonesavect():
    sitename = request.form.get('sitename')
    sitename = unquote(unquote(sitename))
    data = request.form.get("data")
    userhandler.save_phoneexcelct(sitename, data)
    return "success"




@app.before_request
def process_request():
    pass

@app.after_request
def process_response(response):
    response.headers['Access-Control-Allow-Origin'] = "http://192.168.9.1:81"
    response.headers["Access-Control-Allow-Credentials"]="true";
    return response


app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
app.py
    server {
        listen        80;
        server_name   sjz;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
           include         uwsgi_params;
           uwsgi_pass      172.17.51.235:5000;
           uwsgi_param    UWSGI_CHDIR   /root/shijiazhuang/appserver;
           uwsgi_param    UWSGI_SCRIPT  manager:app;

        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {
        listen       81;
        server_name  jiankong;

        location / {
           include         uwsgi_params;
           uwsgi_pass      172.17.51.235:5050;
           uwsgi_param    UWSGI_CHDIR   /root/jiankong/appserver;
           uwsgi_param    UWSGI_SCRIPT  manager:app;

        }
    }
nginx.conf

 说明:

    生产环境中flask启动占用的网卡和端口全部委托给uwsgi服务来管理,不再使用开发环境中app.py中指定的网卡和端口等相关信息

    uwsgi.ini的socket=127.0.0.1:5050    5050表示uwsgi服务进程通过5050端口转发到flask web   在flask的app.py中就无需再指定监听端口

    nginx.conf中的   uwsgi_pass  127.0.0.1:5050;    uwsgi_param     UWSGI_SCRIPT  manager:app;

 

查看uwsgi服务是否成功启动

[root@iz2ze6e5pr6sbu9qxhzulaz ~]# ps -ef |grep uwsgi
root     23579     1  0 08:30 ?        00:00:00 uwsgi jiankong/appserver/uwsgi.ini
root     23581 23579  0 08:30 ?        00:00:00 uwsgi jiankong/appserver/uwsgi.ini
root     23582 23579  0 08:30 ?        00:00:00 uwsgi jiankong/appserver/uwsgi.ini
root     23584 23579  0 08:30 ?        00:00:00 uwsgi jiankong/appserver/uwsgi.ini
root     23585 23579  0 08:30 ?        00:00:00 uwsgi jiankong/appserver/uwsgi.ini
root     23592     1  0 08:30 ?        00:00:00 uwsgi shijiazhuang/appserver/uwsgi.ini
root     23594 23592  0 08:30 ?        00:00:00 uwsgi shijiazhuang/appserver/uwsgi.ini
root     23595 23592  0 08:30 ?        00:00:00 uwsgi shijiazhuang/appserver/uwsgi.ini
root     23597 23592  0 08:30 ?        00:00:00 uwsgi shijiazhuang/appserver/uwsgi.ini
root     23598 23592  0 08:30 ?        00:00:00 uwsgi shijiazhuang/appserver/uwsgi.ini
root     23715 23697  0 08:48 pts/1    00:00:00 grep --color=auto uwsgi
View Code

 

设置uwsgi为开机启动

       vi    /etc/rc.d/rc.local

      在/etc/rc.d/rc.local文件末尾追加  /root/a.sh

      chmod +x    /root/a.sh

      chmod  +x    /etc/rc.d/rc.local

    

 

var RestConfig = {
  "restip":"127.0.0.1",
  "restport":"5000"
};
本地config
var RestConfig = {
  "restip":"60.204.203.31",
  "restport":"81"
};
远程config

 

posted @ 2018-09-05 09:21  不懂123  阅读(4032)  评论(0编辑  收藏  举报