ubuntu14.04 - Flask+Nginx+WSGI 部署报错问题

查看:249
本文介绍了ubuntu14.04 - Flask+Nginx+WSGI 部署报错问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我根据How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 14.04的指导部署我的Flask应用,使用教程中简单的例子是可以部署成功的,但是当把应用的入口文件替换成自己的却不行,并且奇怪的是在virtualenv 环境下直接用

python wsgi.py

却是可以的,而用

uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi

就不行了,错误如下:

    dev@ubuntu:~/tests$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi
    *** Starting uWSGI 2.0.12 (64bit) on [Sat Apr  9 16:11:05 2016] ***
    compiled with version: 4.8.2 on 08 April 2016 16:57:14
    os: Linux-3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014
    nodename: ubuntu
    machine: x86_64
    clock source: unix
    detected number of CPU cores: 4
    current working directory: /home/xiaoyi/tests
    detected binary path: /home/xiaoyi/tests/env/bin/uwsgi
    !!! no internal routing support, rebuild with pcre support !!!
    *** WARNING: you are running uWSGI without its master process manager ***
    your processes number limit is 7733
    your memory page size is 4096 bytes
    detected max file descriptor number: 1024
    lock engine: pthread robust mutexes
    thunder lock: disabled (you can enable it with --thunder-lock)
    uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3
    Python version: 2.7.6 (default, Jun 22 2015, 18:01:27)  [GCC 4.8.2]
    *** Python threads support is disabled. You can enable it with --enable-threads ***
    Python main interpreter initialized at 0xb98500
    your server socket listen backlog is limited to 100 connections
    your mercy for graceful operations on workers is 60 seconds
    mapped 72768 bytes (71 KB) for 1 cores
    *** Operational MODE: single process ***
    Traceback (most recent call last):
      File "./wsgi.py", line 17, in <module>
        app.run(host=os.getenv('IP', '0.0.0.0'), port = int(os.getenv('PORT',5000)))
      File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/flask/app.py", line 772, in run
        run_simple(host, port, self, **options)
      File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 694, in run_simple
        inner()
      File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 656, in inner
        fd=fd)
      File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 550, in make_server
        passthrough_errors, ssl_context, fd=fd)
      File "/home/xiaoyi/tests/env/local/lib/python2.7/site-packages/werkzeug/serving.py", line 464, in __init__
        HTTPServer.__init__(self, (host, int(port)), handler)
      File "/usr/lib/python2.7/SocketServer.py", line 419, in __init__
        self.server_bind()
      File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
        SocketServer.TCPServer.server_bind(self)
      File "/usr/lib/python2.7/SocketServer.py", line 430, in server_bind
        self.socket.bind(self.server_address)
      File "/usr/lib/python2.7/socket.py", line 224, in meth
        return getattr(self._sock,name)(*args)
    socket.error: [Errno 98] Address already in use
    unable to load app 0 (mountpoint='') (callable not found or import error)
    *** no app loaded. going in full dynamic mode ***
    *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI worker 1 (and the only) (pid: 13702, cores: 1)
    ^A--- no python application found, check your startup logs for errors ---
    [pid: 13702|app: -1|req: -1/1] 14.28.139.49 () {34 vars in 644 bytes} [Sat     Apr  9 16:12:50 2016] GET / => generated 21 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 83 bytes (0 switches on core 0)

虽然根据以上错误提示表示为Address已被使用,但并未开启多个占用了该地址和端口,刚开始接触这块不太懂,还请指导下!

解决方案

经过仔细排查终于解决了,先贴下我的入口文件的代码:

#!/usr/bin/env python
# -*-  coding=utf-8 -*-

from application import create_app

__author__ = 'Riky'

app = create_app('idc')

app.run()

当使用uwsgi --socket 0.0.0.0:5000 --protocol=http -w wsgi直接执行的时候,报的错如问题中贴的错误提示一样,地址被占用。但是进程中和端口都找不到占用的对应的应用程序,为啥简单的例子可以呢?我仔细对比了下:

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

很明显因为我在IDE中开发中习惯了,而忽略了入口文件最基础的部分:

#入口在没有以下代码的前提下,使用python run.py 是可以执行的
if __name__ == "__main__":
    app.run(host='0.0.0.0')

这样错误就变成了:

*** Operational MODE: single process ***
unable to load app 0 (mountpoint='') (callable not found or import error)

而出现这个错误是一个比较让人无语的问题,uwsgi只能在入口文件识别application,而无法识别我定义的run。之所以会出现端口占用的情况,是因为app.run()实际上也执行了,但并不是uwsgi要加载的应用application

最终改成一下就可以了:

#!/usr/bin/env python
# -*-  coding=utf-8 -*-

from application import create_app

__author__ = 'Riky'

application = create_app('idc')

if __name__ == "__main__":
    application.run()

这篇关于ubuntu14.04 - Flask+Nginx+WSGI 部署报错问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆