Giter Club home page Giter Club logo

flask_deploy's Introduction

使用nginx和uwsgi部署flask应用

安装python3

  1. 安装编译python的信赖包

    yum install -y gcc make libffi-devel zlib-devel bzip2-devel openssl-devel xz-libs wget
    
  2. 编译安装python的最新版本

    • 从官网下载最新的源代码并解压缩

      wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
      tar -xvf Python-3.7.3.tar.xz
      
    • 编译安装

      cd Python-3.7.3
      #安装到/usr/local下与自带的python区分开
      ./configure --prefix=/usr/local
      make && make install
      
    • 设置PATH,更改默认python

      export PATH="/usr/local/bin:$PATH"
      

安装使用uwsgi

为了统一管理,在python的虚拟环境下安装使用uwsgi,不使用全局的uwsgi

mkdir -p /var/www/flask_api
cd /var/www/flask_api
python -m venv venv
source venv/bin/activate
pip install flask uwsgi

接下来可以用uWSGI来跑Flask应用

uwsgi --http 0.0.0.0:8080 --home env --wsgi-file flask_uwsgi.py --callable app --master

测试上面的程序是否成功

curl http://127.0.0.1:8080

编译安装nginx

  1. 安装信赖包

    yum install pcre-devel gcc make zlib-devel lua-devel perl
    
  2. 下载最新版本的openssl和nginx源代码

    wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz
    wget https://nginx.org/download/nginx-1.14.2.tar.gz
    
  3. 编译安装

    tar -xvf openssl-1.1.1b.tar.gz
    tar -xvf nginx-1.14.2.tar.gz
    cd nginx-1.14.2
    ./configure  --prefix=/usr/local/nginx --with-stream --with-stream_ssl_preread_module  --with-http_v2_module --with-http_ssl_module  --with-pcre  --with-openssl=../openssl-1.1.1b --with-openssl-opt='enable-tls1_3'
    make -j 4
    make install
    
  4. 添加systemctl配置, 添加 /usr/lib/systemd/system/nginx.service,并将下面内容写入

    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  5. 修改nginx配置, 先使用useradd nginx添加nginx用户, 再将下面内容覆盖/usr/local/nginx/conf/nginx.conf, 最后在/usr/local/nginx/conf下面添加vhosts文件夹,最后所有域名的配置都可以放到这个目录下面。

    user  nginx;
    worker_processes  8;
    
    events {
    	worker_connections  1024;
    }
    
    
    http {
    	include       mime.types;
    	default_type  application/octet-stream;
    
    	log_format  main  '$remote_addr - $request_time [$time_local] "$request" '
    					  '$status $body_bytes_sent $bytes_sent $content_length $request_length "$http_referer" '
    					  '"$http_user_agent" "$http_x_forwarded_for"';
    
    	access_log  logs/access.log  main;
    
    	sendfile        on;
    	#tcp_nopush     on;
    
    	#keepalive_timeout  0;
    	keepalive_timeout  65;
    
    	#gzip  on;
    	include vhosts/*.conf;
    }

参考

  1. 发一个 Fedora23 上自动搭建、配置 Flask 的 shell 脚本
  2. Installing Python 2.7.8 on CentOS 6.5
  3. mking/flask-uwsgi
  4. How To Deploy Flask Web Applications Using uWSGI Behind Nginx on CentOS 6.4
  5. 使用uwsgi和Nginx部署flask应用
  6. 阿里云部署 Flask + WSGI + Nginx 详解
  7. audit2why
  8. SELinux policy for nginx and GitLab unix socket in Fedora 19
  9. nginx install官网

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.