您的位置:首页 > 运维架构 > Nginx

nginx+uwsgi部署flask(新)

2015-12-02 00:00 513 查看
可以直接yum install uwsgi uwsgi-plugin-python安装
uwsgi-plugin-python是必不可少了
否则uwsgi的log日志会提示– unavailable modifier requested: 0

然后nginx配置文件

server {
listen       80;
server_name  localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
}
}

flask 入口文件app.py

#!/usr/bin/env python
#coding:utf-8

from flask import Flask

app = Flask(__name__)
app.debug = True

@app.route('/')
def hello():
return "Hello World!"

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

uwsgi app.xml配置文件

<uwsgi>
<pythonpath>/home/webapp/flaskapp</pythonpath> #网站根目录
<module>app</module>     #Flask的主入口文件,即上面的app.py
<callable>app</callable>   #app.py里面的程序入口
<socket>127.0.0.1:9090</socket>   #socke或者端口
<master/>
<plugins>python</plugins>
<processes>4</processes>         #进程数
<memory-report/>
</uwsgi>

uWSGI /usr/local/bin/uwsgiser管理脚本

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
printf "Error: You must be root to run this script!\n"
exit 1
fi

if [ "$1" = "start" ]; then
psid=`ps aux|grep "uwsgi"|grep -v "grep"|wc -l`
if [ $psid -gt 2 ];then
echo "uwsgi is running!"
exit 0
else
/usr/sbin/uwsgi -s 127.0.0.1:9090 -M -p 4 -t 30 -x app.xml --limit-as 128 -R 10000 --plugin python -d /var/log/uwsgi.log
/usr/local/nginx/sbin/nginx -s reload
fi
echo "Start uwsgi service [OK]"
elif [ "$1" = "stop" ];then
killall -9 uwsgi
echo "Stop uwsgi service [OK]"
elif [ "$1" = "restart" ];then
killall -9 uwsgi
/usr/sbin/uwsgi -s 127.0.0.1:9090 -M -p 4 -t 30 -x app.xml --limit-as 128 -R 10000 --plugin python -d /var/log/uwsgi.log
/usr/local/nginx/sbin/nginx -s reload
echo "Restart uwsgi service [OK]"
else
echo "Usages: uwsgiserver [start|stop|restart]"
fi

这样可直接通过uwsgiser start/stop/restart

如果启动提示 unable to load app 0 (mountpoint='') (callable not found or import error)
则把run.py里面按照如下修改

# coding: utf-8
# __author__ = 'hex'

from app import app

if __name__ == "__main__":  # 增加的这行
app.run(host='0.0.0.0',debug=True)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: