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
- Create a file named `.env` in the root folder of your project, this file will hold environmental variables and their values.
54
67
55
68
```bash
@@ -61,17 +74,22 @@ DB_HOST=<db-host-name-or-ip>
61
74
DB_NAME=<database-name>
62
75
DEBUG=False
63
76
```
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.
77
+
78
+
`SECRET_KEY` is a random string that is used to protect your Flask application from attack.
79
+
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
80
66
81
Using the `python-dotenv` package, the contents of the `.env` file are loaded into environmental variables when the application starts.
67
82
68
-
### Enabling database migration for application
83
+
<br />
84
+
85
+
## Handle `DataBase Migration`
86
+
69
87
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
88
`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
@@ -88,21 +106,27 @@ Migrate(app, db)
88
106
- Next step is to create the migration directory and apply the migration to the current database schema
89
107
90
108
```bash
91
-
(venv) flask-berry-dashboard$ flask db init
92
-
(venv) flask-berry-dashboard$ flask db migrate --message "initial db migration"
109
+
(venv) $ flask db init
110
+
(venv) $ flask db migrate --message "initial db migration"
93
111
```
94
112
95
113
By setting up `flask-migrate` you can be able to move scale your application database and alter its schema without losing data.
96
114
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.
115
+
<br />
116
+
117
+
## Compressing Assets via `Flask-Minify`
118
+
119
+
`Flask-Minify` is a Flask extension that can be used to minify HTML, CSS, and JavaScript files.
120
+
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.
121
+
122
+
> Install flask-minify from the terminal with the command
99
123
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
139
117
-
### Increasing Application security using `Flask-Talisman`
140
+
Test the minification of your files to make sure that it does not break your application.
141
+
Minification can sometimes introduce errors in your files, so it is important to test them thoroughly before you deploy your application.
142
+
143
+
<br />
144
+
145
+
## Increasing Security using `Flask-Talisman`
118
146
119
147
`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`
@@ -134,31 +162,35 @@ talisman = Talisman(app)
134
162
```
135
163
With this, we have added a new layer of security to our application in a production environment.
136
164
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.
165
+
<br />
166
+
167
+
## Serve Static Files via `CDN`
168
+
169
+
A content delivery network (CDN) is a network of servers that are distributed around the world.
170
+
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
171
140
172
To deploy a Flask application using a CDN, you will need to:
141
173
142
174
- Choose a CDN provider. There are many CDN providers available, such as Amazon CloudFront, Cloudflare, and Akamai.
143
-
144
175
- Create an account with the CDN provider.
145
-
146
176
- 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
177
- To configure your Flask application to use the CDN, add the following to the `.env` file
178
+
149
179
```
150
180
# .env
151
181
# CDN Support
152
182
# 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
192
+
> 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
193
+
162
194
```py
163
195
# apps/__init__.py
164
196
...
@@ -175,44 +207,62 @@ def create_app():
175
207
return app
176
208
```
177
209
178
-
- Add the CDN domain to your python flask configuration.
210
+
>Add the CDN domain to your python flask configuration.
With this done, our application is ready to serve static files over a CDN.
199
234
235
+
<br />
236
+
200
237
## 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.
238
+
239
+
Deploying a Flask application is a crucial step in turning your Python web application into a fully functional and accessible website.
240
+
It involves the process of making your Flask application available to users on a live server, ensuring its stability, security, and optimal performance.
241
+
Without proper deployment, your Flask application may not reach its intended audience or function as expected.
202
242
203
243
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
244
205
-
Before you start the process of deploying your application, execute the command below on your terminal to create an updated `requirements.txt` file
245
+
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.
254
+
255
+
Docker is a popular containerization platform that allows you to package and distribute applications along with their dependencies in a portable and isolated environment.
256
+
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
257
213
258
For this tutorial, we will be using `Nginx` as the reverse proxy server and `Gunicorn to serve the application.
214
259
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.
260
+
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.
261
+
Gunicorn is an application server that can be used to run web applications. It is designed to be used with a reverse proxy.
262
+
If Gunicorn is exposed directly to the internet, it can be vulnerable to denial-of-service (DoS) attacks.
263
+
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.
264
+
265
+
This type of attack is known as a Slowloris attack.
216
266
217
267
We will start by configuring our docker container.
- Execute the command to build the docker image and deploy the application on docker.
367
+
317
368
```bash
318
-
(venv) flask-berry-dashboard$ docker-compose up --build
369
+
(venv) $ docker-compose up --build
319
370
```
371
+
320
372
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
373
322
-
### Deploy on Render
374
+
<br />
375
+
376
+
### `Deploy on Render`
377
+
323
378
Deploying Flask application on render is straightforward
324
379
325
380
- Create an account on [render](https://render.com)
@@ -333,43 +388,53 @@ Deploying Flask application on render is straightforward
(venv) flask-berry-dashboard$ git commit -m "Changes for deployment"
415
+
(venv) $ git add .
416
+
(venv) $ git commit -m "Changes for deployment"
357
417
```
358
418
359
419
- 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
426
366
427
- Open the application by executing the command
367
428
```
368
-
(venv) flask-berry-dashboard$ heroku open
429
+
(venv) $ heroku open
369
430
```
370
431
432
+
<br />
433
+
371
434
## 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.
435
+
436
+
In this article, we have covered the steps involved in setting up, preparing, and deploying a Flask application.
437
+
We have also discussed some of the best practices for securing and optimizing your application.
373
438
374
439
Here are some of the key takeaways from this article:
375
440
@@ -378,14 +443,11 @@ Here are some of the key takeaways from this article:
378
443
- You can improve the security of your application by using Flask-Talisman and serving static files using a CDN.
379
444
- There are a variety of deployment options available for Flask applications, including Docker, Render, and Heroku.
380
445
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/
446
+
We 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