nginx+uwsgi+supervisor快餐部署

需要文件

Miniconda3-latest-Linux-x86_64.sh,下载地址

安装Nginx

  • 执行yum install -y yum-utils
  • 在/etc/yum.repos.d创建nginx.repo,并添加如下内容
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    
    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    
  • 执行yum install -y nginx

安装Miniconda

  • chmod u+x Miniconda3-latest-Linux-x86_64.sh
  • 执行Miniconda3-latest-Linux-x86_64.sh,并根据提示完成安装
  • 在用户home目录创建.condarc文件,也可通过conda config命令创建
  • 在.condarc文件中添加如下内容
    show_channel_urls: true
    channels:
      - defaults
      - conda-forge
    default_channels:
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
      - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
    custom_channels:
      conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
      simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
    
  • 执行conda create -n flask python=3.7.3 pylint requests flask libiconv创建虚拟环境。若没有安装libiconv会报如下错误:
    uwsgi: error while loading shared libraries: libiconv.so.2: cannot open shared object file: No such file or directory

配置uWSGI

  • 在虚拟环境中执行conda install -y uwsgi
  • 上传项目文件,在项目目录(本文为/home/flask/FlaskTest)中创建uwsgi.ini,并添加如下内容(删除注释)。
    [uwsgi]
    socket = 127.0.0.1:8001    //与nginx通信的端口
    chdir = /home/flask/FlaskTest/    //你的Flask项目目录
    wsgi-file = hello.py    //项目启动文件
    callable = app      //hello.py文件中flask实例化的对象名
    processes = 1     //处理器个数
    threads = 1     //线程个数
    stats = 127.0.0.1:9191   //获取uwsgi统计信息的服务地址
    
  • 在.bash_profile中增加export FLASK_ENV='production'
  • 代码DEBUG配置项改为False
  • 在虚拟环境中执行uwsgi uwsgi.ini可在控制台上启动应用。验证uwsgi.ini配置。

配置supervisor

  • 在虚拟环境中执行conda install -y supervisor
  • 创建/home/flask/supervisor目录
  • 执行echo_supervisord_conf > /home/flask/supervisor//supervisord.conf生成配置文件模板
  • 修改配置文件如下所示
    [unix_http_server]
    file=/home/flask/supervisor/supervisor.sock
    
    [supervisord]
    logfile=/home/flask/supervisor/supervisord.log
    logfile_maxbytes=50MB
    logfile_backups=10
    loglevel=info
    pidfile=/home/flask/supervisor/supervisord.pid
    nodaemon=false
    minfds=1024
    minprocs=200
    
    [rpcinterface:supervisor]
    supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
    
    [supervisorctl]
    serverurl=unix:///home/flask/supervisor/supervisor.sock
    
    [program:flask]
    command=/home/flask/miniconda3/envs/flask/bin/uwsgi /home/flask/FlaskTest/uwsgi.ini
    directory=/home/flask/FlaskTest
    autostart=true
    startretries=3
    user=flask
    
  • 执行supervisord -c /home/flask/supervisor/supervisord.conf使用指定配置文件启动
  • 执行supervisorctl -c /home/flask/supervisor/supervisord.conf进入控制台,可查看程序状态、更新配置文件等

配置Nginx

  • 进入/etc/nginx/conf.d目录
  • 创建flask.conf文件,并添加如下内容
    server {
        listen 80;
        root /home/flask/FlaskTest;
        server_name localhost;
        location / {
            proxy_set_header x-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            include uwsgi_params;
            uwsgi_pass localhost:8001;
        }
    }
    
  • 执行nginx -s reload重载配置
  • Nginx常用命令
    • nginx — start nginx
    • nginx -s stop — fast shutdown
    • nginx -s quit — graceful shutdown
    • nginx -s reload — reloading the configuration file
    • nginx -s reopen — reopening the log files

测试

在浏览器中输入服务器地址,结果如下


测试结果

你可能感兴趣的:(nginx+uwsgi+supervisor快餐部署)