在 Apache 服务器上部署烧瓶应用程序的问题

Posted

技术标签:

【中文标题】在 Apache 服务器上部署烧瓶应用程序的问题【英文标题】:Issue deploying flask application on Apache server 【发布时间】:2022-01-06 07:24:53 【问题描述】:

设置:

CentOS
Apache
Python 3.6
flask
flask-cors

部署应用程序时出现此错误:

mod_wsgi (pid=18182): Failed to exec Python script file '/var/www/www.refinance.com.au/refinance/dist/refinance/wsgi.py'.
mod_wsgi (pid=18182): Exception occurred processing WSGI script '/var/www/www.refinance.com.au/refinance/dist/refinance/wsgi.py'.
Traceback (most recent call last):
  File "/var/www/www.refinance.com.au/refinance/dist/refinance/wsgi.py", line 10, in <module>
    import app as application
  File "/var/www/www.refinance.com.au/refinance/dist/refinance/app.py", line 6, in <module>
    from flask_cors import CORS
ModuleNotFoundError: No module named 'flask_cors'

有趣(且令人紧张)的是,我在虚拟环境中安装了 flask_cors,但由于某种原因,我的应用程序找不到它。

我什至尝试直接将文件夹添加到 sys.path 中,但没有成功。

httpd.conf

<VirtualHost 203.98.82.57:80>
    ServerName www.refinance.com.au
    WSGIDaemonProcess refinance user=apache group=apache threads=2
    WSGIScriptAlias / /var/www/www.refinance.com.au/refinance/dist/refinance/wsgi.py
    <Directory /var/www/www.refinance.com.au/dist/>
        Require all granted
    </Directory>
</VirtualHost>

wsgi.py

import sys
import site

site.addsitedir(
    "/var/www/www.refinance.com.au/refinance/dist/refinance/lib/python3.6/site-packages"
)

sys.path.insert(0, "/var/www/www.refinance.com.au/refinance/dist/refinance/")

import app as application

app.py

import json
import os
from flask import Flask, Response, request, send_from_directory
from flask_cors import CORS

import sendemail

app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY", "devel")

cors = CORS(
    app,
    resources=r"*": "origins": "*",
    supports_credentials=True,
)
app.config["CORS_HEADERS"] = "Content-Type"


@app.route("/<path:path>", methods=["GET"])
def static_proxy(path):
    if path.endswith(".js"):
        return send_from_directory("./", path, mimetype="application/javascript")
    return send_from_directory("./", path)


@app.route("/")
def root():
    return send_from_directory("./", "index.html")

我已经在这个问题上浪费了好几个小时,但找不到解决方案。任何帮助表示赞赏。

我的包裹:

【问题讨论】:

【参考方案1】:

通过更新我的 wsgi.py 文件解决

我的路径中缺少“env”目录,并且没有正确导入我的应用程序。

下面是正确的代码。

wsgi.py

import sys
import site

site.addsitedir(
    # Before: "/var/www/www.refinance.com.au/refinance/lib/python3.6/site-packages"
    "/var/www/www.refinance.com.au/refinance/env/lib/python3.6/site-packages"
)

sys.path.insert(0, "/var/www/www.refinance.com.au/refinance/dist/refinance/")

# Before: import app as application
from server import app as application

【讨论】:

以上是关于在 Apache 服务器上部署烧瓶应用程序的问题的主要内容,如果未能解决你的问题,请参考以下文章

在 apache 24 中部署 python 烧瓶应用程序

尝试在 Apache2 上使用 WSGI 部署 Flask 时如何解决导入错误

在 IIS 服务器上部署烧瓶 api 给出错误 0x8007010b 通知:ExecuteRequestHandler

如何在 apache windows 10 中使用烧瓶修复错误 500

将 Next.js 部署到 Apache 服务器

在 Windows 上使用 apache mod_wsgi 运行烧瓶应用程序时的导入冲突