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

Issue deploying flask application on Apache server

设置:

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")

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

我的包裹:

通过更新我的 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