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:
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
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:
- Create a folder named
.ebextensionsin your project root - Inside it, make a file called
01-fix-permissions.configwith 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"
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__)(notmy_appor 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
"出现异常" 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 includesqlite3.OperationalError(path/permission issues) orImportError(missing dependencies)
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.
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

