You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technologies/flask/production-checklist.mdx
+372-2Lines changed: 372 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,12 +10,382 @@ import SubHeading from "@site/src/components/SubHeading";
10
10
11
11
## Introduction
12
12
13
-
ToDO
13
+
Flask is an open-source Python microframework used for building web applications. Emphasize its simplicity, flexibility, and wide adoption within the Python community.
14
+
15
+
Deployment is the process of making your Flask application accessible to users on a live server ensuring its stability, security, and optimal performance.
16
+
17
+
For this reference guide, we will be covering some things to be done before deploying your application to ensure the application is both safe and the deployment employ best practices
18
+
19
+
We will be making reference to [Flask Berry dashboard](https://github.com/app-generator/flask-berry-dashboard) for this tutorial.
- Create a file named `.env` in the root folder of your project, this file will hold environmental variables and their values.
54
+
55
+
```bash
56
+
# .env
57
+
FLASK_APP=run.py # application entry point
58
+
SECRET_KEY=<your-secret-key>
59
+
DB_ENGINE=<database-to-be-used>
60
+
DB_HOST=<db-host-name-or-ip>
61
+
DB_NAME=<database-name>
62
+
DEBUG=False
63
+
```
64
+
`SECRET_KEY` is a random string that is used to protect your Flask application from attack. It is used to encrypt cookies and other sensitive data. If an attacker can guess your secret key, they could potentially steal your users' session data or other sensitive information.
65
+
66
+
Using the `python-dotenv` package, the contents of the `.env` file are loaded into environmental variables when the application starts.
67
+
68
+
### Enabling database migration for application
69
+
Migrations are a way to track changes to your database schema over time. This makes it easy to roll back changes if something goes wrong or to deploy your application to a new environment.
70
+
`Flask-Migrate` is a Flask extension that provides support for database migrations.
- In the entry point of your application (`run.py`) add the following line of code
78
+
```py
79
+
# run.py
80
+
...
81
+
from flask_migrate import Migrate
82
+
...
83
+
# after creating Flask and SQLAlchemy objects
84
+
Migrate(app, db)
85
+
...
86
+
```
87
+
88
+
- Next step is to create the migration directory and apply the migration to the current database schema
89
+
90
+
```bash
91
+
(venv) flask-berry-dashboard$ flask db init
92
+
(venv) flask-berry-dashboard$ flask db migrate --message "initial db migration"
93
+
```
94
+
95
+
By setting up `flask-migrate` you can be able to move scale your application database and alter its schema without losing data.
96
+
97
+
### Minifying application files using `Flask-Minify`
98
+
`Flask-Minify` is a Flask extension that can be used to minify HTML, CSS, and JavaScript files. Minifying files can help to improve the performance of your Flask application by reducing the size of the files that need to be downloaded by the user.
99
+
100
+
- Install flask-minify from the terminal with the command
Test the minification of your files to make sure that it does not break your application. Minification can sometimes introduce errors in your files, so it is important to test them thoroughly before you deploy your application.
116
+
117
+
### Increasing Application security using `Flask-Talisman`
118
+
119
+
`Flask-Talisman` is a Flask extension that helps to secure your Flask application by setting HTTP security headers. These headers can help to protect your application from a variety of common web attacks, such as cross-site scripting (XSS), clickjacking, and session hijacking.
- Setup flask-talisman by adding this line of code in `run.py`
127
+
```py
128
+
# run.py
129
+
...
130
+
from flask_talisman import Talisman
131
+
...
132
+
talisman = Talisman(app)
133
+
...
134
+
```
135
+
With this, we have added a new layer of security to our application in a production environment.
136
+
137
+
### Serve static files using CDN
138
+
A content delivery network (CDN) is a network of servers that are distributed around the world. CDNs can be used to improve the performance of web applications by delivering static content, such as images and JavaScript files, from the server that is closest to the user.
139
+
140
+
To deploy a Flask application using a CDN, you will need to:
141
+
142
+
- Choose a CDN provider. There are many CDN providers available, such as Amazon CloudFront, Cloudflare, and Akamai.
143
+
144
+
- Create an account with the CDN provider.
145
+
146
+
- Upload your static content to the CDN provider in a compressed format. This will reduce the size of the files and improve the performance of your application.
147
+
148
+
- To configure your Flask application to use the CDN, add the following to the `.env` file
149
+
```
150
+
# .env
151
+
# CDN Support
152
+
# Note: Used only when DEBUG=False (production mode)
- Adding `flask-cdn` to the flask application, `apps/__init__.py` contain a function for creating a flask object, that is where we will be initializing our CDN
162
+
```py
163
+
# apps/__init__.py
164
+
...
165
+
from flask_cdn importCDN
166
+
167
+
cdn = CDN()
168
+
...
169
+
170
+
defcreate_app():
171
+
...
172
+
ifnotDEBUGand'CDN_DOMAIN'in app.config:
173
+
cdn.init_app(app)
174
+
175
+
return app
176
+
```
177
+
178
+
- Add the CDN domain to your python flask configuration.
With this done, our application is ready to serve static files over a CDN.
199
+
200
+
## Deploying Flask Application
201
+
Deploying a Flask application is a crucial step in turning your Python web application into a fully functional and accessible website. It involves the process of making your Flask application available to users on a live server, ensuring its stability, security, and optimal performance. Without proper deployment, your Flask application may not reach its intended audience or function as expected.
202
+
203
+
Deploying a Flask application goes beyond simply running it on your local development environment. It requires careful planning, configuration, and implementation of various steps to ensure a successful deployment.
204
+
205
+
Before you start the process of deploying your application, execute the command below on your terminal to create an updated `requirements.txt` file
Docker is a popular containerization platform that allows you to package and distribute applications along with their dependencies in a portable and isolated environment. It provides a consistent and reproducible way to build, ship, and run applications, making it easier to deploy applications across different environments without worrying about compatibility issues.
212
+
213
+
For this tutorial, we will be using `Nginx` as the reverse proxy server and `Gunicorn to serve the application.
214
+
215
+
A reverse proxy is a server that sits between the internet and a web application. It can be used to improve the performance, security, and reliability of the web application. Gunicorn is an application server that can be used to run web applications. It is designed to be used with a reverse proxy. If Gunicorn is exposed directly to the internet, it can be vulnerable to denial-of-service (DoS) attacks. A DoS attack is an attempt to make a computer or network resource unavailable to its intended users. In the case of Gunicorn, a DoS attack could be performed by creating a load that trickles data to the servers. This type of attack is known as a Slowloris attack.
216
+
217
+
We will start by configuring our docker container.
218
+
219
+
- Create a file `docker-compose.yml` in the root folder of your project
220
+
```yml
221
+
# docker-compose.yml
222
+
version: '1.0'# the current version of your application
223
+
services:
224
+
appseed-app: # can be given any name
225
+
container_name: appseed_app
226
+
restart: always
227
+
build: .
228
+
networks:
229
+
- db_network
230
+
- web_network
231
+
nginx:
232
+
container_name: nginx
233
+
restart: always
234
+
image: "nginx:latest"
235
+
ports:
236
+
- "5085:5085"
237
+
volumes:
238
+
- ./nginx:/etc/nginx/conf.d # mapping nginx configuration to a configuration file we will be creating
239
+
networks:
240
+
- web_network
241
+
depends_on:
242
+
- appseed-app
243
+
networks:
244
+
db_network:
245
+
driver: bridge
246
+
web_network:
247
+
driver: bridge
248
+
```
249
+
250
+
- Create a Nginx configuration file named `appseed-app.conf` in a folder `nginx` in the root of your project as seen under "volumes" from the docker-compose.yml file.
- Execute the command to build the docker image and deploy the application on docker.
317
+
```bash
318
+
(venv) flask-berry-dashboard$ docker-compose up --build
319
+
```
320
+
Now your application is running on docker. If you are running the application on your machine, visit [`http://localhost:5085`](http://localhost:5085) to access your application.
321
+
322
+
### Deploy on Render
323
+
Deploying Flask application on render is straightforward
324
+
325
+
- Create an account on [render](https://render.com)
326
+
327
+
- Ensure your code is present on Github, Gitlab or any public repository
328
+
329
+
- After completing account creation and verification, open your dashboard, click "New", create a `Web Service` and connect the code repository
330
+
331
+
- Use the following values when creating the service
(venv) flask-berry-dashboard$ git commit -m "Changes for deployment"
357
+
```
358
+
359
+
- Create the Heroku application by executing the command and push the code base to the Heroku repository that the application will be hosted from by executing the commands below
This will set up the application and start the Gunicorn server.
365
+
366
+
- Open the application by executing the command
367
+
```
368
+
(venv) flask-berry-dashboard$ heroku open
369
+
```
370
+
371
+
## Conclusion
372
+
In this article, we have covered the steps involved in setting up, preparing, and deploying a Flask application. We have also discussed some of the best practices for securing and optimizing your application.
373
+
374
+
Here are some of the key takeaways from this article:
375
+
376
+
- Flask is a powerful Python micro framework that can be used to create a wide variety of web applications.
377
+
- When setting up a Flask application, it is important to configure your application's environment variables and enable database migration.
378
+
- You can improve the security of your application by using Flask-Talisman and serving static files using a CDN.
379
+
- There are a variety of deployment options available for Flask applications, including Docker, Render, and Heroku.
380
+
381
+
I hope this article has been helpful. For more information on Flask, please visit the official website: https://flask.palletsprojects.com/en/2.1.x/
0 commit comments