Python Flask Tutorial: Deploying Your Application (Option #1) - Deploy to a Linux Server

preview_player
Показать описание
In this Python Flask Tutorial, we will be learning how to deploy our application to a Linux Server from scratch using Linode.

If you would like $20 of free credit towards a Linode account, then you can use my link here to sign up:

We will be covering the entire deployment of a Flask application. This includes spinning up a new Linux Server and tightening its security with SSH keys and firewalls. Then we will install Nginx and Gunicorn so that we can get our Flask App running. Let's get started...

To skip the server setup, jump to 26:48

NGINX config used in this video:

Supervisor config used in this video:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python #Flask
Рекомендации по теме
Комментарии
Автор

In this video, we set up a Linux server from scratch before we deploy Flask, which is what a lot of people have requested. But if you already have a server ready to go then you might not be interested in the setup part. If you'd like to skip the Linux server setup and go straight to the deployment then please skip to 26:48

coreyms
Автор

== TIME STAMPS of CODE and COMMANDS==
29:41 - `pip freeze > requirements.txt` - and move the text into Flask_Blog folder
31:50 - `sudo apt install python3-pip`
32:12 - `sudo apt install python3-venv`
32:44 - `python3 -m venv Flask_Blog/venv`
33:19 - `source venv/bin/activate` - in our Flask_Blog dir we activate our venv
33:50 - `pip install -r requirements.txt` - we install our pip packages into our venv
37:40 - `sudo touch /etc/config.json` &
`sudo nano /etc/config.json` - where we now store our environment variables inside a file instead of the system
40:22 - `sudo nano flaskblog/config.py` - *add* the following code
`import json`
`with open('/etc/config.json') as config_file:
config = json.load(config_file)`
41:50 - we are still in config.py and *replace* `os.environ.get` with `config.get`
43:35 - `export FLASK_APP=run.py` - where we assign the environment variable FLASK_APP to the module
43:55 - `flask run --host=0.0.0.0` - we run a local dev server on the linode
48:26 - `sudo apt install nginx`
48:53 - `pip install gunicorn`
50:02 - `sudo rm - we remove nginx default config
50:20 - `sudo nano - new nginx config solely for our app. Add the code from Corey's snippets. Remember: gunicorn is running on port 8000, nginx is running on port 80
54:27 - `sudo nano ufw allow http/tcp` &
`sudo ufw delete allow 5000` &
`sudo ufw enable`
55:00 - `sudo systemctl restart nginx` - restart nginx server
56:25 - access your URL/static/main.css. This proves that nginx server is serving static content and that it cannot serve the dynamic content because Gunicorn is not running yet.
58:02 - `nproc --all` - to find out how many cores on this machine
58:35 - `gunicorn -w 3 run:app` - now that gunicorn is running, we access our main site and this proves that nginx is passing HTTP requests to gunicorn
1:00:23 - `sudo apt install supervisor` - this software (written in python!) will run Gunicorn in the background
1:00:50 - `sudo nano - create a supervisor config file referring to flaskblog package, and Gunicorn. Add corey's snippets
1:04:10 - note that we point supervisor to (yet uncreated) log files located:
&

1:05:03 - `sudo mkdir -p /var/log/flaskblog`
1:05:22 - `sudo touch &
`sudo touch - where we now create our log files
1:05:42 - `sudo supervisorctl reload` - where we restart supervisor
1:07:40 - `sudo nano /etc/nginx/nginx.conf` - where we access general config file for nginx
1:07:54 - `client_max_body_size 5M;` - add this code in the config file to increase upload limit from 2MB to 5MB
1:08:30 - `sudo systemctl restart nginx`

Your site is now in production! :D

lightninginmyhands
Автор

Almost three years has passed, yet still a very helpful and thorough video! Thank you!

catman
Автор

Just came here to say that I really enjoy your videos. I’m still learning the basics of python and your videos are very compact and easy to understand. Thanks for your great work and the time and effort you put into these videos. They help me and probably many other college students a lot that can’t really effort expensive online courses.

TheRingo
Автор

The way he explains everything in such a natural way makes it all seem so trivial! I love it! It all just....works when he explains it.

coreycarter
Автор

There are so many short "to the point videos" out there but I really appreciate what you did Corey, for us new guys it's helpful how you show best practices and all the little details. This is so helpful, especially for those that are not familiar with linux

kianow
Автор

Request: Please create a video on migrating the DB to PostgreSQL. In the initial videos of this series, you mentioned that you will show how to use Postgres. Eagerly waiting for this :D

zuman_one
Автор

Great stuff Corey. Really blown away by the clarity and attention to detail.

notbsd
Автор

Was tasked to get a Flask app up and running on a Linux server.
I have never seen Linux before, and thought Gunicorn was something from my daughters fairy tale books. I followed this video step by step and without a hitch, had the site up and running within an hour. What an excellent video, well presented, to the point.
Thank you very much.

GrahamRusson
Автор

I appreciate your thorough guide and doing everything the right way. So many tutorials skip over every other detail and just show the bare minimum. I like how your video shows all the best practices. Thank you!

howards
Автор

After many hours searching for how to deploy my project, I find everything I need right here.
Amazing work Corey :D

GabrielSantos-jskk
Автор

1:10:15 Corey, the best feeling was shutting down that console and still seeing my site online. THANK YOU

lightninginmyhands
Автор

I can't believe you're providing this high quality content for free! I'm supporting your channel to Patreon and recommend others to do so too!

rxtez
Автор

2023 and this series is still relevant thank you, Corey.

Destide
Автор

Your whole video series on this is absolutely amazing! Thank you.

vat
Автор

followed the whole series. Really amazing. Wish I could stop you at times to explain but figuring it out myself is when I learn the most!

matttamer
Автор

A great thanks to you Corey, your way of explaining the topics in a simple way make me all the way to learn the flask in a easiest way. Each and every video is crystal clear and easy to follow. Thank again.

ShyamGoli-uz
Автор

Corey "let me clear my screen here" Schafer :D.
You are amazing Corey thanks for the in depth tutorial!

SorinOltean
Автор

I don’t understand why some people put dislike on such Video, very informative very clear video ... I learned a lot ... thanks

Hassan
Автор

Seriously, your videos are top of the top of the top. I knew all of what you explained here, but I wanted to watch the whole video again to see if there were more tips and enjoying the way you explained. Awesome man.

You know what would be nice? Tutorial on prefect, much better than airflow. open source, pythonic, etc. great way for pipelines.

tomasemilio