V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Pooc
V2EX  ›  Python

[求助] flask 部署, supervisor 配置出现问题,不能正常运行,需要 dalao 们指点迷津,谢谢!

  •  
  •   Pooc · 2018-06-28 03:22:36 +08:00 · 5653 次点击
    这是一个创建于 2348 天前的主题,其中的信息可能已经有所发展或是发生改变。

    [环境] :centos+supervisor+gunicorn+flask

    在 /www/blog 目录下,运行是正常的

    /www/blog/venv/bin/gunicorn -b 0.0.0.0:5000 -w 4 microblog:app
    

    [状态] :添加到 supervisor 配置中却一直重复在 running

    $ supervisorctl status
    microblog      RUNNING    pid 12499, uptime 0:00:02
    

    [配置] 如下:

    [supervisord]
    http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
    logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
    logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
    logfile_backups=10          ; (num of main logfile rotation backups;default 10)
    loglevel=info               ; (logging level;default info; others: debug,warn)
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    nodaemon=false              ; (start in foreground if true;default false)
    minfds=1024                 ; (min. avail startup file descriptors;default 1024)
    minprocs=200                ; (min. avail process descriptors;default 200)
    
    [supervisorctl]
    serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
    
    [program:microblog]
    command=/www/blog/venv/bin/gunicorn -b 0.0.0.0:5000 -w 4 microblog:app
    directory=/www/blog
    user=root
    autostart=true
    autorestart=true
    stopasgroup=true
    killasgroup=true
    
    19 条回复    2018-07-02 18:06:32 +08:00
    ericls
        1
    ericls  
       2018-06-28 03:33:09 +08:00   ❤️ 1
    log?
    defunct9
        2
    defunct9  
       2018-06-28 04:55:18 +08:00 via iPad   ❤️ 3
    开 ssh,让我上去看看
    0xABCD
        3
    0xABCD  
       2018-06-28 08:01:10 +08:00 via Android   ❤️ 1
    日志贴出来
    knktc
        4
    knktc  
       2018-06-28 08:07:01 +08:00 via iPhone   ❤️ 1
    不是和 venv 有关系?
    yylucifer
        5
    yylucifer  
       2018-06-28 09:14:33 +08:00   ❤️ 1
    没 log 我只能等 dalao 来 review 你的配置了..
    summerwar
        6
    summerwar  
       2018-06-28 09:20:19 +08:00   ❤️ 1
    话说 running 不是运行中的意思吗?是网站打不开还是
    tempdban
        7
    tempdban  
       2018-06-28 10:36:54 +08:00 via Android   ❤️ 1
    兄弟你用了 venv 你环境变量却没设置啊
    robinlovemaggie
        8
    robinlovemaggie  
       2018-06-28 11:29:00 +08:00   ❤️ 1
    不用用 root 去跑非系统程序,不用用 root 去跑非系统程序,不用用 root 去跑非系统程序……
    ps: supervisor 官网文档了解一下?
    bolide2005
        9
    bolide2005  
       2018-06-28 11:30:31 +08:00   ❤️ 1
    running 有啥问题啊…… running 是正常运行中
    Pooc
        10
    Pooc  
    OP
       2018-06-29 17:20:33 +08:00
    @ericls @0xABCD @yylucifer /var/tmp 下的日志是空的。。。
    @defunct9 小哥,怎么联系。。。
    @knktc @tempdban 我也觉得是很 venv 有关系,但是我添加了环境变量了,在 venv 在没有进入 venv 的情况下能正常使用。。。在 venv/bin/activate 跟 /etc/profile 都添加了 export FLASK_APP=microblog.py 会不会是这个有误?
    @summerwar @bolide2005 running 几秒钟就重复 start,running,block。。。
    Pooc
        11
    Pooc  
    OP
       2018-06-29 17:22:48 +08:00
    @robinlovemaggie 是的,老哥,的确是不要在 root 下跑非系统程序。。。
    knktc
        12
    knktc  
       2018-06-29 17:42:02 +08:00   ❤️ 1
    @Pooc 这个其实不会执行 active 脚本的,所以我感觉修改 active 脚本没有用处的。
    我感觉不行就是得给 supervisor 的 program 配置中加入 environment=PATH="/www/blog/venv/bin" 这样环境配置试试
    tempdban
        13
    tempdban  
       2018-06-29 17:47:52 +08:00 via Android   ❤️ 1
    program: microblog
    里面可以加环境变量
    加的是 VIRTUAL_ENV
    你仔细看看 venv/bin/activate
    都干了啥
    tempdban
        14
    tempdban  
       2018-06-29 17:49:01 +08:00 via Android   ❤️ 1
    所有的 Python venv 方案都要改 VIRTUAL_ENV 环境变量
    Pooc
        15
    Pooc  
    OP
       2018-06-29 17:49:31 +08:00
    @ericls @0xABCD @yylucifer
    日志找到了。。。原来我看错了,是在 logfile=/var/log/supervisor/supervisord.log

    ```
    2018-06-29 17:07:08,984 CRIT Running without any HTTP authentication checking
    2018-06-29 17:07:08,989 INFO daemonizing the process
    2018-06-29 17:07:08,991 INFO supervisord started with pid 5375
    2018-06-29 17:07:08,995 INFO spawned: 'microblog' with pid 5376
    2018-06-29 17:07:09,866 INFO exited: microblog (exit status 3; not expected)
    2018-06-29 17:07:09,867 INFO received SIGCLD indicating a child quit
    2018-06-29 17:07:10,870 INFO spawned: 'microblog' with pid 5382
    2018-06-29 17:07:11,864 INFO exited: microblog (exit status 3; not expected)
    2018-06-29 17:07:11,865 INFO received SIGCLD indicating a child quit
    2018-06-29 17:07:13,871 INFO spawned: 'microblog' with pid 5389
    2018-06-29 17:07:14,576 INFO exited: microblog (exit status 1; not expected)
    2018-06-29 17:07:14,576 INFO received SIGCLD indicating a child quit
    2018-06-29 17:07:17,581 INFO spawned: 'microblog' with pid 5396
    2018-06-29 17:07:18,413 INFO exited: microblog (exit status 1; not expected)
    2018-06-29 17:07:18,413 INFO received SIGCLD indicating a child quit
    2018-06-29 17:07:19,414 INFO gave up: microblog entered FATAL state, too many start retries too quickly
    2018-06-29 17:33:58,601 CRIT Supervisor running as root (no user in config file)
    2018-06-29 17:33:58,635 INFO localhost:0 - - [29/Jun/2018:09:33:58 +0800] "GET / HTTP/1.0" 200 2204
    2018-06-29 17:35:16,200 CRIT Supervisor running as root (no user in config file)
    2018-06-29 17:35:16,232 INFO localhost:0 - - [29/Jun/2018:09:35:16 +0800] "GET / HTTP/1.0" 200 2204
    2018-06-29 17:37:37,650 CRIT received SIGTERM indicating exit request
    2018-06-29 17:37:58,299 CRIT Supervisor running as root (no user in config file)
    2018-06-29 17:37:58,311 INFO /var/tmp/supervisor.sock:Medusa (V1.1.1.1) started at Fri Jun 29 17:37:58 2018
    Hostname: <unix domain socket>
    Port:/var/tmp/supervisor.sock
    2018-06-29 17:37:58,363 CRIT Running without any HTTP authentication checking
    2018-06-29 17:37:58,365 INFO daemonizing the process
    2018-06-29 17:37:58,366 INFO supervisord started with pid 5702
    2018-06-29 17:37:58,368 INFO spawned: 'microblog' with pid 5703
    2018-06-29 17:37:58,851 INFO exited: microblog (exit status 3; not expected)
    2018-06-29 17:37:58,852 INFO received SIGCLD indicating a child quit
    2018-06-29 17:37:59,855 INFO spawned: 'microblog' with pid 5707
    2018-06-29 17:38:00,681 INFO exited: microblog (exit status 3; not expected)
    2018-06-29 17:38:00,682 INFO received SIGCLD indicating a child quit
    2018-06-29 17:38:02,687 INFO spawned: 'microblog' with pid 5713
    2018-06-29 17:38:03,381 INFO exited: microblog (exit status 3; not expected)
    2018-06-29 17:38:03,381 INFO received SIGCLD indicating a child quit
    2018-06-29 17:38:06,386 INFO spawned: 'microblog' with pid 5720
    2018-06-29 17:38:07,114 INFO exited: microblog (exit status 1; not expected)
    2018-06-29 17:38:07,114 INFO received SIGCLD indicating a child quit
    2018-06-29 17:38:08,115 INFO gave up: microblog entered FATAL state, too many start retries too quickly
    2018-06-29 17:38:53,721 INFO localhost:0 - - [29/Jun/2018:09:38:53 +0800] "POST /RPC2 HTTP/1.0" 200 254
    2018-06-29 17:38:53,724 INFO localhost:0 - - [29/Jun/2018:09:38:53 +0800] "POST /RPC2 HTTP/1.0" 200 1242
    2018-06-29 17:38:56,321 INFO localhost:0 - - [29/Jun/2018:09:38:56 +0800] "POST /RPC2 HTTP/1.0" 200 254
    2018-06-29 17:38:56,325 INFO localhost:0 - - [29/Jun/2018:09:38:56 +0800] "POST /RPC2 HTTP/1.0" 200 1242
    2018-06-29 17:39:00,612 INFO localhost:0 - - [29/Jun/2018:09:39:00 +0800] "POST /RPC2 HTTP/1.0" 200 254
    2018-06-29 17:39:00,614 INFO localhost:0 - - [29/Jun/2018:09:39:00 +0800] "POST /RPC2 HTTP/1.0" 200 1242
    2018-06-29 17:45:58,505 INFO localhost:0 - - [29/Jun/2018:09:45:58 +0800] "POST /RPC2 HTTP/1.0" 200 254
    2018-06-29 17:45:58,509 INFO localhost:0 - - [29/Jun/2018:09:45:58 +0800] "POST /RPC2 HTTP/1.0" 200 1242

    ```
    defunct9
        16
    defunct9  
       2018-06-29 18:08:14 +08:00 via iPhone   ❤️ 1
    微信:defunct
    robinlovemaggie
        17
    robinlovemaggie  
       2018-06-29 23:19:42 +08:00   ❤️ 1
    用 pyenv 隔离一套 python 环境,保证 python,venv,程序目录和 supervisor 在同一用户下,这是算是最理想的部署环境。
    defunct9
        18
    defunct9  
       2018-07-01 21:35:48 +08:00 via iPhone   ❤️ 1
    搞定,mark 一下,走人。
    Pooc
        19
    Pooc  
    OP
       2018-07-02 18:06:32 +08:00
    @defunct9 多谢老哥帮助!!!
    其实是跟 @robinlovemaggie 老哥你的想法一致,也跟上面的各位 dalao 想法一致的。
    我把 @defunct9 老哥解决方法说一下,以便后面的同学能及时处理这个问题。

    @defunct9 原话:
    原因是 venv 跟 gunicorn 必须是同一个 shell 引导,否则无法继承环境变量。

    解决方法:
    ```
    # cat /etc/supervisord.conf
    [supervisord]
    http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
    logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
    logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
    logfile_backups=10 ; (num of main logfile rotation backups;default 10)
    loglevel=info ; (logging level;default info; others: debug,warn)
    pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
    nodaemon=false ; (start in foreground if true;default false)
    minfds=1024 ; (min. avail startup file descriptors;default 1024)
    minprocs=200 ; (min. avail process descriptors;default 200)

    [supervisorctl]
    serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket

    [program:microblog]
    command=/www/blog/venv/bin/gunicorn_start
    directory=/www/blog
    user=root
    autostart=true
    autorestart=true
    stopasgroup=true
    killasgrouv=true
    stdout_logfile=/www/blog/logs/microblog.log
    redirect_stderr=true


    # cat /www/blog/venv/bin/gunicorn_start
    #!/bin/sh

    export FLASK_APP=microblog.py

    cd /www/blog/
    source venv/bin/activate
    /www/blog/venv/bin/gunicorn -b localhost:3000 -w 4 microblog:app

    ```
    直接通过一个 sh 脚本处理同一个 shell 引导,
    这个时候由 gunicorn 启动的 microblog 应用服务器现在运行在本地端口 3000,
    然后我再设置一下 Nginx 就可以将应用程序暴露给外部世界。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   958 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 20:13 · PVG 04:13 · LAX 12:13 · JFK 15:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.