Flask uwsgi与nginx部署(详细配置)

环境

  • python2或3都支持
  • linux

uwsgi

UWSGI 下载

pip install uwsgi

UWSGI 配置

[uwsgi]
# 外部访问地址,可以指定多种协议,现在用http便于调试,之后用socket
# socket方式
socket=0.0.0.0:7777
# http方式
http=0.0.0.0:7777
# http与socket通用方式
http-socket=0.0.0.0:7777
# 项目路径
# chdir=/home/ubuntu/apt
pythonpath=/home/ubuntu/apt_epiboly
# 虚拟环境的目录路径
# virtualenv = /rasa/zndhjqr_nlp/venv
# 主程序文件
wsgi-file=/home/ubuntu/apt_epiboly/run.py
#wsgi-file=./run.py
# flask项目创建的application
callable=app_main
#主线程
master=true
# 处理器数
processes = 1
# 线程数
threads = 10
buffer-size = 65536

stats=/home/ubuntu/apt_epiboly/config/uwsgi.status
pidfile=/home/ubuntu/apt_epiboly/config/uwsgi.pid

# 网上查询说是防止假死的参数
reload-mercy = 1
worker-reload-mercy = 1

enable-threads = true

harakiri=180
#自动移除unix Socket 和 Pid 文件 当服务停止的时候
vacuum = true

# plugins=python

lazy-apps = true

pythonpath=/usr/lib/python2.7/dist-packages
#home=/home/ubuntu/hjy/python2
pythonpath=/usr/local/lib/python2.7/dist-packages

nginx

nginx 配置

该配置适用uwgi采用socket方式
该配置支持部署多个项目


user  root;
worker_processes 1;

#pid        logs/nginx.pid;


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"';

    #access_log  logs/access.log  main;
    access_log /var/log/nginx/access.log;  # 成功日志存储路径
    error_log /var/log/nginx/error.log;  # 失败日志存储路径

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    client_max_body_size 500M;

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        #
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;

        location / {
            try_files $uri $uri/ /index.html;
            root  /home/ubuntu/apt/dist;  # 参考前端打包的dist文件目录位置
            index  index.html index.htm;
            #add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Origin *;
            #add_header Access-Control-Allow-Credentials true
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            add_header Access-Control-Allow-Headers X-Requested-With;
            if ($request_method = OPTIONS){
                    return 200;
                }
            }

        location /api/ {
            include uwsgi_params;
            uwsgi_pass  127.0.0.1:8888;

            uwsgi_send_timeout 600;
            uwsgi_connect_timeout 600;
            uwsgi_read_timeout 600;

            #proxy_pass  http://127.0.0.1:8888/api/;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    server {
        listen       82;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        #
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;

        location / {
            try_files $uri $uri/ /index.html;
            root  /home/ubuntu/apt_epiboly/dist;
            index  index.html index.htm;
            #add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Origin *;
            #add_header Access-Control-Allow-Credentials true
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            add_header Access-Control-Allow-Headers X-Requested-With;
            if ($request_method = OPTIONS){
                    return 200;
                }
            }

        location /api/ {
            include uwsgi_params;
            uwsgi_pass  127.0.0.1:7777;

            uwsgi_send_timeout 600;
            uwsgi_connect_timeout 600;
            uwsgi_read_timeout 600;

            #proxy_pass  http://127.0.0.1:7777/api/;
            #proxy_set_header Host $host;
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    server {
        listen       81;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        #
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;

        location / {
            try_files $uri $uri/ /index.html;
            root  /home/ubuntu/apt_probe/dist;
            index  index.html index.htm;
            #add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Origin *;
            #add_header Access-Control-Allow-Credentials true
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            add_header Access-Control-Allow-Headers X-Requested-With;
            if ($request_method = OPTIONS){
                    return 200;
                }
            }

        location /api/ {
            include uwsgi_params;
            uwsgi_pass  127.0.0.1:6666;

            uwsgi_send_timeout 600;
            uwsgi_connect_timeout 600;
            uwsgi_read_timeout 600;

            proxy_pass  http://127.0.0.1:6666/api/;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

启动

启动uwsgi

路径参考自己的uwsgi.ini配置文件

nohup uwsgi --ini /home/apt/config/uwsgi.ini  >/home/apt/apt.log 2>&1  &

启动nginx

systemctl start nginx

访问

浏览器输入部署服务器ip和端口号

你可能感兴趣的:(Python,flask,nginx,ubuntu)