zoukankan      html  css  js  c++  java
  • Python3+flask+nginx+uwsgi

    配置项目的时候一般使用虚拟环境,是各个项目的环境独立起来,更多方便管理。至于如何使用搜索即可,并不难
    1、安装python3
    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
    xz -d Python-3.6.4.tar.xz
    tar -xvf Python-3.6.4.tar
     
    ./configure --prefix=/usr/local/python3.6.4 --enable-shared CFLAGS=-fPIC
    加上--enable-shared和-fPIC之后可以将python3的动态链接库编译出来,默认情况编译完lib下面只有python3.xm.a这样的文件,python本身可以正常使用,但是如果编译第三方库需要python接口的比如caffe等,则会报错;所以这里建议按照上面的方式配置,另外如果openssl不使用系统yum安装的,而是使用自己编译的比较新的版本可以使用--with-openssl=/usr/local/openssl这种方式指定,后面目录为openssl实际安装的目录,另外编译完还要将openssl的lib目录加入ld运行时目录中即可. 
     
    make;make install;
    添加软连接
    ln -s /usr/local/python3.6.4/bin/python3 /usr/bin/python3
    ln -s /usr/local/python3.6.4/bin/pip3 /usr/bin/pip3
     
    防止运行Python是提示找不到库
    cp libpython3.6m.so.1.0 /usr/lib/ /usr/lib64/
     
    Python3安装后导致yum不能使用,因为执行yum需要python2的版本,修改两个文件
    vim /usr/bin/yum vim /usr/libexec/urlgrabber-ext-down 把#!/usr/bin/python 修改为 #!/usr/bin/python2
     
    2、先安装nginx(https://www.cnblogs.com/fwqblogs/p/10132205.html)。配置uwsgi
    pip install uwsgi
    ln -s /usr/local/python3.6.4/bin/uwsgi /usr/bin/uwsgi
     
    vim config.ini
     
    [uwsgi]
    chdir=/home/Py
    pythonpath= /home/Py
    #项目启动文件
    wsgi-file = /home/Py/run.py
    #为你的项目入口文件
    module=run
    #实例化flask的变量 application = Flask(__name__)
    callable=application
    #当修改文件内容后重启uwsgi
    py-autoreload=1
    #开启一个master进程监控项目运行
    master=true
    #uwsgi的端口。要与项目运行的端口一致
    socket=/home/Py/flask_socket.sock
    socket=127.0.0.1:5000
    processes=4
    #threads=2
    buffer-size=65536
    #允许用内嵌的语言启动线程。这将允许你在app程序中产生一个子线程。
    enable-threads=true
    pidfile=/tmp/uwsgi-master.pid
    uid=nginx
    gid=root
    #当服务器退出的时候自动删除unix socket文件和pid文件。
    vacuum=true
    #皇帝”模式运行。在这种模式下,它将件事uWSGI配置文件的路径并且为每一个它找到的文件生成实例(‘诸侯’)。无论什么时候一个配置文件被修改了,皇帝都会自动重启诸侯
    emperor=true
    logto=/var/log/nginx/uwsgi.log
     
    后台启动
    uwsgi --ini config.ini &
     
    ● 常用选项:
    socket : 地址和端口号,例如:socket = 127.0.0.1:50000
    processes : 开启的进程数量
    workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
    chdir : 指定运行目录(chdir to specified directory before apps loading)
    wsgi-file : 载入wsgi-file(load .wsgi file)
    stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
    threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
    master : 允许主进程存在(enable master process)
    daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
    pidfile : 指定pid文件的位置,记录主进程的pid号。
    vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
    disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
    [pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)
     
    配置nginx
    location /{
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:5000;
    uwsgi_param UWSGI_PYHOME /usr/bin/python; #也可对应虚拟环境
    uwsgi_param UWSGI_CHDIR /home/Py;
    uwsgi_param UWSGI_SCRIPT run:application;
    }
    有定义路由前缀home时 http://192.168.164.128/home/index/
     
    最后如果有多个项目的话 如果用域名到很简单配置多个nginx server 和uwsgi就可以
    下面将配置使用ip访问不同项目
    假如我有两个项目 分贝在 Py1 和 Py2
    匹配该路径下的项目 重定向到81端口 定义81端口的项目uwsgi配置 一个项目对应一个uwsgi配置
    location /Py1/{
    proxy_pass http://127.0.0.1:81/;
    }
    server{
    listen 81;
    server_name localhost;
    location /{
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:5000;
    uwsgi_param UWSGI_PYHOME /usr/bin/python;
    uwsgi_param UWSGI_CHDIR /home/Py1;
    uwsgi_param UWSGI_SCRIPT run:application;
    }
    }
     
    location /Py2/{
    proxy_pass http://127.0.0.1:82/;
    }
    server{
    listen 82;
    server_name localhost;
    location /{
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:5000;
    uwsgi_param UWSGI_PYHOME /usr/bin/python;
    uwsgi_param UWSGI_CHDIR /home/Py2;
    uwsgi_param UWSGI_SCRIPT run:application;
    }
    }
     
     
  • 相关阅读:
    IOS创建Button简单实例
    IOSActionSheet、AlertView、Slider、Switch的简单使用
    IOS翻转注意
    [教程]iOS 4 开发的好东西 (资料)url
    Proxy和Decorator模式
    Using JNDI connecting DB
    影片“One Day”,还不错
    Minabased TCP server examples
    MySQL vs. Oracle on sequence
    Java DB and JDBC(Embedded Derby)
  • 原文地址:https://www.cnblogs.com/fwqblogs/p/10132476.html
Copyright © 2011-2022 走看看