美文网首页
Flask+gunicorn+nginx实现单机部署

Flask+gunicorn+nginx实现单机部署

作者: 垃圾桶边的狗 | 来源:发表于2020-11-14 23:44 被阅读0次

安装相关包

pip install flask
pip insatll gunicorn
安装nginx

flask项目

qwq.png
App/__init__.py
from flask import Flask

from App.view import my_blue


def create_app():

    app = Flask(__name__)

    app.register_blueprint(my_blue)

    return app

App/view.py

from flask import Blueprint, render_template

my_blue = Blueprint('my_blue', __name__)

@my_blue.route('/')
def index():
    return render_template('my_html.html')

App/templates/my_html.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Hello Flask</h1>
</body>
</html>

my_nginx/manager.py

from flask_script import Manager

from App import create_app

app = create_app()

manager = Manager(app=app)

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

my_nginx/guncorn_config.py


# workers = 5    # 定义同时开启的处理请求的进程数量,根据网站流量适当调整
# worker_class = "gevent"   # 采用gevent库,支持异步处理请求,提高吞吐量
# bind = "0.0.0.0:8888"    # 监听IP放宽,以便于Docker之间、Docker和宿主机之间的通信


# -*- coding: utf-8 -*-

from multiprocessing import cpu_count

# 要与nginx端口一致
bind = ["0.0.0.0:5000"]

# 是否开启守护进程模式
daemon = False  

# 自动生成  用来杀死进程 kill -9 ‘gunicorn.pid内容’
pidfile = 'gunicorn.pid'

workers = cpu_count() * 2
worker_class = "gevent"
worker_connections = 65535

keepalive = 60
timeout = 30
graceful_timeout = 10
forwarded_allow_ips = '*'

# # # 日志处理
# capture_output = True
# loglevel = 'info'
# errorlog = 'error.log'

/etc/nginx/nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
        proxy_pass http://localhost:5000; # 需要改动这一行 端口号要与guincorn_config.py端口号一致
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

           }


 
    include servers/*;
}

在项目目录下输入

gunicorn manager:app -c gunicorn_conf.py
gunicorn manager:app -c gunicorn_conf.py & # 后台启动 或者将
daemon=True

不加端口查看浏览器

wqeq7.png

到这一个最最简单的flask项目部署完成

相关文章

网友评论

      本文标题:Flask+gunicorn+nginx实现单机部署

      本文链接:https://www.haomeiwen.com/subject/dvugbktx.html