- Flask自带的开发服务器不适合用作生产环境。
不论哪种托管方案,应用安装到生产服务器上之后都要执行一系列的任务,其中包括创建和更新数据库表。如果每次都手动操作会很麻烦,因此编写py程序来做这些工作。
from flask_migrate import upgrade
from app.models import User, Follow, Role, Permission, Post, Comment
from app import create_app, db
app = create_app()
@app.cli.command()
def deploy():
"""Run deployment tasks."""
upgrade()
Role.insert_roles()
User.add_self_follows()
由于命令执行可能出错,所以在实现上面的函数的时候要考虑多次执行的情况,由于本身已经考虑了,所以这里如果deploy执行失败,多次执行不会有影响。
在dev环境下时Werkzeug是直接在shell里输出err_log和在网页显示错误信息。Prod环境不能对外展示错误,否则容易被黑客利用。这时候我们一般是进行错误重定向和写日志。应用启动时会创建logging.Logger的实例并附加在应用实例上通过app.logger访问。同时我们可以让错误日志发送到管理员的邮箱。
class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'data.sqlite')
@classmethod
def init_app(cls, app):
Config.init_app(app)
import logging
from logging.handlers import SMTPHandler
credentials = None
secure = None
if getattr(cls, 'MAIL_USERNAME', None) is not None:
credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
if getattr(cls, 'MAIL_USE_TLS', None):
secure = ()
mail_handler = SMTPHandler(
mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
fromaddr=cls.FLASKY_MAIL_SENDER,
toaddrs=[cls.FLASKY_ADMIN],
subject=cls.FLASKY_MAIL_SUBJECT_PREFIX + ' Application Error',
credentials=credentials,
secure=secure)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
部署方式有三种:
云托管:把应用部署到一台或堕胎虚拟服务器上(如Amazon的EC2)
方法与专用服务器部署一致,只是云服务器是虚拟服务器。
容器把应用隔离在镜像(image)中,包含应用即其全部依赖。
Heroku是一个PaaS平台,使用dyno计算单元衡量用量并据此收费。如:Web dyno、Worker dyno。
heroku create <appname>
git remote show heroku
heroku config:set FLASK_APP=depoly.py
- 配置日志
heroku把应用写入stdout、stderr的输出当作日志,我们需要添加相应的日志处理程序。使用heroku logs
可以查看日志。
import logging
from logging import StreamHandler
file_handler = StreamHandler()
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
- 生成密钥
python -c "import uuid; print(uuid.uuid4().hex
把输出可以当作SECURITY_KEY
heroku config:set SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxx
- heroku安装插件举例:
heroku addons:create heroku-postgresql:hobby-dev
Original: https://blog.csdn.net/qq_33583069/article/details/122019663
Author: 真·skysys
Title: Flask # 部署与生产环境
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/746178/
转载文章受原作者版权保护。转载请注明原作者出处!