Python3 – Nginx+uwsgi+Flask 部署到Centos服务器

前面我们讲了如何使用uwsgi部署flask代码到centos服务器; 本章节结合实际业务中, 用户并发量较大, 通常采用Nginx + uwsgi + Flask 的形式部署, 提升性能。通过本篇博文, 或许你会有收获~
演示配置: Centos 7.9 , uwsgi 2.0.20, Flask 2.0.2

1.创建虚拟环境

pip install virtualenv
pip install virtualenvwrapper

非root登录的, 所以 vim .bash_profile

export PATH=$PATH:/usr/local/python3/bin
alias pip=/usr/local/python3/bin/pip3.9
alias python=/usr/local/python3/bin/python3.9
export WORKON_HOME=$HOME/.virtualenvs #指定虚拟环境存放目录
source /home/harry/.local/bin/virtualenvwrapper.sh  # virtualenvwarpper.sh 所在目录

cd 到项目, 创建虚拟环境

mkvirtualenv blogprojectenv

虚拟环境路径

/home/harry/.virtualenvs/blogprojectenv

2.nginx

轻量级, 并发能力强, 高度模块话, 负载均衡, 反向代理的能力

常用指令:

sudo systemctl enable ngix 设置开机自启动
sudo systemctl disenable ngix 设置禁止开机自启动
sudo systemctl status nginx 查看状态
sudo systemctl start nginx 启动
sudo systemctl restart nginx 重新启动
sudo systemctl stop nginx 停止

默认打开路径        /usr/share/nginx/html;

查看进程:
ps -ef | grep nginx

启动指定配置:
sudo nginx -c /home/harry/BlogProject/nginx1.conf
控制nginx
sudo nginx -s stop 快速关闭
                            quit 优雅的关闭
                            reload 重新加载配置
Nginx 配置文件
指令:
    nginx -t 不运行, 仅测试配置文件
    nginx -c configpath 从指定路径加载启动配置文件
    nginx -t -c configpath 测试指定配置文件

3. nginx配置文件结构:

main    全局配置
events{ 工作模式, 连接配置

}
http{       http的配置
        upstream xxx{  负载均衡配置
        }
        server{ 主机设置
                location xxx{ URL匹配
                }
        }
}
1.main包含什么呢?

user nginx;  worker进程运行的用户和组, 通常是登录的用户

worker_processes 1;  指定Nginx开启的子进程数, 多核CPU建议设置和CPU数量一样的进程数, 或者写 auto

error_log xxx level; 用来定义全局错误日志文件, 通常放在var中, level有debug, info, notice, warn, error, crit

pid xxx;  指定进程id的存储文件位置
2.events 指定工作模式和连接上限
events{
    use epoll;
    worker_connections 1024;
}

use 指定nginx工作模式
        epoll  高效工作模式, linux
        kqueue 高效工作模式, bsd
        poll   标准模式
        select 标准模式

worker_connections 定义nginx每个进程的最大连接数
        正向代理   连接数 * 进程数
        反向代理     连接数 * 进程数 / 4
        linux系统限制最多能同时打开65535个文件, 默认上限是65535, 可接触 ulimit -n 65535
最核心的模块, 主要负责http服务器相关配置, 包含server, upstream子模块
include mime.types; 设置文件的mime类型
include xxxconfig; 包含其他配置文件, 分开规划解耦
defalut_type xxx;  设置默认类型为二进制流, 文件类型未知时就会使用默认
log_format   设置日期格式
sendfile     设置高效文件传输模式
keepalive_timeout   设置哭护短连接活跃超时
gzip             gzip压缩

        server{ 用来指定虚拟主机
                listen 80;                       指定虚拟主机监听的端口
                server_name localhost; 指定ip地址或域名, 多个域名使用空格隔开
                charset utf-8;               指定网页的默认编码格式
                error_page 500 502 /50x.html 指定错误页面
                access_log xxx main;   指定虚拟主机的访问日志存放路径
                error_log xxx main;      指定虚拟主机的错误日志存放路径
                root xxx;                        指定这个虚拟主机的根目录
                index xxx;                       指定默认首页
        }
server{
    location xxx{
                ...
    }
}
核心中的核心, 以后主要配置都在这里
主要功能: 定位url, 解析url, 支持正则匹配, 还能支持条件, 实现动静分离
语法
        location [modifier] uri{
                    ...
        }
modifier修饰符
        =    使用精准匹配并且终止搜索结果
        ~        区分大小写的正则表达式
        ~*   不区分大小写的正则表达式
        ^~   最佳匹配, 不是正则匹配, 通常用来匹目录
常用指令
        alias  别名, 定义location的其他名字, 在文件系统中能够找到, 如果location指定了正则表达式, alias将会引用正则表达式中的捕获, alias替代location中匹配的部分, 没有匹配的部分将会文件系统中搜索
proxy_pass URL; 反向代理转发地址, 默认不转发header, 需要转发header则需要设置proxy_set_header HOST $host;

proxy_method POST;                         转发的方法名
proxy_hide_header Cache_Control; 指定头部不被转发
proxy_pass_header Cache_Control; 指定哪些头部转发
proxy_pass_request_header on;    设置转发http请求头
proxy_pass_request_body on;          设置转发请求体

负载均衡模块, 通过一个简单的调度算法来实现客户ip到后端服务器的负载均衡

写法
upstream myproject{
        ip_hash;
        server 127.0.0.0.1:8000;
        server 127.0.0.0.1:8001 down;
        server 127.0.0.0.1:8002 weight=3;
        server 127.0.0.0.1:8003 backup;
}
负载均衡算法
        weight  负载均衡权重
        down        当前server不参与负载均衡
        backup  其他机器全down掉或满载使用此服务
        ip_hash 按每个请求的hash结果分配
        fair    按后端响应事件来分(第三方的)
Django服务器
            runserver
            wsgi
uwsgi: web服务器, 多线程处理的不错
            1. pip install uswgi
            2. 工程目录下创建uwsgi.ini 配置文件
            3. 书写配置信息
            4. 使用uwsgi服务器
                                - 启动    uwsgi --ini uwsgi.ini
                                - 停止      uwsgi --stop uwsgi.pid
nginx配置
            location /static{
                                alias xxx/static/;

            }
            location / {
                                include uwsgi_params;
                                uwsgi_pass localhost:8000;
            }
For more information on configuration, see:
  * Official English Documentation: http://nginx.org/en/docs/
  * Official Russian Documentation: http://nginx.org/ru/docs/

user harry;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    include             /etc/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  /var/log/nginx/access.log  main;

    sendfile            on;
   tcp_nopush          on;
   tcp_nodelay         on;
    keepalive_timeout   65;
  types_hash_max_size 4096;
  gzip on;

 #include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        root /home/harry/blogproject;
        location /static {
            alias /home/harry/blogproject/static;
        }
        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_pass localhost:8080;
           # root /home/harry/blogproject;
           # index index.html index.htm;
        }
        error_page 500 502 503 504  /50x.html;
        location = /50x.html {
            root  /usr/share/nginx/html;
        }

    }
}

4. 安装uwsgi

Linux下 换conda镜像源

将以上配置文件写在 ~/.condarc
vim ~/.condarc

channels:
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true
安装uwsgi:
conda install -c conda-forge libiconv

conda install -c conda-forge uwsgi
  • 在本项目中, 不使用conda管理;

有一个很坑的问题, uwsgi 默认一直用的是python2.7, 会一直报各种错误, 如果想使用最新Python3.9,则需要以下操作:

删除所有的uwsgi, 然后安装插件

yum install -y uwsgi-plugin-python36
pip3 install uwsgi

查看uwsgi的python版本
uwsgi --python-version

uWSGI wsgi都是协议, 其中uWSGI包括wsgi, http等

WSGI server 是Flask内置的调试服务器

cd 项目
touch uwsgi.ini
[uwsgi]
使用nginx时使用
socket = 0.0.0.0:8080

直接作为web服务器使用
http=0.0.0.0:8080
配置工程目录
chdir = /home/harry/BlogProject

配置项目的wsgi目录, 相当于工程目录
wsgi-file=OnlineStore/wsgi.py

适用于Flask项目部署
wsgi-file = manager.py
router
callable = app

配置进程, 线程信息
processes = 4

threads = 10

enable-threads = True

master = True

这里的p指的是processes
pidfile = uwsgi.pid

oaemonize = uwsgi.log
启动
uwsgi --ini uwsgi.ini
uwsgi --stop uwsgi.pid
/home/harry/uwsgi/uwsgi 是通过代码编译安装的
ls -s /home/harry/uwsgi/uwsgi /usr/bin/uwsgi

uwsgi的python版本
uwsgi --python-version

pythonpath = /home/harry/anconda2/versions/3.6.1/lib/python3.6/site-packages
 19 virtualenv = /root/.pyenv/versions/3.6.1

5. 启动nginx和uwsgi

终于完成了所有的配置, 下面就可以正常启动了~

sudo nohup nginx -c /home/harry/blogproject/nginx.conf &
sudo nohup uwsgi --ini /home/harry/blogproject/uwsgi.ini &

Original: https://blog.csdn.net/qq_31810357/article/details/123285557
Author: 韩俊强
Title: Python3 – Nginx+uwsgi+Flask 部署到Centos服务器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/747763/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球