首頁  >  問答  >  主體

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已被使用,但並未開啟多個佔用了該地址和端口,剛開始接觸這塊不太懂,還請指導下!

阿神阿神2713 天前816

全部回覆(3)我來回復

  • 某草草

    某草草2017-05-16 17:19:52

    經過仔細排查終於解決了,先貼下我的入口文件的代碼:

    #!/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()

    回覆
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 17:19:52

    uwsgi有一個配置文件,裡面可以定義訪問的地址和端口號,我覺得這 socket.error: [Errno 98] Address already in use 句報錯可能是你沒有配置uwsgi的原因。 uwsgi的配置以及flask專案的部署過程可以參考一下我寫的文章,嘿嘿。
    /a/1190000004294...

    回覆
    0
  • 黄舟

    黄舟2017-05-16 17:19:52

    netstat -ntlp查看埠是不是被uwsgi佔用,是的話ps -ef | grep uwsgi 找到pid,然後kill

    回覆
    0
  • 取消回覆