Home > Net >  Issue deploying flask application on Apache server
Issue deploying flask application on Apache server

Time:12-07

Setup:

CentOS
Apache
Python 3.6
flask
flask-cors

I'm getting this error when deploying my application:

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'

The funny (and stressing) thing is that I have flask_cors installed inside my virtual environment but for some reason my application can't find it.

I even tried to add the folder to the sys.path directly but didn't work.

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

I wasted quite a few hours on this problem already but can't find a solution. Any help is appreciated.

My packages: enter image description here

CodePudding user response:

Solved by updating my wsgi.py file

I was missing the "env" directory in my path and not importing my app correctly.

Below is the correct code.

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
  • Related