Python3+Flask+uwsgi Nginx

安装 python 虚拟环境
pip install python3         # 安装 python3
mkdir flask_uwsgi           # 创建部署 flask 的文件夹
cd flask_uwsgi
python3 -m venv ./ven       # 创建虚拟环境,放在测试目录下 ven 目录中,此命令将创建目标目录
source env/bin/activate     # 激活虚拟环境
deactivate                  # 退出虚拟环境
安装 flask,以下是在 python 虚拟环境中运行
pip install flask       # 安装 flask

​ 创建 myapp.py 以便运行网站

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

@app.route("/moco")
def moco():
    return "Hello moco!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

​ 运行 python myapp.py,正常情况浏览器访问 主机地址:50000 进行访问(防火墙开放该端口)

安装 uwsgi (中文说明文档)
  1. 安装依赖包
    A. 安装 libxml:yum install libxml # 本次未安装
    B. 安装 python-devel:yum install python-devel -y
    C. 安装编译工具:yum install -y gcc* pcre-devel openssl-devel
    D. yum install python3-devel

  2. 安装 uwsgi

   pip install uwsgi
  1. 安装 uwsgi 成功后,创建 test.py 测试下
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    # return ["Hello World"] # python2
    return [b"Hello World"] # python3

​ 运行 uwsgi:uwsgi –http:8000 –wsgi-file test.py,正常可以访问 主机地址:8000 进行访问(防火墙开放该端口)

开始 uwsgi 部署 flask

这里简化下,就用 uwsgi 部署1中的 myapp.py,新建一个 config.ini 文件,内容如下:

[uwsgi]
http = 127.0.0.1:5000
# 虚拟环境中的目录,这里env后边不要/bin
home = /home/flask_uwsgi/env
# 启动的文件
wsgi-file =  /home/flask_uwsgi/myapp.py
# python 程序内用以启动的 application 变量名,不加callable=app,访问时报服务器错误Internal Server Error
callable = app
# 处理器数
processes = 1
# 线程数
threads = 1
buffer-size = 32768
master = true
stats = /home/flask_uwsgi/uwsgi.status
pidfile = /home/flask_uwsgi/uwsgi.pid

执行 uwsgi config.ini,项目启动成功后,可通过浏览器访问 主机地址:50000 进行访问(防火墙开放该端口)

添加 uwsgi 到系统服务

创建 service 文件,路径 sudo vim /etc/systemd/system/myproject.service

[Unit]
Description=uWSGI instance to serve jdapi
After=network.target

[Service]
[Unit]
Description=uWSGI instance to serve jdapi
After=network.target

[Service]
WorkingDirectory=/root/flask_uwsgi
ExecStart=/root/flask_uwsgi/env/bin/uwsgi --ini /root/flask_uwsgi/config.ini
ExecStop=/root/flask_uwsgi/env/bin/uwsgi --stop /root/flask_uwsgi/uwsgi.pid
ExecReload=/root/flask_uwsgi/env/uwsgi --reload /root/flask_uwsgi/uwsgi.pid
[Install]
WantedBy=multi-user.target
说明
[Unit] 主要是对这个服务的说明,内容包括Description和After,Description用于描述服务,After用于描述服务类别
[Service] 是服务的关键,是服务的一些具体运行参数的设置
WorkingDirectory 你的项目目录
ExecStart 服务启动的代码
ExecReload 重启命令
ExecStop 停止命令
WantedBy=multi-user.target 指明会跟随系统启动而启动该服务
备注 uwsgi的路径必须是自己编译是的venv环境下的uwsgi,要不然启动之后找不到Flask项目中的依赖包。
安装 nginx
  1. 安装依赖包(正常前面应该依赖包都安装好了)
# 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装
yum install gcc-c++
# PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令
yum install -y pcre pcre-devel
# zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
# OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
yum install -y openssl openssl-devel
  1. 安装 nignx

    a. 直接下载最新版 .tar.gz 安装包,页面地址:https://nginx.org/en/download.html

    ​ wget -c https://nginx.org/download/nginx-1.20.0.tar.gz

    b. 解压

    tar -zxvf nginx-1.20.0.tar.gz
    cd nginx-1.20.0
    

    c. 配置

    ./configure                  # 默认配置
    
    ./configure \                # 自定义配置(不推荐)
    --prefix=/usr/local/nginx \
    --conf-path=/usr/local/nginx/conf/nginx.conf \
    --pid-path=/usr/local/nginx/conf/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi
    # 注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
    

    d. 编译安装

    make
    make install
    whereis nginx                # 查找安装目录
    

    e. 启动、停止 nginx

    cd /usr/local/nginx/sbin/
    ./nginx 
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload
    # 启动时如果报 80 端口被占用
    # nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    # 查询那个应用占用 80 端口
    netstat -lnp|grep 80
    # 停止该服务(或 kill 该进程)
    service stop httpd           # 或者 kill -9 进程号
    # 修改 nginx.conf 后,可以用 nginx -s reload 进行重启
    # 可以 添加到服务,以便开机启动。或者在rc.local增加启动代码
    vi /etc/rc.local         # 增加一行 /usr/local/nginx/sbin/nginx,并设置权限 chmod 755 rc.local
    
nginx 通过 uwsgi 部署 flask
  1. 修改 config.ini 配置,socket 一项有变化,启动 uwsgi, uwsgi config.ini
    [uwsgi]
    socket = 127.0.0.1:5000
    # 虚拟环境中的目录,这里env后边不要/bin
    home = /home/flask_uwsgi/env
    # 启动的文件
    wsgi-file =  /home/flask_uwsgi/myapp.py
    # python 程序内用以启动的 application 变量名,不加callable=app,访问时报服务器错误Internal Server Error
    callable = app
    # 处理器数
    processes = 1
    # 线程数
    threads = 1
    buffer-size = 32768
    master = true
    stats = /home/flask_uwsgi/uwsgi.status
    pidfile = /home/flask_uwsgi/uwsgi.pid
    
  2. 修改 nginx 配置,重新加载 nginx -s reload
    server {
       listen 80;
       server_name test.com;
       location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:5000;
        }
    }
    

    END

评论关闭

return top