Flask+uwsgi+Nginx环境搭建

Posted 努力哥-运维自动化

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask+uwsgi+Nginx环境搭建相关的知识,希望对你有一定的参考价值。

    1. 开源软件准备
      需要的软件列表:
      setuptools-33.1.1.zip
      Python-2.7.13.tgz
      pip-9.0.1.tar.gz
      nginx-1.10.3.tar.gz
      软件统一上传到/usr/local/src/下,python是使用自己编译的。
      技术分享
    2. Python安装

      先安装以下的软件(如果有的话,就可以不安装了,例如使用rpm -qa zlib-devel先查看下)
      yum install zlib-devel -y
      yum install openssl-devel -y

      tar -zxvf解压
      ./configure --prefix=/usr/local/python #安装到/usr/local/python目录下。
      make 
      make install

    3. setuptool和pip安装
      unzip解压setuptool
      /usr/local/python/bin/python setup.py install

      tar -zxvf解压pip
      /usr/local/python/bin/python setup.py install
       
    4. flask和uwsgi安装
      使用pip安装flask和uwsgi
      /usr/local/python/bin/pip install flask
      /usr/local/python/bin/pip install uwsgi
    5. nginx安装

      先安装pcre-devel:
      yum install pcre-devel -y

      ./configure --prefix=/usr/local/nginx
      make && make install

    6. nginx使用uwsgi配置

      cat /usr/local/nginx/conf/nginx.conf

      #user  nobody;
      worker_processes  1;
      
      events {
          worker_connections  1024;
      }
      
      http {
          include       mime.types;
          default_type  application/octet-stream;
          log_format  main  $remote_addr - $remote_user [$time_local] "$request" ‘
                            $status $body_bytes_sent "$http_referer" ‘
                            ‘"$http_user_agent" "$http_x_forwarded_for"‘;
          sendfile        on;
          keepalive_timeout  65;
          server {
              listen       8080;
              access_log  /usr/local/nginx/logs/flask_access.log main;
              error_log /usr/local/nginx/logs/flask_error.log error;
              client_max_body_size 200M;
              location / {
                  include      uwsgi_params;
                  uwsgi_pass   unix:/dev/shm/uwsgi.sock;
                  index  index.html index.htm;
              }
          }
      
      }


      启动nginx:
      /usr/local/nginx/sbin/nginx  -t
      /usr/local/nginx/sbin/nginx

      注意:uwsgi_pass   unix:/dev/shm/uwsgi.sock;需要和uwsgi的配置相对应!

    7. uwsgi配置
      cat /root/uwsgi.ini
      [uwsgi]
      uid = root
      gid = root
      socket = /dev/shm/uwsgi.sock
      chmod-socket = 666
      enable-threads = true
      master = true
      plugins = /usr/local/python/bin/python
      vhost = true
      workers = 5
      max-requests = 1000
      pidfile = /var/run/uwsgi.pid
      daemonize = /var/log/uwsgi.log
      chdir = /root/ops_dev/
      module = ops_dev
      callable = app

      启动uwsgi,有些报错没什么事:
      /usr/local/python/bin/uwsgi /root/uwsgi.ini
      技术分享

    8. 编写测试样例进行测试

      cat /root/ops_dev/ops_dev.py

      # -*- coding: UTF-8 -*-
      from flask import Flask
      app = Flask(__name__)
      
      @app.route(‘/‘, methods=[‘GET‘, ‘POST‘])
      def index():
          return ‘Hello  world,Python ops_dev!‘

      编写完需要重启uwsgi,开改动代码都需要重启uwsgi,直接使用pkill进行重启即可:
      pkill uwsgi

      访问http://localhost:8080/进行测试。
      技术分享

      访问日志记录(nginx和uwsgi的访问日志,可用来排错。):
      技术分享

      技术分享

以上是关于Flask+uwsgi+Nginx环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

nginx+uwsgi+python+flask环境搭建

NGINX+UWSGI+PYTHON+FLASK环境搭建——————学习笔记

centos+python2+flask+nginx+uwsgi环境搭建

centos+python2+flask+nginx+uwsgi环境搭建

nginx+uwsgi+flask搭建后怎么访问.py文件

CentOS7下python3+Flask+uWSGI+Nginx+Supervisor环境搭建