nginx + gunicorn + supervisor + flask+virtualenvwrapper

Posted TNTsc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx + gunicorn + supervisor + flask+virtualenvwrapper相关的知识,希望对你有一定的参考价值。

1.一个简单的flask项目

  hello.py 文件

from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
    return "hello"
if __name__ == \'__main__\':
    app.run(host=\'0.0.0.0\', port=8000, debug=True)

 

2. 虚拟环境使用

 https://www.cnblogs.com/sc-1067178406/p/10624075.html 

3. gunicorn 配置

pip3 install gunicorn

  

①添加 gunicorn.py 文件

 

 

 

gunicorn.py 文件

# -*- coding: utf-8 -*-
import os
import gevent.monkey
gevent.monkey.patch_all()
import multiprocessing
debug = True
loglevel = \'debug\'
bind = \'0.0.0.0:8000\'
log_dir = os.path.join(os.path.dirname(__file__), \'log\')
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
p = os.path.join(log_dir, \'gunicorn.pid\')
j = os.path.join(log_dir, \'debug.log\')
a_ = os.path.join(log_dir, \'access_log.log\')
e_ = os.path.join(log_dir, \'error_log.log\')

accesslog = a_
errorlog = e_
pidfile = p
logfile = j
#启动的进程数
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = \'gunicorn.workers.ggevent.GeventWorker\'
x_forwarded_for_header = \'X-XUBOGZF-FOR\'
print("项目启动")

 

② 测试启动

gunicorn -w 4 -b 0.0.0.0:5000 hello:app

 

4. 安装 supervisor

https://www.cnblogs.com/sc-1067178406/p/10645966.html#_label2

 ①添加配置

(加在  supervisor.conf  文件最后 )

[program:housing]
directory=/html/dump
command=/root/Envs/mycrm/bin/gunicorn -c /html/dump/gunicorn.py hello:app
autorestart=true
user=root
redirect_stderr=true          ; redirect proc stderr to stdout (default false)
stdout_logfile = /var/log/supervior/crm.log
stderr_logfile = /var/log/supervior/crm-err.log
stdout_logfile_maxbytes=50MB   ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)

②web的管理界面(打开  supervisor.conf 中的注释)

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
username=user              ; should be same as http_username if set
password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

 

③ 启动

 启动   supervisord -c  /etc/supervisor.conf


  status housing #查看状态
    start housing #启动任务
    stop  housing #停止任务
    
    start all  
    stop  all  #启停所有的任务

 

5.nginx 配置

①安装

https://www.cnblogs.com/sc-1067178406/p/10639740.html

② .conf  文件 配置

server {
    listen  9999;
    server_name _;
   #服务
    location / {
            client_max_body_size 100m;
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://0.0.0.0:8000;
      }

      location /status  {
        stub_status on;
    }
}

 

 

参考文章 : https://www.jianshu.com/p/be9dd421fb8d

以上是关于nginx + gunicorn + supervisor + flask+virtualenvwrapper的主要内容,如果未能解决你的问题,请参考以下文章

gunicorn + nginx:通过套接字或代理服务器?

使用 Gunicorn + Nginx + Flask 有啥好处? [复制]

Nginx Django 和 Gunicorn。 Gunicorn 袜子文件丢失?

Django-Gunicorn-Nginx 部署没有通过 Nginx

django-gunicorn-nginx:502 网关错误

Django/gunicorn/nginx: 403 禁止