• FLASK 部署22


    Linux 上利用Nginx代理uWSGI处理Flask web应用

     

    一、介绍

      最近开发要用一个测试环境,是这样的Nginx+uwsgi+flask 的一个结构。下面是一些记录,在Centos 系统上使用Flask 架构部署一个简单的Python应用。然后使用Nginx作为前端反向代理,设置uWSGI应用网关处理web应用程序。

    二、条件

    1)         环境要求

    • Server OS:最小化安装 Centos 6.8
    • Nginx :反向代理
    • Python2.6~2.7:开发语言
    • Flask/uWSGI:框架
    • Pip:python包管理工具
    • iptables&selinux:开放用到的端口,关闭selinux。

    2)         安装必备的开发工具以及环境

    更新系统:

    [root@hcloud ~]# yum update -y
    [root@hcloud ~]# yum groupinstall "Development tools" -y

    更新完成后,记得重启系统,然后安装工具:

    [root@hcloud ~]# yum install wget vim telnet python-devel zlib-devel openssl-devel bzip-devel -y
    
    [root@hcloud ~]# yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel python-devel libxml2  libxml2-devel  python-setuptools  zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make autoconf automake
     -y

    3)         安装python 包管理工具-pip

    [root@hcloud ~]# wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py

    或者参考此网站

    4)         安装uWSGI与Flask

    下载软件

    [root@hcloud ~]# mkdir soft
    [root@hcloud ~]# cd soft
    [root@hcloud soft]# wget https://pypi.python.org/packages/0c/1c/44849e293e367a157f1ad863cee02b4b865840543254d8fae3ecdebdbdb9/uwsgi-2.0.12.tar.gz
    [root@hcloud soft]# wget https://pypi.python.org/packages/db/9c/149ba60c47d107f85fe52564133348458f093dd5e6b57a5b60ab9ac517bb/Flask-0.10.1.tar.gz

    如果下载该软件比较慢,可以在/etc/reslov.conf dns中添加一个8.8.8.8 ,很快的哦。

    安装软件(开发要求固定版本,如果不介意版本的话,可以直接使用pip install packge 即可安装,相当于linux中的yum)

    复制代码
    [root@hcloud soft]# tar -xf Flask-0.10.1.tar.gz
    [root@hcloud ~]# cd Flask-0.10.1
    [root@hcloud Flask-0.10.1]# python setup.py install
    
    [root@hcloud soft]# tar -xf uwsgi-2.0.12.tar.gz
    [root@hcloud soft]# cd uwsgi-2.0.12
    [root@hcloud uwsgi-2.0.12]# python setup.py install
    复制代码

    5)         创建一个测试应用

    创建一个Web应用程序存放的文件夹

    [root@hcloud ~]# mkdir -pv /usr/local/webtest
    [root@hcloud ~]# cd /usr/local/webtest

    创建一个python 脚本,内容如下:

    复制代码
    [root@hcloud webtest]# cat myproject.py
    from flask import Flask
    application = Flask(__name__)
    
    @application.route("/")
    def hello():
        return "<h1 style='color:blue'>Hello Flask!</h1>"
    
    if __name__ == "__main__":
        application.run(host='0.0.0.0')
    复制代码

    创建WSGI入口点文件,名字为:wsgi.py,内容如下

    [root@hcloud webtest]# cat wsgi.py
    from myproject import application
    
    if __name__ == "__main__":
        application.run()

    6)         配置uWSGI服务器网关服务运行方式,运行端口等。

    上述两个python脚本文件创建完成后,uwsgi命令启动,运行在8000端口上,指定协议http,运行时会出现一些警告,暂时可以忽略,是因为当前是root账户运行的uwsgi,后面从配置文件启动时会配置普通用户nginx即可。

    [root@hcloud webtest]# uwsgi --socket 0.0.0.0:8000 --protocol=http -w wsgi
    验证下,看下监听端口
    [root@hcloud webtest]# netstat -aultnp | grep 8000
    tcp        0      0 0.0.0.0:8000                0.0.0.0:*                   LISTEN      7181/uwsgi          
    tcp        0      0 192.168.30.58:8000          192.168.30.65:48164         TIME_WAIT   -          

    可以打开浏览器访问测试一下:http://192.168.30.58:8000

    7)         创建uWSGI配置文件

    以.ini格式为uwsgi启动配置文件(还有xml,json,等多种格式),这样就不用在命令行输入一大堆命令了,用uwsgi命令启动,后面可以创建uwsgi启动脚本,加入到系统服务

    在项目的目录中,创建uwsgi.ini:

    复制代码
    [root@hcloud webtest]# cat uwsgi.ini 
    [uwsgi]
    uid = nginx
    gid = nginx
    socket = 127.0.0.1:8000  
    master = true
    vhost = true
    workers = 8
    reload-mercy = 10
    vacuum = true
    max-requests = 10000
    limit-as = 1024
    buffer-sizi = 3000
    pidfile = /var/run/uwsgi.pid
    daemonize = /var/log/uwsgi/uwsgi.log  
    chdir = /usr/local/webtest
    module = wsgi
    chmod-socket = 660
    enable-threads = true
    复制代码

    保存并退出。配置文件参考:http://heipark.iteye.com/blog/1847421

    大致的解释下:

    uid 、gid :以哪个用户、组启动服务

    socket:监听的ip及端口

    master: 启动主进程

    workes:服务器启动的进程数

    reload-mercy:平滑的重启

    pidfile :启动时的pid 文件。

    daemonize :启动日志

    module :web应用的入口模块名称

    enable-threads : 启用线程模式

    8)         创建开机系统启动脚本文件,名字为uwsgid。

    #! /bin/sh
    # chkconfig: 2345 55 25
    # Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
    # run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
    # distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
    
    ### BEGIN INIT INFO
    # Provides:          uwsgid
    # Required-Start:    $all
    # Required-Stop:     $all
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: starts the uwsgi web server
    # Description:       starts uwsgi using start-stop-daemon
    ### END INIT INFO
    
    # Author:   licess
    # website:  http://lnmp.org
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DESC="uwsgi daemon"
    NAME=uwsgi
    DAEMON=/usr/bin/uwsgi
    CONFIGFILE=/usr/local/webtest/$NAME.ini
    PIDFILE=/var/run/$NAME.pid
    SCRIPTNAME=/etc/init.d/uwsgid
    
    set -e
    [ -x "$DAEMON" ] || exit 0
    
    do_start() {
        $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
    }
    
    do_stop() {
        $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
        rm -f $PIDFILE
        echo "$DAEMON STOPED."
    }
    
    do_reload() {
        $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
    }
    
    do_status() {
        ps aux|grep $DAEMON | grep -v grep
    }
    
    case "$1" in
     status)
        echo -en "Status $NAME: 
    "
        do_status
     ;;
     start)
        echo -en "Starting $NAME: 
    "
        do_start
     ;;
     stop)
        echo -en "Stopping $NAME: 
    "
        do_stop
     ;;
     reload|graceful)
        echo -en "Reloading $NAME: 
    "
        do_reload
     ;;
     *)
        echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
        exit 3
     ;;
    esac
    
    exit 0
    uwsgid

    创建完成后,赋予执行权限,加入开机启动

    [root@hcloud ~]# chmod +x /etc/init.d/uwsgid 
    [root@hcloud ~]# chkconfig --add uwsgid
    [root@hcloud ~]# chkconfig --level 2345 uwsgid on

    检查下

    [root@hcloud ~]# chkconfig --list uwsgid
    uwsgid             0:off    1:off    2:on    3:on    4:on    5:on    6:off

    启动uwsgid

    [root@hcloud ~]# service uwsgid start
    Starting uwsgi: 
    [uWSGI] getting INI configuration from /usr/local/webtest/uwsgi.ini

    验证下监听端口(注:这里我们使用127.0.0.1 作监听,是为了后期在使用时,对外只有nginx可以访问web应用):

    [root@hcloud ~]# netstat -aultnp | grep :8000
    tcp        0      0 127.0.0.1:8000              0.0.0.0:*                   LISTEN      8945/uwsgi

    9)         安装Nginx配置代理

    先下载安装6.6的epel源

    [root@hcloud ~]# wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
    [root@hcloud ~]# rpm -ivh epel-release-6-8.noarch.rpm

    安装nginx

    [root@hcloud ~]# yum install nginx -y

    安装完成后,修改nginx默认配置文件default.conf,修改之前可以先备份下源文件噢。完整内容如下:

    复制代码
    [root@hcloud ~]# cat /etc/nginx/conf.d/default.conf
    server {
            listen       80;
            server_name  192.168.30.58;
              
            location / {
                include  uwsgi_params;
                uwsgi_pass  127.0.0.1:8000;
                uwsgi_param UWSGI_SCRIPT wsgi;
                uwsgi_param UWSGI_CHDIR /usr/local/webtest;
                index  index.html index.htm;
                client_max_body_size 35m;
            }
    } 
    复制代码

    以上内容代理了uwsgi的端口,客户端通过访问nginx的80端口,然后nginx在将请求转发到后台flask应用的8000端口上。

    启动nginx

    [root@hcloud ~]# service nginx start
    Starting nginx:                                            [  OK  ]

    在浏览器内测试访问一下nginx:http://192.168.30.58

    每个人都应是守望者,守望我们的心智,我们的理想,以防它在生活中不知不觉地坠落、被操控和被自己遗忘。。。
     
  • 相关阅读:
    be accustomed to doing|actual |acute|adapt |
    By virtue of|sustain|post |scrape off |stretch|access to|take into account of|exploit|hasten|blur |idle|bored her to|account for|accused of|cruelty
    Block to|wreck|Range|Reach|span|chase around|amuse|exploit |instructed
    blast -m1
    qdel
    uname|mv|tar -xzvf|
    Hypothesis Tests for One Population Mean When σ Is Unknown|other
    dll显示调用和隐式调用
    使用预编译头提高编译速度
    NMS过程图示
  • 原文地址:https://www.cnblogs.com/xiaoli3007/p/6090078.html
Copyright © 2020-2023  润新知