Flask+uwsgi+nginx项目部署

最近开发中用到了python,在项目中要做发布,其实简单,但还是花费了一些时间才搞明白怎么使用

网络上的资源大多比较零散,因此我把自己操作的完整过程记录下来,作为备忘吧

1、首先,我需要提供一个外部api供别人调用,因此查阅资料后发现了Flask这个东西,用了之后发现确实很方便

但是要搭建真正的支持并发的web服务,简单的Flask提供的restfull api感觉还不够用,继续查阅资料后,采用了Flask+uwsgi+nginx的方案

2、Flask、uwsgi、nginx安装和简单说明

Flask是一个轻量级的web应用框架,还有Django等

Flask安装:

 

pip install flask

 

Flask的简单应用如下(manage.py)

 

from flask import Flask
app = Flask(__name__)

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

if __name__ == '__main__':   #服务器部署时,注释掉这两行,不需要启动main函数
    app.run()

 

uwsgi是一种接口,定义web服务器和web应用之间的接口规范

uwsgi安装:

 

pip install uwsgi

 

Nginx是一个高性能的http和反省代理web服务器

Nginx安装:

 

rpm -ivh http://nginx.org/packages/centos/6/x86_64/RPMS/nginx-1.10.0-1.el6.ngx.x86_64.rpm
sudo yum install nginx

 

3、服务器环境配置

配置uwsgi,保存在uwsgiconfig.ini文件

 

[uwsgi]

socket = 127.0.0.1:5000     #  启动程序时所使用的地址和端口,通常在本地运行flask项目,
                            #  Flask地址和端口是127.0.0.1:5000,
                            #  不过在服务器上是通过uwsgi设置端口,通过uwsgi来启动项目,
                            #  也就是说启动了uwsgi,也就启动了项目。
chdir = /home/www/          #  项目目录

wsgi-file = manage.py      # flask程序的启动文件,通常在本地是通过运行  
                           # python manage.py runserver 来启动项目的

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

processes = 4     	   #  处理器个数

threads = 2     	   #  线程个数

stats = 127.0.0.1:9191     #  获取uwsgi统计信息的服务地址

pidfile = uwsgi.pid        #  保存pid信息,方便停止服务和重启的时候用

daemonize = ./log/uwsgi.log  #  后台运行时记录uwsgi的运行日志

lazy-apps = true             #  当需要连接cassandra时,uwsgi无法启动服务,可以使用该选项设置

master-fifo = /opt/mt-search/web-service/mfifo   # 使用chain-reloading 逐个work重启,服务不中断, 命令是 echo c > mfifo

touch-chain-reload = true


	   #  程序内启用的application变量名

processes = 4     	   #  处理器个数

threads = 2     	   #  线程个数

stats = 127.0.0.1:9191     #  获取uwsgi统计信息的服务地址

pidfile = uwsgi.pid        #  保存pid信息,方便停止服务和重启的时候用

daemonize = ./log/uwsgi.log  #  后台运行时记录uwsgi的运行日志

lazy-apps = true             #  当需要连接cassandra时,uwsgi无法启动服务,可以使用该选项设置

master-fifo = /opt/mt-search/web-service/mfifo   # 使用chain-reloading 逐个work重启,服务不中断, 命令是 echo c > mfifo

touch-chain-reload = true


 

配置Nginx:

/etc/nginx/nginx.conf 会默认调用下面的配置文件,因此只需要修改下面配置文件即可

sudo vim /etc/nginx/conf.d/default.conf

 

server {
        listen       80;
        server_name  127.0.0.1;

    #charset koi8-r;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:5000;
        uwsgi_param UWSGI_PYHOME /path/mtenv;  #python位置,要么是虚拟机,要么是运行的环境变量位置
        uwsgi_param UWSGI_CHDIR /path/python-prj/flask-prj;  #项目根目录
        uwsgi_param UWSGI_SCRIPT manage:app;    #启动项目的主程序,如果manage.py位域flask-prj的src/flask/这样的目录下,那么就写成src/flask/manage:app这样

        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


4、服务启动

 

启动、停止和升级uwsgi(不需要启动python main.py这样):

 

uwsgi uwsgiconfig.ini

uwsgi --ini uwsgiconfig.ini

uwsgi  uwsgiconfig.ini --daemonize //后台运行启动

uwsgi --stop uwsgi.pid  //停止服务

uwsgi --reload uwsgi.pid  //可以无缝重启服务

 

备注:当需要启动后台线程时,uwsgi也会无法启动代码,这时候可以用进程替代线程

 

启动Nginx:

 

nginx  //启动
nginx -s stop/quit //停止
nginx -s reload   //重启加载配置

 

 

 

参考资料

1、http://blog.csdn.net/lihao21/article/details/52304119

2、http://docs.jinkan.org/docs/flask/

3、https://segmentfault.com/a/1190000004294634

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值