zoukankan      html  css  js  c++  java
  • Flask: Nginx + uWSGI + Supervisor 服务器部署

    环境说明

    • OS: debian 4.9.18-1 (2017-03-30) i686
    • Python: python 3.5.3
    • venv: pyvenv-3.5
    • Nginx version: nginx/1.10.3
    • uWSGI: 2.0.14-debian
    • Supervisor: 3.3.1-1

    项目目录

    aircleaner
    ├── app
    │   ├── api
    │   ├── api_1_0
    │   ├── main
    │   ├── setting
    │   ├── static
    │   ├── templates
    │   └── users
    ├── migrations
    ├── tests
    ├── tools
    ├── uWSGI
    └── venv
        ├── bin
        ├── include
        ├── lib
        └── share
    

    开始

    1. 安装所需软件

    全部默认用 root 安装

    $ apt install build-essential python-dev
    $ apt install virtualenv
    $ apt install nginx
    $ apt install uwsgi-plugin-python3
    $ apt install supervisor
    $ pip install uwsgi
    

    2. 配置 Nginx

    进入 /etc/nginx/sites-enabled/ 文件夹 特别注意不是 sites-available,也可以在 sites-available 文件夹下创建配置文件,再软连接到 site-enabled 文件夹下,在 /etc/nginx/conf.d/ 中也软连接一份

    备份默认配置文件 default

    $ cd /etc/nginx/sites-enabled
    $ mv default ../sites_enabled_default.bak
    

    写入配置文件:

    server {
    	listen	80;
    	server_name	localhost;
    	charset	utf-8;
    	client_max_body_size	75M;
    
    	location / {
    		try_files $uri @aircleaner;
    	}
    
    	location @aircleaner{
    		include uwsgi_params;
    		uwsgi_pass unix:/root/aircleaner/uWSGI/uwsgi.sock; #指定socket文件
    		#uwsgi_pass 127.0.0.1:8080; #指定url + 地址
    	}
    	
    	location /static {
    		alias /root/aircleaner/app/static; #指定静态文件地址
    	}
    }
    

    修改好配置之后测试配置文件

    $ nginx -t
    

    重启 Nginx

    $ /etc/init.d/nginx restart
    

    3. 配置 uWSGI

    在项目文件 uwsgi 文件夹下 新建配置文件 uwsgi.ini (支持 xml, ini, json)

    在配置文件中写下以下内容:

    [uwsgi]
    #application's base folder
    #base = /root/aircleaner
    chdir = /root/aircleaner #指定项目的目录(关键)
    
    #plugin
    plugin = python35 #调用python35 插件(关键)
    
    #misc
    master = True
    py-autoreload = True
    enable-threads = True
    
    #python module to import
    app = app 
    wsgi-file = /root/aircleaner/manage.py  #指定到运行的py文件上(关键)
    
    virtualenv = /root/aircleaner/venv #指定虚拟环境目录(关键)
    
    #socket file's location
    socket = /root/aircleaner/uWSGI/uwsgi.sock #将socket输出到文件
    #socket = 127.0.0.1:8080 #也可以输出到地址+端口
    
    #permissions for the socket file
    chmod-socket = 666  #改变权限
    
    #the variable that holds a flask application inside the module imported at line #6
    callable = app #调用 manage.py 中的 app
    
    #location of log files
    #logto = /root/aircleaner/uWSGI/%n.log #输出日志到文件
    
    

    查看可用的 uWSGI 插件

    $ update-alternatives --list uwsgi
    
    

    启动 uWSGI

    $ uwsgi uwsgi.ini
    

    4. 进程监控

    使用 supervisor

    • supervisord 为服务端
    • supervisorctl 为客户端,用来启动某一进程

    对默认配置文件备份

    $ echo_supervisord_config > /etc/supervisor/supervisord.conf.default
    

    修改 /etc/supervisor/supervisord.conf ,在最下面加入监控程序的配置

    [program:aircleaner]
    command=/usr/bin/uwsgi /root/aircleaner/uWSGI/uwsgi.ini  # 跟手动启动的命令一样
    stopsignal=QUIT
    autostart=true
    autorestart=true
    user=root
    stdout_logfile=/root/aircleaner/uWSGI/supervisor.log      # 运行日志
    stderr_logfile=/root/aircleaner/uWSGI/supervisor_err.log  # 错误日志
    

    重启 supervisord

    $ supervisorctl reload
    

    启动 aircleaner 进程

    $ supervisorctl start aircleaner
    

    排错

    $ supervisorctl status aircleaner
    

    使用以上命令可以返回我们进程的状态信息

    测试:使用 killall 杀死 uwsgi 进程, 然后再查找

    $ killall uwsgi 
    $ ps aux | grep uwsgi
    

    发现 uwsgi 自己重新启动了,目的达成

    参考

    https://www.oschina.net/translate/serving-flask-with-nginx-on-ubuntu
    http://www.linuxidc.com/Linux/2016-07/133064.html
    http://www.jianshu.com/p/1278c7ee0cc7
    http://developer.51cto.com/art/201010/229615.htm
    http://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

  • 相关阅读:
    ORACLE表空间操作(转)
    ORA12514错误解决方法
    ETL工具 kettle问题
    改变oracle端口号
    查询SQL Server中所有数据库的数据文件位置
    使用URTracker构建企业IT服务平台
    那些年,我做共享软件(2)
    那些年,我做共享软件(1)
    那些年,我做共享软件(3)完结
    浅析缺陷管理系统URTracker
  • 原文地址:https://www.cnblogs.com/ash975/p/15209428.html
Copyright © 2011-2022 走看看