pythonapacheflaskamazon-ec2mod-wsgi

Mod_wsgi Cannot Load my Python Server module on Amazon Ec2


Good day everyone, I am trying to deploy a test Flask project onto an Httpd (Apache) server running an Amazon Linux 2(centos rhel fedora) on Amazon Ec2. From My Apache Log I keep getting"

Target WSGI script '/home/ssm-user/Test_Wan/FlaskApp/lado.wsgi' cannot be loaded as Python module." and ImportError: No module named server

This is the error message I get on my Httpd server homepage

enter image description here

Here is the tree of my FlaskApp Directory

Flask Appp Tree View

This is the files in my flask app and the permisions of file and folders in it

Flask App file permissions

This is my lado.wsgi script

import sys
sys.path.insert(0, '/home/ssm- 
user/Test_Wan/FlaskApp/server.py')

from server import app as application

if __name__ == "__main__":
    application.run()

This is my server.py file

from flask import Flask,render_template

app = Flask(__name__)

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

if __name__ == "__main__":
    app.run(debug=True)

This is the files in my templates folder located in my FlaskAPP directory and there permissions

Permssions of files in tree directory

This is my HTML file Located in my templates Folder inside my FlaskApp:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
    <link rel="stylesheet" href="../static/css/styles.css">
</head>
<body>
<div class="wrapper">
    <div class="top">
        <div class="title"><h1>My Blog</h1></div>
    </div>
    <div class="content">
        <div class="card">
            <h2>Test Title</h2>
            <p class="text">Test Subtitle</p>
        </div>
    </div>
</div>
</body>
<footer>
    <p>Made with ♥️ in Lagos.</p>
</footer>
</html>

This is the .htaccess file in my templates folder located in my FlaskApp directory

RewriteEngine On
RewriteBase /
RewriteRule ^(.*\.html)$ /home/ssm-user/Test_Wan/FlaskApp/lado.wsgi/$1

This is my lado.conf file located in my /etc/http/conf.d folder

<VirtualHost *:80>
        ServerName 3.81.5x.xxx #(public IP of my EC2 instance)
        DocumentRoot /home/ssm-user/Test_Wan/FlaskApp

        WSGIDaemonProcess FlaskApp threads=5
        WSGIScriptAlias / /home/ssm-user/Test_Wan/FlaskApp/lado.wsgi

        <Directory /home/ssm-user/Test_Wan/FlaskApp>
                WSGIProcessGroup FlaskApp
                WSGIApplicationGroup %{GLOBAL}
                Require all granted
                AllowOverride All
                DirectoryIndex index.html

                # Additional settings for static files
                Options FollowSymLinks
                AllowOverride None
                Require all granted

        </Directory>

        Alias /templates /home/ssm-user/Test_Wan/FlaskApp/templates
        <Directory /home/ssm-user/Test_Wan/FlaskApp/templates>
                Options FollowSymLinks
                AllowOverride None
                Require all granted
        </Directory>

        ErrorLog /var/log/httpd/error.log
        CustomLog /var/log/httpd/access.log combined
</VirtualHost>

Solution

  • Try removing server.py from sys.path and retry. sys.path should be a directory.