You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amazon EC2上Mod_wsgi无法加载Flask项目Python模块求助

Flask部署到Amazon EC2 Httpd服务器报错排查与解决

问题描述

将测试Flask项目部署到运行Amazon Linux 2的Amazon EC2实例Httpd(Apache)服务器时,Apache日志持续报错:

目标WSGI脚本'/home/ssm-user/Test_Wan/FlaskApp/lado.wsgi'无法作为Python模块加载,且出现ImportError: No module named server错误。

项目基础信息

  • 目录结构:FlaskApp根目录下包含server.py、lado.wsgi、templates文件夹、static文件夹;templates内有index.html和.htaccess;static内有css文件夹及styles.css。
  • 权限配置:所有文件/目录归属ssm-user:ssm-user,目录权限多为rwxr-xr-x,文件权限多为rw-r--r--。

相关文件内容

lado.wsgi脚本

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

server.py文件

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)

templates/index.html

<!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>

templates/.htaccess

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

/etc/httpd/conf.d/lado.conf

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

问题排查与解决方案

1. 核心错误:WSGI脚本路径配置错误

sys.path.insert(0, '/home/ssm-user/Test_Wan/FlaskApp/server.py') 中添加的是具体文件路径,但sys.path需要的是模块所在的目录,Python会在该目录下查找server.py对应的server模块。

修复后的lado.wsgi:

import sys
# 添加FlaskApp根目录到Python路径
sys.path.insert(0, '/home/ssm-user/Test_Wan/FlaskApp')

from server import app as application

2. Apache配置冗余与冲突修复

  • 移除<Directory>块中重复的AllowOverride配置,避免权限冲突;
  • 不需要为templates设置Alias,Flask会自动从项目根目录的templates文件夹加载模板;
  • 添加静态文件Alias,确保CSS等资源可正常访问;
  • 为WSGIDaemonProcess指定Python路径,强化模块查找能力。

修复后的lado.conf:

<VirtualHost *:80>
        ServerName 3.81.5x.xxx # 替换为你的EC2公网IP
        WSGIDaemonProcess FlaskApp threads=5 python-path=/home/ssm-user/Test_Wan/FlaskApp
        WSGIScriptAlias / /home/ssm-user/Test_Wan/FlaskApp/lado.wsgi

        <Directory /home/ssm-user/Test_Wan/FlaskApp>
                WSGIProcessGroup FlaskApp
                WSGIApplicationGroup %{GLOBAL}
                Require all granted
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        # 配置静态文件访问
        Alias /static /home/ssm-user/Test_Wan/FlaskApp/static
        <Directory /home/ssm-user/Test_Wan/FlaskApp/static>
                Options FollowSymLinks
                Require all granted
        </Directory>

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

3. 其他必要操作

  • 删除templates文件夹中的.htaccess,WSGI已接管所有请求,该重写规则多余且易引发冲突;
  • 确保Apache用户有权限访问项目目录:
    chmod 755 /home/ssm-user/Test_Wan/FlaskApp
    chmod 644 /home/ssm-user/Test_Wan/FlaskApp/*.py
    chmod 755 /home/ssm-user/Test_Wan/FlaskApp/templates
    chmod 644 /home/ssm-user/Test_Wan/FlaskApp/templates/*.html
    
  • 重启Httpd服务使配置生效:
    sudo systemctl restart httpd
    
  • 验证Flask安装在Apache使用的Python环境中:
    sudo pip3 install flask
    

内容的提问来源于stack exchange,提问作者wanicedude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:20:59