1. 程式人生 > 實用技巧 >Flask部署

Flask部署

Flask部署

安裝必備環境

配置

安裝必備環境

nginx,uwsgi

配置

uwsgi

啟動配置檔案

# 配置 /home/liuwei/mycode/py/FlaskDemo/uwsgi.ini

[uwsgi]
#application's base folder,配置為專案的根目錄
base = /home/liuwei/mycode/py/FlaskDemo

#python module to import
# 這個app的值指的就是flask應用下的app.py
app = app
module = %(app)

# 專案依賴的環境目錄
home = /home/liuwei/mycode/py/envs/flaskdemoenv
pythonpath 
= %(base) #socket file's location,生成nginx依賴的檔案,其中%n表示當前檔案的名字 socket = %(base)/%n.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module imported at line #6 callable = app #location of log files logto = /var/log/uwsgi/%n.log

日誌輸出檔案

# 建立上面配置logto的資料夾,以及降許可權
sudo mkdir -p /var/log/uwsgi sudo chown -R liuwei:liuwei /var/log/uwsgi

啟動

# cd  /home/liuwei/mycode/py/FlaskDemo
uwsgi --ini uwsgi.ini

# 當啟動後,就會在專案目錄下生成uwsgi.sock檔案,這個檔案被nginx依賴,所以你要先啟動uwsgi

#日誌需要注意引數:
    # current working directory
    # detected binary path
    # uwsgi socket 0 bound to UNIX address
  #
PythonHome、Python Version # 是否有報錯資訊 # 檢視啟動成功的日誌: *** Starting uWSGI 2.0.18 (64bit) on [Tue Dec 10 17:12:46 2019] *** compiled with version: 7.4.0 on 07 December 2019 04:39:53 os: Linux-5.0.0-37-generic #40~18.04.1-Ubuntu SMP Thu Nov 14 12:06:39 UTC 2019 nodename: liuwei-ThinkPad-E450 machine: x86_64 clock source: unix detected number of CPU cores: 4 current working directory: /home/liuwei/mycode/py/FlaskDemo detected binary path: /home/liuwei/mycode/py/envs/flaskdemoenv/bin/uwsgi !!! no internal routing support, rebuild with pcre support !!! *** WARNING: you are running uWSGI without its master process manager *** your processes number limit is 31398 your memory page size is 4096 bytes detected max file descriptor number: 1048576 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uwsgi socket 0 bound to UNIX address /home/liuwei/mycode/py/FlaskDemo/uwsgi.sock fd 3 Python version: 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0] Set PythonHome to /home/liuwei/mycode/py/envs/flaskdemoenv *** Python threads support is disabled. You can enable it with --enable-threads *** Python main interpreter initialized at 0x565080299420 your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 72904 bytes (71 KB) for 1 cores *** Operational MODE: single process *** added /home/liuwei/mycode/py/FlaskDemo/ to pythonpath. WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x565080299420 pid: 31106 (default app) *** uWSGI is running in multiple interpreter mode *** spawned uWSGI worker 1 (and the only) (pid: 31106, cores: 1)

nginx

啟動配置檔案

user root;
worker_processes auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# /×××/ngin  -c  /×××/nginx.conf   -s   stop
pid /usr/local/nginx/nginx.pid;


events {
    worker_connections  1024;
}

http {

    include /usr/local/nginx/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"';

    #access_log  logs/access.log  main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
  

    gzip on;
    gzip_disable "msie6";

    server {
        listen 80 default_server;
        #listen [::]:80 default_server;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        root /home/liuwei/mycode/py/FlaskDemo;

        server_name _;


        location /static {
            alias /home/liuwei/mycode/py/FlaskDemo/static;
        }

       location / {
            try_files $uri @yourapplication;
        }
        
         location @yourapplication {
            include /usr/local/nginx/uwsgi_params;
            # 這是和django有差別的地方
            uwsgi_pass unix:/home/liuwei/mycode/py/FlaskDemo/uwsgi.sock;
        }

    }
}

日誌輸出檔案

# 建立上面配置logto的資料夾,以及降許可權
sudo mkdir -p /var/log/nginx
sudo chown -R liuwei:liuwei /var/log/nginx

# 啟動
/***/nginx -t -c /***/nginx.conf

/***/nginx -c /***/nginx.conf

/***/nginx -c /***/nginx.conf -s stop