我正在嘗試將我的 Flask 應用程式部署到 Heroku,但收到以下錯誤:
2022-05-10T12:13:10.776664 00:00 heroku[web.1]: State changed from crashed to starting
2022-05-10T12:13:15.072537 00:00 heroku[web.1]: Starting process with command `python website/__init__.py -p 31242`
2022-05-10T12:13:19.049387 00:00 app[web.1]: * Serving Flask app '__init__' (lazy loading)
2022-05-10T12:13:19.049410 00:00 app[web.1]: * Environment: production
2022-05-10T12:13:19.049443 00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.
2022-05-10T12:13:19.049473 00:00 app[web.1]: Use a production WSGI server instead.
2022-05-10T12:13:19.049493 00:00 app[web.1]: * Debug mode: off
2022-05-10T12:13:19.060373 00:00 app[web.1]: * Running on http://127.0.0.1:5000 (Press CTRL C to quit)
2022-05-10T12:13:20.000000 00:00 app[api]: Build succeeded
2022-05-10T12:14:15.405462 00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-05-10T12:14:15.556488 00:00 heroku[web.1]: Stopping process with SIGKILL
2022-05-10T12:14:15.802247 00:00 heroku[web.1]: Process exited with status 137
2022-05-10T12:14:16.000898 00:00 heroku[web.1]: State changed from starting to crashed
這是我的Procfile:
web: python website/__init__.py -p $PORT
這是我的專案結構: 專案結構
這是我的__init__.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object("website.config")
login_manager = LoginManager(app)
login_manager.init_app(app)
login_manager.login_view = 'login'
db = SQLAlchemy()
migrate = Migrate(app, db)
db.init_app(app)
from website.blueprints.main import main as main_blueprint
app.register_blueprint(main_blueprint)
from website.blueprints.auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
from website.blueprints.shop import shop as shop_blueprint
app.register_blueprint(shop_blueprint)
from website.blueprints.admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint)
import website.models
import website.forms
import website.utils
db.create_all(app=app)
if __name__ == "__main__":
app.run()
不知道出了什么問題,Flask 正常啟動,但隨后 heroku 崩潰了。我猜我的專案結構或 Procfile 錯誤,我搜索了我的問題但沒有找到解決方案。請幫幫我。
uj5u.com熱心網友回復:
Flask 應用程式需要以編程方式系結到 Heroku$PORT
port_nr = int(os.environ.get("PORT", 5001))
app.run(port=port_nr, host='0.0.0.0')
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/472678.html
