I am trying to deploy my Flask app to Heroku but receive these errors:
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
Here is my Procfile
:
web: python website/__init__.py -p $PORT
Here is my project structure: Project structure
Here is my __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()
Can't get what's wrong, Flask boots normally but then heroku crashes. I guess my project structure or Procfile is wrong, I searched my problem but didn't found solution. Help me please.
CodePudding user response:
The Flask app needs to bind programmatically to the Heroku $PORT
port_nr = int(os.environ.get("PORT", 5001))
app.run(port=port_nr, host='0.0.0.0')