I'm trying to deploy my flask application on Ubuntu 20.04 using Nginx and Gunicorn. It's been working smoothly when I run FLASK_APP=run.py
and then gunicorn -w 9 run=app
. However when I setup the configuration files for gunicorn and a config.py I was only able to access the home page but every other route results in an Internal Sever Error.
I set my log file to /var/log/app_name/app_name.err.log
. The error reported in the logs is raise RuntimeError( RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
.
Here is a copy of my config.py which is inside my application module where routes.py,models.py and forms.py are.
import os
import json
with open('/etc/scheye_config.json') as config_file:
config = json.load(config_file)
class Config:
SECRET_KEY = config.get('SECRET_KEY')
SQL_ALCHEMY_DATABASE_URI = config.get('SQL_ALCHEMY_DATABASE_URI')
MAIL_SERVER = config.get('MAIL_SERVER')
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = config.get('EMAIL_USER')
MAIL_PASSWORD = config.get('APPLICATION_PASS')
As you can see I'm importing the settings from my json.config file whose code is shown below. The location for the json file is /etc/app_name.json.
{
"SECRET_KEY": "mysecretkey",
"SQLALCHEMY_DATABASE_URI": "database_uri",
"EMAIL_USER": "[email protected]",
"APPLICATION_PASS": "application_password",
"MAIL_SERVER": "smtp.servermail.com"
}
In my init.py also has almost the same configurations only that I use environment variables. Here's a copy of the init.py.
import os
from flask import Flask
from sqlalchemy.orm import backref
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager, login_manager
from flask_mail import Mail
from sqlalchemy import create_engine
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
# configuring the database using SQLAlchemy
ENV = 'dev'
if ENV == 'dev':
app.debug=True
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')
else:
app.debug=False
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager= LoginManager(app)
login_manager.login_view='login' #
login_manager.login_message_category='info' #styling the message returned
app.config['MAIL_SERVER'] = os.environ.get('MAIL_SERVER')
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME']= os.environ.get('EMAIL_USER')
app.config['MAIL_PASSWORD']= os.environ.get('APPLICATION_PASS')
#initializing our extension
mail = Mail(app)
engine = create_engine('database_uri')
connection = engine.raw_connection()
cursor = connection.cursor()
from app_name import routes
I have intentionally set my SQLALCHEMY_DATABASE_URI in order to continue testing the app before creating a production database.
I've gone through Configuration Handling in the flask documentation 2.2x and tried possible solutions but to no success. Being quite new to deployment I fear the answer is quite obvious and the source of the error is in the numerous configuration files. If anyone understands what the issue is I would highly appreciate his/her help.
CodePudding user response:
in init.py you do not use your config module
try loading your base config, after app = Flask(__name__)
app.config.from_object('config.Config')
but if you haven't SECRET_KEY in env vars then SECRET_KEY will None again then remove app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
or use it in Config class for SECRET_KEY definition as:
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY', config.get('SECRET_KEY'))