Flask + Gunicorn + NGINX 问题 运行 gunicorn

Flask + Gunicorn + NGINX problem running with gunicorn

我对 python 没有太多经验,这是我第一次手动上传内容(使用 .conf 文件)。我有一个用 Flask 制作的应用程序,我想将它上传到服务器上。我使用了 gunicorn 和 nginx。 Nginx 抛出一些错误,发现应用不是 运行 gunicorn(所以我还没有调试 nginx)。使用 flask run --host=0.0.0.0 命令和 python app.py 命令,该应用程序 运行 运行良好。我认为该错误与我在 python 中导入文件的方式有关,但我不确定。错误日志是这样的:

[2022-05-16 15:40:39 +0200] [127342] [INFO] Starting gunicorn 20.1.0
[2022-05-16 15:40:39 +0200] [127342] [INFO] Listening at: http://0.0.0.0:8003 (127342)
[2022-05-16 15:40:39 +0200] [127342] [INFO] Using worker: sync
[2022-05-16 15:40:39 +0200] [127343] [INFO] Booting worker with pid: 127343
[2022-05-16 15:41:09 +0200] [127342] [CRITICAL] WORKER TIMEOUT (pid:127343)
[2022-05-16 15:41:09 +0200] [127343] [INFO] Worker exiting (pid: 127343)
[2022-05-16 15:41:10 +0200] [127350] [INFO] Booting worker with pid: 127350
[2022-05-16 15:41:24 +0200] [127350] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/var/www/FlaskApp/AppName/venv/lib/python3.10/site-packages/gunicorn/util.py", line 359, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/www/FlaskApp/wsgi.py", line 14, in <module>
    from AppName import app
  File "/var/www/FlaskApp/AppName/app.py", line 19, in <module>
    @app.route('/')
AttributeError: 'NoneType' object has no attribute 'route'
[2022-05-16 15:41:24 +0200] [127350] [INFO] Worker exiting (pid: 127350)
[2022-05-16 15:41:25 +0200] [127342] [INFO] Shutting down: Master
[2022-05-16 15:41:25 +0200] [127342] [INFO] Reason: Worker failed to boot.

它无法从 app.py:

中读取 @app.route('/')
-AttributeError: 'NoneType' object has no attribute 'route'

我项目的文件结构是:

---AppName
------app.py
------_init_.py (same as app.py)
------main.py (same as app.py)
------file1.py
------file2.py
------folder1
------static
------templates
------folder2
------venv
---wsgi.py
---make-image.sh
---requirements.txt
---gunicorn_starter.sh
---appname.wsgi (I thought maybe the naming needed to be the same with the name of the app)

我添加了一个 shell 脚本文件来启动它 gunicorn_starter.sh:

#!/bin/sh

gunicorn wsgi:AppName -w 1 --threads 1 -b 0.0.0.0:8003

注意:我使用了 2 个 worker 2 个线程,然后是 3 个,然后我认为为了测试我将只需要一个。不知道会不会影响结果。

app.py:

from flask import Flask, request, render_template
from file1 import function

import spacy
from spacy import displacy
nlp = spacy.load("el_core_news_sm")
from flaskext.markdown import Extension,Markdown

def create_app():
        #Init App
        app = Flask(__name__)
        Markdown(app)

app = create_app()

@app.route('/')
def home():
    return render_template('index.html')


@app.route('/submit', methods=['POST'])
def submit():
        variale1 = function(var)
        ....some code...
        return something

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

wsgi.py:

import os
import sys

fpath = os.path.join(os.path.dirname(__file__),'AppName')
sys.path.append(fpath)
#print("||the system path||",sys.path)

import file2
import file1
import AppName
from AppName import app

if __name__ == "__main__":
#    app = create_app()
    app.run()

NGINX:

我 运行 它在 /etc/systemd/system/AppName.service .

有一个服务

AppName.service:

[Unit]
Description=Gunicorn instance to serve AppName
After=network.target

[Service]
User=user1
Group=www-data
WorkingDirectory=/var/www/FlaskApp/AppName
Environment="PATH=/var/www/FlaskApp/AppName/venv/bin"
ExecStart=/var/www/FlaskApp/AppName/venv/bin/gunicorn -w 2 --bind unix:AppName.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

此处 nginx 抛出无法连接到套接字的错误,这是因为 gunicorn 不是 运行ning。

此服务与 /etc/nginx/sites-available/AppName.conf 一起使用。

AppName.conf:

server {
    listen 80;
    server_name domainname.com www.domainname.com;

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/FlaskApp/AppName/AppName.sock;
    }
}

然后我使用命令:

sudo systemctl start AppName
sudo systemctl enable AppName
sudo systemctl status AppName

并在启用站点中添加符号 link。 那我直接启动nginx。

我看到一个post说要使用函数装饰器: BT 但我对 python 了解不多,所以我对此感到很困惑。我什至不确定这是错误还是我拥有 init.py 文件的方式。

我遵循了这些指南:

https://www.linkedin.com/pulse/building-deploying-production-ready-flask-app-anand-iyer

https://gunicorn.org/#deployment

https://docs.gunicorn.org/en/stable/deploy.html

https://towardsdatascience.com/understanding-python-imports-init-py-and-pythonpath-once-and-for-all-4c5249ab6355

https://www.geeksforgeeks.org/decorators-in-python/

我使用的主要指南是这样的:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-18-04

我用于调试的链接:






加入函数create_app return语句。在函数的末尾放置 return app.

没有它returns None,所以预计python不知道什么是app.route