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

Elastic Beanstalk与SQLite兼容问题:Flask网站部署故障求助

Hey there! Let's figure out why your Flask + SQLite site works locally on Windows but breaks when deployed to Elastic Beanstalk. I’ve debugged this exact scenario dozens of times, so here are the most common fixes to walk through:

1. Fix SQLite Path Mismatches Between Windows and Linux

Windows uses backslashes (\) for file paths, but Elastic Beanstalk runs on Linux instances which use forward slashes (/). Even worse, your app’s working directory on EB might not match what you expect locally.

Instead of hardcoding paths, use Python’s os.path module to generate absolute, OS-agnostic paths for your database. Update your database connection code like this:

import os
from flask import Flask
import sqlite3

app = Flask(__name__)

# Get the absolute path to your app's root directory
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# Build the full path to your SQLite database
DB_PATH = os.path.join(BASE_DIR, "your_database.db")  # Replace with your actual DB filename

def get_db_connection():
    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row  # Makes query results easier to work with
    return conn
2. Fix File Permission Issues

Linux is strict about file permissions, and the user that runs your Flask app on EB (usually webapp) might not have read/write access to your database file or its parent directory.

Quick Fix: Use the /tmp Directory

EB’s /tmp folder is universally writable, so you can move your database there instead. Modify the DB_PATH line to:

DB_PATH = os.path.join("/tmp", "your_database.db")

⚠️ Note: Files in /tmp are deleted when the instance restarts, so if you need persistent data, consider switching to a managed database like RDS (SQLite isn’t designed for production cloud environments anyway).

For Pre-Populated Databases

If you uploaded a pre-filled SQLite file, add a config file to fix permissions automatically:

  1. Create a folder named .ebextensions in your project root
  2. Inside it, make a file called 01-fix-permissions.config with this content:
container_commands:
  01_chmod_db:
    command: "chmod 664 /var/app/current/your_database.db"
  02_chmod_app_dir:
    command: "chmod 775 /var/app/current"
3. Confirm Your Flask Entry Point is Correct

Elastic Beanstalk looks for a Flask app instance named app in a file called application.py by default. Double-check:

  • Your main file is indeed named application.py
  • Inside it, you have app = Flask(__name__) (not my_app or another custom name)

If you need to use a different filename or app instance name, add this to your .ebextensions/02-flask-config.config file:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: your_file.py:your_app_instance_name
4. Check EB Logs for Exact Error Details

"出现异常" is too vague—you need the actual error message to pinpoint the issue. Here’s how to get logs:

  • Go to your Elastic Beanstalk environment dashboard
  • Click Logs in the left sidebar
  • Choose Request Logs > Last 100 Lines (or Full Logs for more detail)
  • Look for lines starting with ERROR—common culprits include sqlite3.OperationalError (path/permission issues) or ImportError (missing dependencies)
5. Verify Your ZIP File Structure

EB expects your app files to be at the root of the ZIP, not nested inside a parent folder. Your ZIP should look like this when unzipped:

application.py
your_database.db
static/
  css/
    bootstrap.min.css
templates/
  welcome_form.html
  results.html

If you zipped a folder containing these files, EB won’t find your application entry point. Re-zip the files directly instead.

6. Make Sure Dependencies Are Listed in requirements.txt

Elastic Beanstalk needs to install your Python dependencies automatically. Create a requirements.txt file in your project root with at minimum:

Flask==2.3.3  # Replace with the exact Flask version you're using locally

If you’re using other libraries (like Flask-WTF for forms), add those too.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:39:33