Issues while deploying flask app using apache2

16 Views Asked by At

I need to deploy a flask app using apache2 and wsgi but I can't figure out how to do it. I have seen many tutorials and blogs on how to do it but they all use a demo flask app and I have built my app using flask application factory. I cant find any material for my specific usecase.

Here is how the app directory structure looks like:

mysite/
└── app
    ├── app (main app folder)
    │   ├── app (app source code)
    │   │   ├── blueprint1 (blueprint number 1)
    │   │   │   ├── __init__.py
    │   │   │   └── routes.py (routes for blueprint1)
    │   │   ├── blueprint2 (blueprint number 2)
    │   │   │   ├── __init__.py
    │   │   │   └── routes.py (routes for blueprint2)
    │   │   ├── entensions.py (flask extensions)
    │   │   ├── __init__.py (app init file)
    │   │   ├── models (database models)
    │   │   │   ├── model1.py
    │   │   │   └── model2.py
    │   │   ├── static (static dir)
    │   │   │   ├── css
    │   │   │   │   └── main.css
    │   │   │   ├── img
    │   │   │   │   └── main.png
    │   │   │   └── js
    │   │   │       └── main.js
    │   │   └── templates (html templates)
    │   │       ├── base.html
    │   │       └── main.html
    │   ├── config.py (config file)
    │   └── database.db (sqlite database)
    └── wsgi.py (wsgi script)

Below are the file contents of all files:

  1. __init__.py (app init file):
from flask import Flask, render_template

from config import Config
from app.extensions import db

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    # flask extensions
    db.init_app(app)
    
    # blueprints
    from app.blueprint1 import bp as bp1
    app.register_blueprint(bp1, url_prefix='/bp1')

    from app.blueprint2 import bp as bp2
    app.register_blueprint(bp2, url_prefix='/bp2')
  1. __init__.py (blueprint init file)
from flask import Blueprint

bp = Blueprint('blueprint1', __name__)

from app.blueprint1 import routes
  1. routes.py (blueprint routes file)
from app.blueprint1 import bp
from flask import render_template

@bp.route('/')
def index():
    return render_tamplate('main.html`)
  1. config.py (app config file)
import os

basedir = os.path.abspath(os.path.dirname(__file__))

class Config:
    # general config
    # SECRET_KEY = os.environ.get('SECRET_KEY')
    # database config
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'database.db')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
  1. /etc/apache2/sites-available/mysite.conf (apache https conf file)
<VirtualHost *:443>
  ServerName domain.name

  WSGIDaemonProcess app
  WSGIScriptAlias / /var/www/mysite/app/wsgi.py
  <Directory /var/www/mysite/app/app/>
    WSGIProcessGroup app
    WSGIApplicationGroup %{GLOBAL}
    Require all granted
  </Directory>
  SSLEngine on
  SSLCertificateFile /etc/letsencrypt/live/domain.name/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/domain.name/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

With different wsgi files I was getting different errors:

  • ModuleNotFoundError: No module named 'config'
  • TypeError: 'module' object is not callable

These 2 are main.


What should be the wsgi file look like and what all changes are recommended.

0

There are 0 best solutions below