zoukankan      html  css  js  c++  java
  • 通过uwsgi+nginx启动flask的python web程序

    通过uwsgi+nginx启动flask的python web程序


    一般我们启动python web程序的时候都是通过python直接启动主文件,测试的时候是可以的,当访问量大的时候就会出问题
    python manage.py

    通过wsgi web服务器网关接口规范启动是一种比较好的方式:

    web服务器 nginx + uwsgi + flask

    原理就是nginx通过代理访问通过uwsgi启动监听在本机的flask程序

    1.安装uwsgi模块
    # pip install uwsgi
    2.通过uwsgi启动flask项目
    # 通过http方式启动(推荐)
    # uwsgi --http 127.0.0.1:9999 -w user:app


    # 通过socket方式启动
    uwsgi -s 127.0.0.1:9999 -w user:app
    # 主入口需要__init__.py文件
    user/__init__.py

    # cat /etc/nginx/conf.d/cmdb.conf 
    server {
        listen       8080;
        server_name  cmdb;
    
    
        location / {
            root   /home/python/Desktop/51reboot/cmdb/;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:9999;
        }
    }

    访问方式是:
    http://192.168.3.91:8080 --> uwsgi:9999

    centos6.8_x64安装python3.7.2和wusgi
    
    wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
    
    tar -xf Python-3.7.2.tar.xz 
    cd Python-3.7.2
    mkdir /usr/local/python372
    ./configure --prefix=/usr/local/python372
    make
    make install
    
    
    # 报错
        from _ctypes import Union, Structure, Array
    ModuleNotFoundError: No module named '_ctypes'
    
    # 解决办法:
    
    yum install libffi-devel libffi
    
    
    # 再次编译安装即可
    ./configure --with-ssl --prefix=/usr/local/python372
    make
    make install
    echo $?
    
    # 删除原来的软链,重新建立软连接
    # rm -f /usr/local/python3
    cd /usr/local
    ln -s python372 python3
    
    [root@srv3:/usr/local/python3]# python3 -V
    Python 3.7.2
    
    # 安装ipython
    # pip3 install ipython
    # ln -s /usr/local/python372/bin/ipython3 /usr/sbin/ipython3

    [root@srv3:
    /usr/local/uwsgi]# tar zxf uwsgi-latest.tar.gz [root@srv3:/usr/local/uwsgi]# cd uwsgi- uwsgi-2.0.18/ uwsgi-latest.tar.gz [root@srv3:/usr/local/uwsgi]# cd uwsgi-2.0.18/ [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# python3 uwsgiconfig.py --build ################# uWSGI configuration ################# kernel = Linux execinfo = False ifaddrs = True locking = pthread_mutex event = epoll timer = timerfd filemonitor = inotify pcre = True routing = True capabilities = False yaml = embedded json = False ssl = True xml = libxml2 debug = False plugin_dir = . zlib = True ucontext = True malloc = libc ############## end of uWSGI configuration ############# total build time: 3 seconds *** uWSGI is ready, launch it with ./uwsgi *** # 继续install # python3 setup.py install [root@srv3:~]# whereis uwsgi uwsgi: /usr/bin/uwsgi /etc/uwsgi /usr/local/bin/uwsgi /usr/local/uwsgi /opt/uwsgi-2.0.13.1/bin/uwsgi [root@srv3:~]# uwsgi --version 2.0.18 # 编写测试文件 [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# cat testuwsgi.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # 启动 [root@srv3:/usr/local/uwsgi/uwsgi-2.0.18]# ./uwsgi --http :9000 --wsgi-file testuwsgi.py *** Starting uWSGI 2.0.18 (64bit) on [Wed Jun 19 02:39:19 2019] *** compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-23) on 19 June 2019 07:37:03 os: Linux-2.6.32-642.6.2.el6.x86_64 #1 SMP Wed Oct 26 06:52:09 UTC 2016 nodename: srv3.keepvid.com machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 24 current working directory: /usr/local/uwsgi/uwsgi-2.0.18 detected binary path: /usr/local/uwsgi/uwsgi-2.0.18/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 128183 your memory page size is 4096 bytes detected max file descriptor number: 65536 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :9000 fd 4 spawned uWSGI http 1 (pid: 927) uwsgi socket 0 bound to TCP address 127.0.0.1:25232 (port auto-assigned) fd 3 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.7.2 (default, Jun 19 2019, 02:23:57) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x1e8dcd0 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72920 bytes (71 KB) for 1 cores *** Operational MODE: single process *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1e8dcd0 pid: 926 (default app) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 926, cores: 1) [pid: 926|app: 0|req: 1/1] 127.0.0.1 () {28 vars in 298 bytes} [Wed Jun 19 02:39:58 2019] GET / => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 switches on core 0) # 验证 [root@srv3:~]# curl http://127.0.0.1:9000/ Hello World

    ./configure --with-ssl --prefix=/usr/local/python372
    编译的时候一定要添加 --with-ssl (cenos7.x上可以)否则pip3 install -r 安装模块时会报错:
    pip is configured with locations that require TLS/SSL, however the ssl module in Python is not avail

    centos6安装python3.7.x参考 https://www.cnblogs.com/reblue520/p/11103311.html

  • 相关阅读:
    hdu1865 1sting (递归+大数加法)
    hau1021 Fibonacci Again(递归)
    hdu2553 N皇后问题(dfs+回溯)
    BZOJ2342 [Shoi2011]双倍回文 【manacher】
    Manacher算法学习 【马拉车】
    BZOJ2743 [HEOI2012]采花 【离线 + 树状数组】
    BZOJ1296 [SCOI2009]粉刷匠 【dp】
    BZOJ1086 [SCOI2005]王室联邦 【dfs + 贪心】
    BZOJ1565 [NOI2009]植物大战僵尸 【最大权闭合子图 + tarjan缩点(或拓扑)】
    BZOJ2157 旅游 【树剖 或 LCT】
  • 原文地址:https://www.cnblogs.com/reblue520/p/7884943.html
Copyright © 2011-2022 走看看