Nginx+uwsgi+flask部署

系统环境:Centos6.7、Python2.7、pip2.7(安装方式参考前一篇)

安装

安装flask

pip2.7 install flask

安装uwsgi

#安装
pip2.7 install uwsgi

#验证安装
uwsgi --version

安装Nginx

我采用编译安装,直接跑的以前写的脚本,过程略,百度(Google)一大堆

配置

新建flask项目

mkdir -p /opt/flask/app #app为Flask程序目录

文件目录如下:
Nginx+uwsgi+flask部署_第1张图片

vim app/__init__.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello Flask'

if __name__ == '__main__':
    app.run('0.0.0.0')

vim manage.py #启动文件

from app import app

if __name__ == "__main__":
    app.run('0.0.0.0')

此时本地环境即可运行flask

python2.7 manage.py runserver

uwsgi配置

在项目目录下创建uwsgi8001.ini,用于启动uwsgi

[uwsgi]
uid = nginx
gid = nginx
# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:8001 

# 指向网站目录
chdir = /opt/flask/ 

# python 启动程序文件
wsgi-file = manage.py 

# python 程序内用以启动的 application 变量名
callable = app 

# 处理器数
processes = 4

# 线程数
threads = 2

#每次修改代码自动reload,无需要手动重启
py-autoreload = 1

#状态检测地址
stats = 127.0.0.1:9001

Nginx配置

server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        access_log  logs/host.access.log  main;

        location / {
            root   /opt/flask;
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8001;

            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP  $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   Referer http://$host;

            index  index.html index.htm;
        }

启动uwsgi、Nginx

uwsgi uwsgi8001.ini 

#检查配置
nginx -t
#reload
nginx -s reload

验证:
这里写图片描述

PS:可通过supervisor保持uwsgi持久运行

你可能感兴趣的:(python,flask,uwsgi,python)