Deploy a Python Flask App on Digital Ocean Droplet Nginx

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the scholar online youtube  channel the channel that is all about learning   if you're new to the channel and if you haven't  subscribed yet please click on the subscribe   button at the bottom of the video that you're  watching right now is that red button over there   just click on it and turn on the notifications  so that you'll be notified every time we produce   a new video on this channel we generally produce  new videos on a wednesday and on a saturday and   sometimes only once a week it depends really um  sometimes our videos are very long and then only   produce one video a week instead of two all right  and if you're new to our channel as well we always   uh make sure that we give you all the information  that you need to follow along with the videos   that we produce so at the bottom of the video  right next to where the description should be   there is a button over there that says shows more  you can click on it and it's going to expand the   description okay so if we discuss about you  know a link or you know something that i said   to you i'm going to link in the description below  you're gonna find that link over there all right   and for every video that we make as well we  have time stems to help you navigate through   our content a little bit better as you can see a  lot of our videos are very very long because we   explain all of our concepts in great detail okay  so instead of watching the entire three hour video   you can just read through the time step and figure  out which part of the video you want to see of   course you're welcome to watch the entire video  but if for example you're coming back and you   wanted to remind yourself of something you can use  the timestamp okay the way that timestamps work is   if you for example you want to you know figure out  oh this i want to do a tweet a treat with an image   just click on the timestamp of a day and it's  going to fast forward and take you to that section   that is talking about that okay so this helps you  navigate through the videos that we're making okay   you don't have to watch the entire video okay  so on the description as well we will have links   to our youtube instagram and all of that you know  so feel free to like you know follow us on social   media so that you know you can learn more about  us online in this video i'm going to be taking you   step by step through the process of deploying  your own a python flask web application on the   digital ocean a platform all right we're going  to be building a web application using python 3   um on the ubuntu operating system and  we'll be using jinx as our web server   and obviously the application will be built on a  flask which is a lightweight you know a python um   web framework um and easy to work with and easy  to demonstrate um exactly how to do this all   right so let's get started first of all digital  ocean so obviously to get started you will need   a digital ocean um account you national ocean  a virtual private server um or droplet as they   call it but um this tutorial can be applied to  any other uh virtual private server hosting uh   platform you don't have to be working with digital  ocean you could be working with even domains um   has got virtual private servers you could work  with it's really up to you um what you want to   use i'm using visual ocean because it's one of  the cheapest i mean you can get a droplet here off   for five us dollars and already get started so  that's why i'm gonna work off of this and i'm   just head over there and i will put this link in  the description below and you can get yourself   um any operating system of a day but obviously to  follow along with this tutorial we'll be working   off ubuntu and you can use the latest ubuntu  version which is 20.04 and then you can get   started i also have a tutorial in this um another  tutorial in our youtube channel where i take you   through the whole process of setting up your whole  uh you know droplet from the beginning to the end   and how to set up your ssh keys and all of that  you will need to have that set up before you can   do what we're doing here today so this tutorial  is assuming that you already have your droplet   you've already bought it whether it's from detail  ocean or anywhere else you've already set up ssh   you can already log into your droplet and we're  gonna work off of that so if you look over here   um this is our droplet um you see i'm working off  um you know um um you know a macbook and you'll   see with the coloring day that i've already logged  into my droplet so we're gonna go from the process   of you've already logged in you already have  everything working all right but what i i wanna   spend a little bit more time on is setting up your  domain obviously to deploy an application you need   um a domain you need a custom domain and you  can buy a domain anywhere um i love working   with domain society and i also put this link in  the description below okay so you can go to um   domains you can buy a domain for as little as 80  rents um you know if you get a hosting package you   get the domain for free all right then um once  you're logged into your domains account um um   you need to um set up the dns record this is the  important part why that's why i want to cover this   because you need to set up what they call a dns  records to point to your droplet so there are two   steps here all right you need a virtual private  server which is a droplet so this is your droplet   all right and then you need um a domain all right  so um for the droplet part i think i've covered   that and for the domain part this is where you  need to get it off domains for 0.00 and i'm going   to use one of the domains i've already purchased i  won't buy a new one just to show you this tutorial   and what i'll do is that i will use a subdomain  of one of these ones so i think i'll work off web   design johannesburg.com all right so head over to  your um if you log into your account you click on   manage account and then you click on um domains  over there and then you'll get to the page that   i'm at right now and then when you get here you  need to go to manage over there at the bottom   and make sure you're managing the right domain and  then you need to go to dns records this is where   you need to adjust your dns records there's two  types of dns records you need to adjust um the a   record and the cname record so click on manage dns  over there okay and we're gonna go right to the   bottom of this page and um the a record is already  selected for us over there and um we're gonna just   change this 120 and we're going to create a new  sub domain under this domain right so to create   a new sub domain we decide what we want to call  that sub domain we want to call it a learning so uh you uh the host will be  learning um that the error code   is the type of record is aimed and then  the value of the day will be um 41 76 109 0.72 obviously you not gonna enter the  same numbers i've entered you are going to   enter the numbers here that is the ip address of  the droplet that you have bought so when you come   over here and you purchase a droplet or whether  you purchase it in any other provider you will get   an ip address that ip address is the number that  you need to enter over here okay so don't enter my   ip address or else um it's not going to work and  then once you've done that um click add record we   need to then add what what a cname record so the  cm record will now be um the host for cname will   be learning dot whatever your domain is um so  learning dot web design johannesburg.co.com uh   i mean dot com yeah whatever so click add record  all right so we've covered the first point we've   got our vps and we've got our domain so let's log  into our vps and you need obviously to set up your   dpa vps install all the things that you need to  install um and after you've installed and set up   the vps for python um you need to uh do a  couple more things you need to install in jynx   okay and jinx is going to be the web server  that we'll be using and just do sudo apt install   engines for that and then you also need to install  certbot we'll use swordboard later on to install   an ssl certificate on our website this is optional  um you don't have to do this you can always buy an   ssl certificate from other uh you know third-party  providers but said bot provides it for free with   uplift encrypt so um to get that bot you need to  just uh run the commands sudo app install server   board python 3 set board and jinx you can do it  in one line all right maybe let me make this a   little bit bigger so you can see this right so  you need to just run sudo apt install cert bot   space python 3 dash set bot i'm jinx this will  allow you to run circuit commands um directly off   the you know the command line and then once you've  done that you need to install virtual environment   and then to do that you need to run a pip install  virtual environment okay so these three commands   if i did them for you i'm not going to install  them because i already have these things installed   then um we're gonna now uh you know install the  actual virtual environment we'll be working with   all right and to do that uh we just do virtual  environment like that and uh we're gonna call it   learning you can call the virtual environment  anything you want i like to call it   based on the project name so that i can  remember which virtual environment is which   and i've got a system that i work with so if you  do virtual environment virtual env learning envy   it will install a virtual environment in this  location so make sure you are in the location   you want to work with so if you see i've already  uh created a new folder called learning and i'm   working inside of the folder learn called learning  by installing the virtual environment in there   then i'll click enter and it'll take some time to  install it and then i'm going to just clear this   and if you ls to list everything in here you will  see this is an empty folder it only has one a file   in the one folder in there called learning env  this learning env is your virtual environment so   all the packages we're going to be installing the  python packages are going to be installed in here   um separated so that we can run our application  directly from there once you've done that then   you need to activate this virtual environment and  um so um to to activate the virtual environment   you need to type a source and you're just  going to copy that virtual environment   slash pen slash activate all right this activates  the visual environment not you won't see any   change really except you will now have like a  learning env you know in brackets in front of   your command line and then if you list you'll see  you're still just in there there's nothing else in   there but um this is pretty much what you need  to do to get started our environment is set up   um we have the folder it's installed and then  once you get to this step um you'll be able to   continue with everything else that we're doing all  right so the so the first three steps is pretty   much done in what five ten minutes now we can  start building our actual application all right   and like i said our application is gonna  be in flask flask is one of the easiest   honestly the most lightweight um platforms that's  why i wanted to use this to demonstrate to you how   to uh deploy an application right so um with flask  you know all you need to do is once inside the   virtual environment you need to do pip install  a flask all right so this will run and it will   install flask for you and everything else that you  need with flask and then um we're gonna create a   new file basically you need two things you need  um you need um a directory called templates   where you're gonna install all your templates you  need a directory called um you know a static for   all your static files but we're going to skip  the static directory for now um let's just work   with templates so we can have at least one html  template so for that we just do mkdir our template   all right and then after we've done that  mkdir template um we're going to a cd in the right so we're going to go inside of the templates  of a file and then we're going to um nano   index or html right so this will open a  new file called index.html and we're just   going to put basic html in there  right and i'm going to use my um   you know code um what you call in here to help  me uh figure out what you know the contents of   that html file so in inside of the index.html file  if you're using um the you know atom code editor   you can just type in html like that and then you  press enter and it will give you like a basic html   um you know um you know yeah like basic html  sort of like what you know the things that can   go into a html file which is the opening closing  brackets of the html the head tags the body tags   and in here you can just put your title and  i'm just going to call this learning um and   then inside of the body i'm just gonna have like  um a div um and i'm gonna call this div container   and then inside of here i'll just have an h1  tag and then i will say um this is the learning   website all right that's all i'm going to do  i'm not building this is not a a a tutorial   for building a website this is a tutorial for  deploying a website you would have a website   um at this point that you want to deploy all  right so i'm just going to click yes in there   and i'm on a cd out of the and then once i'm in  here we need to then create the python file that   runs our application and um so that's going to be  um nano app you want all right and then inside of   this python file i'm just going to copy some way  uh basic contents that should go in here and then   i'll come in and paste them in here request render  template you need a secret key um and then for   the app root which is um you know the the home  root of this application we call it home we'll   just render that index html template and provided  we've saved inside of the templates folder in the   same directory as where our app is um everything  should be fine okay then i'm gonna just close this   and and click yes all right and then if you just  wanna test um that your app um you're at this   point you wanna test that your app is running  properly we're just gonna do python app with   py like that and if there's no errors it means  everything at least was imported properly and then   you can just quickly go to um um any uh website  and and then we need to um obviously say http   our domain our um you know um ip address is this  four one uh seven six one or nine seven two and   then it's triple eight all right like that and if  everything goes well you will see that html will   get rendered this is the learning website that's  all we had in the we had an h1 saying this is a   their learning website so basically now we have  a flask application so this is the application   that we are going to be deploying right obviously  in your case you probably have a more complex   application but this is a minimalistic um you know  flask app that is you know with one web page and   obviously for this html you can replace this html  file with anything that you want them you can have   you know um whatever you want in here but for now  i'm just i'm just gonna have this simplistic app   and um we will be using this to deploy all  right so we've finished building our app   all right so the next step is to install um  obviously the tools that will be required to   get our application uh to be um deployed  on digital ocean all right so for that i'm   going to need this file here which has a lot of my  notes all right and we're going to start over the   by um creating a file that is called wsgi.py so  let's uh stop this first of all let's clear this   all right and then we're going to create a file  nano um wsgi to py because we'll be using this   to run our our app all right and once we've done  that inside of this file um we're gonna just have   um like three lines of code so basically we are  importing this file from admins import app from   app this app is that app with py file which is in  the same directory as uh this ws file that we just   created and then we're just running the app so  we're importing the app and then we're running   it that's all we're doing here all right so um  click enter there and obviously at this point   you need to make sure that you have installed a  uw sgi so let's do that quickly okay pick install because we're going to be using this later on and  um successfully installed it perfect that's all   right so after you've installed it um we're just  gonna test that we can use wsgi to run the app   all right so this is like a production grade you  know i'm running because when you run your app um   in uh if you take it from debug to production  you won't be able to run it like the way you run   it on you know normal you know uh debug you know  command line you're gonna need something like wsgi   that is production grade to run the application  that's what that's why we're doing um this what   we're doing now so we're gonna test that our uws  gi is working properly uh by running this line   okay it's just um you know we're gonna be using  um a triple five a port you can use triple h you   can use whatever what you like in fact the ports  that you have inside of your app at this point   becomes irrelevant um and um let's test this okay  and if you don't get an error over there you're   good but to test that the app is still running  we're gonna just um you know refresh this page okay so it's no longer running on triple h  that's why we have that problem so we need to   try it on triple five uh wsgi is running it on  triple five there you go so the application is   still running um wsgs is running it on triple  five you can see some of the records of a day   so um we're good to go so you can stop this  and then um we've got the second step covered   okay and then the next one now  we can now completely deactivate   um we can completely deactivate our learning  environment virtual environment because we are   now creating some files to help us um you know um  you know use a system to run this for you know um   our application all right so the first  thing that we need is to create a um   you know an ini file and inside of that ini file  we're going to create a socket that we're going to   be using to run our application all right and for  that i'm just going to create a file that's called   nano learning dot ini just like that right and  then inside of that ini file um we're going to   just paste this contents in there and i'll show  you later on what this is that we're pasting in   here basically we're telling it that we're  running uwsgi as an app we'll be running it   on five processors and then we're gonna create  a new socket and that socket we're gonna call   our learning and it will be created in the  same directory um that we're at and then   once you've done this um there's optional uh  location of log files and this is useful for   if you're still debugging and if you still sort of  want to be able to keep track of all your logs um   you can specify specifically where you where you  want your log files to go um so when you have an   issue with your um application you can go and look  at the log files and this is where you would see   you know the um the content that will be print  that would have normally been printed on your   terminal window um if you printed anything out or  if there was an error and and you had had a log   record are created um you know they will go into  the log files okay but i'm not going to do um the   log files for now i'm just going to do this and  then i'm going to close this and save it and say   yes all right so once you've done that then we can  go into creating our system file the system file   is the is is a file that is um saved inside all  inside of your um you know operating system system   as a section um every time you restart your server  every time you restart your droplet your vps   whatever happens the system files always get run  um so that um you know your application will be   up all the time you know so system file  helps you know keep your uptime of your app   up so if something went wrong there was silver  maintenance there was issues um with you know   um with anything and this happens all the time  especially with the visual private servers that   they'll go under maintenance or something will  go wrong and something will break and then it'll   switch off and then it switches on again let's  say it's in the middle of the night and you're not   reminded to go back and switch on your application  um the system file will rerun all the system files   get rerun um um when the um you know the droplet  gets uh restarted so um your web app also will get   you started that way right so um for the system  file i will need to put it in a specific location   um the system md location um and um so this is  the location that it needs to go into um etc   system md system and then you can give it a name  over there and it needs to be a service type file   all right and obviously because this is a this  is a pseudo location or this is an important   location we can only access or create or delete  anything inside of this location with a pseudo   privileges that's why you have the sudo um  at the beginning of that command over there   so um so only a super user can run this  all right so um we're gonna then um   run this command and we're gonna call  this a service file learning service all right this means that um we'll be able to  access the file using the learning name and create   it in the same location where all our system files  are created right and then i'm gonna click enter   and then i have to enter my password obviously to  run a pseudo command and then inside of that file   you're gonna enter a couple of things i'm gonna  just copy this and then i'll take you through the   lines um step by step right the first um uh part  obviously is to you you need to give it a name all   right um so it's the uwsgi instance to serve um  the learning web app all right so i'm just going   to call it to save learning all right and then  this is a service um that is going to be running   um the user will be myself so whatever the user um  that you want this system to run um s you need to   put in that username there and then the group  will be www data for you know for the website   and then after that you need to specify three  things you're working directly your environment   and your executive start order okay so  this is um these are the three important   things that you need to specify and um  for our python app uh we need to know   where the location of the working directory is  right so for me the location was our development   and i think i created a folder in there called  learning right so it will be development learning   let me just copy that learning because  um we'll be using it a lot and then the   environment this is where you have saved your  virtual environment okay because it will be   accessing that virtual environment to run your  python file okay so you have to specify where   the virtual environment is and um the name of  my virtual environment if i recall correctly   i did call it a learning env so it's important  to use the same you know consistent naming   convention so that um everything works out in the  end and you don't forget what you named something   if you name it in a consistent  manner you'll always remember   so um inside of my development folder there  was a folder called learning and then the name   of my virtual environment was learning env um so  that's all you need to specify and then over there   um to the exec start obviously you  need to mention the environment   and then um the init file right so for that um  the virtual environment here again is learning env all right and then the the  learning envy file is inside of   learning inside of development and then the  name of the unified was also learning.ini okay if you're not sure at this point you can  stop and go double check again what you called   everything all right you can double check what  you call the ini file you can double check what   you call your uh your virtual environment you can  double check again the location of your working   directory these are very very important if you  have even a small slight error here you're going   to have a problem running your system file all  right so i'm going to save the changes here um   and then close that and and this is where we are  i'm just going to click ls and you can see we did   call our learning our file our i mean our ini file  learning dot ini and um our environment is also   learning env and this location is development  learning okay so just make sure all these are   right inside of your um you know learning do  service file okay it's once you're done with   the with the service file you can do a couple  of checks to make sure that your service file   has been installed um correctly all right so you  can start the service all right so to start the   service you need to do um sudo system um ctl start  um the name of our service is learning all right   so let's start it and then um if there's no errors  it needs to study the service properly all right   um and then we're gonna enable the service all  right you need to enable it that's the next step enable learning all right and then you'll see a feedback that  we've created assembling for learning that's good   so your service is now enabled and then you can  check the status of your service okay this is   important if there's errors even if there's  errors now inside of your application itself   uh if it wasn't started properly like maybe it  was a syntax error or whatever you see it here   all right um under the status so the status  of the learning service all right so you'll   see in our specific case we've got green which  means the service is running without an issue   and this is what's has been printed out okay um  wsgi started this so everything is working well   and we don't have an issue um our service is  running all right so i'm going to clear this   if you have a red over there um then something  is wrong so just double check your system file   again just double check all the routes of uh uh uh  you know configured properly double check that any   file is configured properly just go through this  video again and make sure you don't miss a step   uh if you follow everything i've done step by  step at this point you should have a green over   there without an issue all right then um this  needs to be green before you can proceed to the   next step if it's not green you're gonna continue  just gonna have more problems in the future right   so now if you go back at um everything that  we're doing over there we are now um we've   completed we've completed everything inside of  the tutorial except created the jinx file which   is finally gonna actually serve our application  all right so um to do that that's the last step um we are going to um create um the jinx files  in this location okay you need to create it under   each single site available and then you're gonna  whatever you wanna call your jinx file in there   all right and because this is also um you know  um you know an important location you need pseudo   privileges to create anything in here all right  so i'm just going to copy all of that and paste   it you see so studio nano this is opening up a  new file inside of this location and that file   is going to be called whatever we put the name in  there so we're going to call it learning you can   call it learning.c if you want to remind yourself  it's a learning website you can call it learning   dot whatever whatever helps you remember okay if  you don't yeah i don't have a lot of things inside   of my virtual server so if i call it learning  i'll know what learning is for but at this point   you must give it a name that um you will know what  what was inside of that file when you created it   all right um so instead of the learning uh  i'm just going to copy what's in here this is   all you need to put in there all right so you're  just basically telling jinx to listen at port 80   all right and then once you've listed  port 80 um you need to then um uh you know   if if if any if if you get any if if anybody  makes a request a post or a um a get request   on port 80 this is a server under this server  then you're going to serve that location right   so um the name of our server remember i think we  called it learning dot web design johannesburg.com um it was learning dot web design and you can also do www here let me just copy this i  don't have to retype it again okay www learning design johannesburg.com right so  this is a server that somebody would be requesting   um and listen and then jinx will  be listening on this port for this   request and then if you get it you're going to  then serve the file the any file or the socket   file that's inside of this location okay  so i know i called my socket file learning   all right and then the location  is uh development learning all right so that's all you have to do  you have to go find where you saved your   your your this actually your working  environment so everything up to here   is the working environment of where  your python flask application is saved   and then um you need the socket file which was  created by the ini file um and the socket file   will be now called learning.sock but that this  is also inside of your ini file and you can just   double check to make sure that this is correct and  then once you've done this um also make sure that   your server name is correct you're just going  to do that and save the changes and click yes   right and then once you've done that you need to  then initiate and jinx but before we can do that   before we can initiate in things we just need to  link um sites uh available to sites enabled all   right okay so just replace that with learning  and then you press enter once you've done that   we then um i need to check that there  are no errors inside of our in jinx file okay and then it's it's telling us there  that our configuration test is successful   then once we're done with that we're just  going to restart um jinx and um if everything   is has gone well you should be able to go to any  website and just look for um you know your domain   i mean your server learning web developer  jonathan.com and you just press enter   they and then you should be able to see the  website that you created running up properly
Info
Channel: Skolo Online
Views: 2,342
Rating: undefined out of 5
Keywords: python, flask, digital ocean, flask digital ocean, digitalocean, deployment, nginx, deploy, developers, deploy flask app on ubuntu server, deploy flask app, deploy flask app ubuntu, deploy flask app linux, flask tutorial, python flask, flask deployment, flask application, python flask tutorial, python flask tutorial for beginners, python tutorial, flask tutorial for beginners, python web application, python web, python flask from scratch, python web development, web application
Id: RP8nhiiQnTc
Channel Id: undefined
Length: 31min 38sec (1898 seconds)
Published: Sat Apr 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.