uwsgi nginx 部署 flask

送 Doris 限量T恤,快来围观!>>> hot3.png

当前项目目录

.
├── app
├── app.log
├── app.py
├── config.py
├── manager.py
├── Pipfile
├── Pipfile.lock
├── __pycache__
│   ├── app.cpython-36.pyc
│   ├── app.cpython-37.pyc
│   └── view.cpython-36.pyc
├── uwsgi.ini
└── uwsgi.pid

app.py:

from flask import Flask, request
from flask_restful import Resource, Api

import logging
import json

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

handler = logging.FileHandler('app.log', encoding='UTF-8')
logging_format = logging.Formatter(
        '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')
handler.setFormatter(logging_format)
app.logger.addHandler(handler)

api = Api(app)


class AgentMock(Resource):
        def get(self):
                return {"method": "get"}

        def post(self):
                app.logger.error("---begin--")
                data = request.form.to_dict()
                app.logger.error(data)
                return data


api.add_resource(AgentMock, '/mock')
if __name__ == '__main__':
        app.run(host="10.0.100.159")

manager.py:

from flask_script import Manager, Shell

from app  import app

manager=Manager(app)

def make_shell_context():
    return dict(app=app)

manager.add_command("shell",Shell(make_context=make_shell_context))

@manager.command
def deploy():
    '''run deployment tasks'''
    pass


if __name__=='__main__':
    manager.run()

安装nginx和uwsgi

注意uwsgi一定要使用pip来安装,安装过程有问题自己百度解决

pip install uwsgi

uwsgi配置:

在服务器上使用的pipenv管理虚拟环境

[uwsgi]
http=0.0.0.0:5000   #uwsgi启动时,所使用的地址和端口(这个是http协议的,启动uwsgi之后可以使用curl来测试使用
socket=0.0.0.0:8001
#nginx与uwsgi通讯推荐使用socket,所以在配置nginx的时候应该使用socket配置,如果http同时存在.那么在nginx配置的时候一定更要使用socket的端口
chdir = /opt/code/garen
# 当前项目目录
wsgi-file = manager.py
# 入口文件.
stats = 127.0.0.1:9191
#状态监测地址
callable = app
# 回调应用
processes = 4
threads = 2
virtualenv = /root/.local/share/virtualenvs/garen-AI5T5shl
# 虚拟环境路径

supervisor配置

[program:flask]
command=/usr/local/bin/uwsgi /opt/code/garen/uwsgi.ini
directory=/opt/code/garen/
user=root
autostart=true
autorestart=true
stdout_logfile=/var/log/flask.log

nginx 配置:

改掉默认的nginx.conf得端口为非80,这里自定义的conf才可以使用80端口.

flask.conf

    server {
        listen       80;
        server_name  localhost;

        location / {
         include  uwsgi_params;
         uwsgi_pass 127.0.0.1:8001;
        }

    }

启动supervisor和nginx.

测试:

[root@sti-zbe2e-001 code]# curl http://10.0.100.159/mock
{"method": "get"}

你可能感兴趣的:(uwsgi nginx 部署 flask)