How to Deploy a Flask App to a Linux Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome to the final video in the flash tutorial series we're actually going to take this kind of minimal website that we built and deploy this on to a Linux server specifically using Ubuntu now the reason I'm doing this rather than using Heroku or something like that is because I want to show you guys how you know you can do this yourself just using a bare-bones you know headless Linux server it's it probably the cheapest option for most of you if you're gonna be doing website hosting gives you the most flexibility in terms of what you're able to do and just a good skill to have since we're gonna be doing today a bulk of how I've learned to do this is actually from this website here I would say that it's maybe a little bit outdated there's a bunch of stuff in here we don't actually need to do so that's why I'm not just you know kind of refer you directly to this page but I will leave a link to it in the description because it has helped me learn kind of about this process anyways what we're gonna start by doing is just downloading some software and then we're actually gonna set up a linux server and we'll talk about that more in a second we need to download putty some of you probably already have this but I'll leave a link to it in the description down below it's free software we're also gonna download winscp which is going to allow us to do some file transfer between our computer and the linux server that we're gonna be setting up again these will be linked to the description download that software and then we're gonna talk about lonoa quickly which is actually the sponsor of this video and it's going to allow you guys to be hosting this website for free without having to pay for any server provider at least while you're testing this out so essentially the low note has been kind enough to sponsor this video they're gonna be giving you guys $20 free credit towards your first Linux server now this will actually allow you to use the most basic plan on the node which is five dollars a month for four months for free so it's awesome if you're just doing some development testing some stuff out whatever Web Apps you want to use you don't necessarily have to make a website you can do pretty much whatever you want with lenôtre if it runs on Linux it will run on low note they have an awesome support team it's 100% human it's 24/7 they're always willing to help you out they've helped me out before personally and to deploy your server just hit the link in the description down below and it look you guys you know that free $20 so I'm gonna click deploy my Linux server again that link will be in the description you can also use the discount code twt 19 I'm just going to set up a basic linux server so i'm going to go here to create create a Nuala note i'm gonna choose my distribution which I'm gonna pick it bun two four so a bun to nineteen point ten you can also actually set these up with one click images unfortunately there's not one for a flask application but if you're doing something like WordPress or I don't know creating a minecraft server then you can actually just click one click you can click these and that everything you need will actually be installed on your server automatically which is pretty nice so let's go back to distributions for Regions oh I need to pick a bunch you again so I'm just gonna go the most recent version of a bunch of you guys can pick whatever you want for regions pick whatever you want I'm closest to Toronto so I'm just gonna pick Toronto Ontario but again they have all these different data centers that you can choose from okay so for the plans I'm gonna recommend to you guys that you don't need anything crazy here again you have your free $20 credit so I would just go with the most basic plan which is nano duan gigabyte you can see how much it cost per hour it's literally like fractions of a cent and then for the name I'm just gonna call this a test because while this is gonna be a test for us right now for the password of our server make sure you guys remember this mines weak but that's fine you're probably gonna want to make this a strong password as if someone has your server IP address which is pretty easy to get they can actually log into your server if they can guess this password great okay so once we've done that we'll click create this is just gonna take a second to boot up and once this is done we'll be back and we'll go into the next steps okay so that booted up now so what we're actually gonna do is go to this networking tab you should see this under your server and I just want to make it clear that you know you guys don't necessarily only have to have one server here so while you have this $20 credit you can create another loan owed you can use it for whatever you want you can use those one-click so you can upload your own images actually if you want to put those on the server and you know you can just use it for other things so don't think you can only have one server you could have multiple with that $20 credit anyways what I'm gonna do is I'm gonna grab the IP address that is here so it says this under access SSH access it says root at and what we're gonna do is just copy this so whatever this IP address is I'm gonna open up my putty window which I'm assuming you guys have already downloaded I'll zoom in on this you guys can see and for the hostname we're gonna type in the hostname that is for our server so whatever one we found from the Linode website then I'm just gonna change the text size of this to make it a little bit bigger so that you guys can actually read it while I go through this so I'm gonna change this to 22 and then inside a putty we're just gonna save this as a session so I actually have flask app already here so what I'm gonna do is highlight this and hit save and what that's gonna do is save this IP address and save that text size so every time I go in here I can just click on flask app click load it will load this IP address for me load all my settings I can click open I can get into that quickly good idea to do that if you guys don't have any here what you can do is just give this a name so like hello hit save and then it will save it for you so you can see we have hello now saved like that open this up and when we open this up we're gonna get a warning that says warning potential security breach just click yes and then we'll be logged in and ready to go okay so what we need to do is log in as the root user so the user name is gonna be root password is gonna be whatever that password is you set when we were setting up this server and now we should be in here and we should be ready to start typing some commands and getting some things done so the first thing that we need to do is actually install Apache on our server now I think that's how you say it Apache Apache I don't know the actual name but anyways to do that we're gonna start off by just doing a system update to make sure everything's good so sudo apt update like that give this a second it'll run through and once that's done we'll move on to the next step okay so we finished that now and what we're gonna do is install Apache so to do that we're gonna say sudo apt install Apache - I believe that's correct it's gonna pop up a thing that says you want to install this yes or no we'll hit Y go ahead and hit enter wait for this to load and then again we'll go into the next step and test that Apache is actually up and running and working okay alright so that is finished now what we're gonna do is just make sure that this worked properly we're gonna type Apache - - version if we get some output here that's non error so we see the server version and the server build we are all good to go and we are ready to move on to the next step which is actually configuring our firewall now since this is a Linux server and I guess we're hosting the samel node by default most of these have firewalls and they don't actually let Apache through so we have to make sure that it's gonna go through so that people can access this so to do that we're gonna check what the services are for our firewall and then it will just add the appropriate one so in this case we're gonna type sudo UFW list like that and then you'll see that we have a pachi pop up here you can see these other two you don't have to worry about those necessarily what we're gonna do is just add this to an aloud service for for a firewall serene it's a pseudo you have W allow and then single quotation marks appache like that now make sure you type this line exactly you don't need the full you don't need to secure this is fine and then you should see that the rules are updated and now this is allowed in the firewall ok so now that we've done that the next step is just to configure Apache and make sure it's actually running properly so to do this we're gonna say sudo systemctl and then I'm just gonna look at my other screen to make sure I do this right status and then in single quotes Apache like that you should see oh my apologies on that so I did single quotes Apache but I guess I was reading my wrong thing it's actually gonna be a pod sheet too and when we do that so we say sudo ufw where's that sudo systemctl status apache - not this single quotes Apache you should see some green text that says active running and you should get that little green dot there if you get that this means it's running everything's fine you've completed these steps we're ready to move forward now to get out of this we're just gonna hit ctrl C on our keyboard that should exit out of that and we'll be ready to move on to the next step which is to install a few libraries now this one's kind of a long line again it'll be in the description so you guys can just copy them if you want we're gonna say sudo apt - get install and then in this case Lib Apache - - mod - WSGI and then we're gonna do space Python - dev now I know that was a lot but essentially what this is gonna do is just install some libraries that we need for WSGI which is kind of how flask runs I'm not really an expert on this so I'm not gonna talk to detail because I don't want to make any you know horrible mistakes and mislead you guys but essentially that was the command right here sudo apt we get all that we'll hit yes go ahead and continue okay so now we've done most of the setup that we actually need to do and what we're gonna do now is set up a few directories and get into the right area where we're actually gonna place our flask application then we'll do a few more configurations and we should be good to go and actually host this flask app so what I'm gonna do is CD into var ww slash now this is gonna bring you to where you need to put your flask application now I believe you can put this somewhere else but this is actually I don't know I've only ever tried it in this location so I would recommend put it here just follow along with me get into this directory and we'll actually gonna create a few more directories now we're gonna start by just making a new directory which is gonna be called web app now if you name this something different make sure you remember that name and wherever you see me type web app you type that name okay so the names here are gonna be very important a big mistake a lot of you guys will have is you name it something different you forget you name it something different and then you're gonna be using my script which is referencing this directory so just make sure you know if you want to make sure this works just name everything the exact same as me so web app like that is what I'm gonna go with now that I have that I'm going to actually I don't need to CD into there right now um or do i you know what yeah let's let's CD into web app and now we're gonna just install some Python stuff and then we'll actually be good to go and drag in some files so what I'm gonna do is start by installing Python and pip so I'm gonna say sudo apt - get install oops Python - pip so this is gonna install pip make sure that we have that correctly and then once we have pip we're actually going to install flask we're gonna install flask SQL alchemy and then we will have the Python libraries that can actually run our Python code from the Apache web server or whatever it is that's gonna be doing that okay so now that we have this what we're gonna do is install flask and we're gonna install flask SQL alchemy so this is the exact same as we've done in previous tutorials so pip install flask like that wait for that to run through and then now we're gonna do SQL alchemy so pip install flask underscore SQL alchemy like that um and there we go once this goes through we will be good to go and now we're actually gonna bring our flask app on to this which is going to involve us using a different piece of software so I'm gonna open up winscp which is actually just a file transfer protocol and what we're gonna do is setup a new kind of entry in here login to our server and then transfer some files over this just makes it easier than having to actually copy and pay stuff because no one really wants to do that so what we need to get to start is the IP address of our server so good job I left this open because I forgot so I'm gonna copy that that's my IP address I'm going to put it in here so if you guys are here you might actually need to click new site my apologies you're gonna put the hostname as whatever your IP address is the username is root you can leave the password as blank for now and then go ahead and hit save when you do that it's gonna bring up a window save this as whatever you want I'm just gonna say test because I probably will be deleting this after and then I'm gonna go ahead and go in and log in now it's gonna say update add all this stuff for you if it just says add you're gonna hit add for me I'm just gonna go ahead and hit update and then I'm going to sign in with the correct password so whatever that password was that you set up as your root password that's what you need to sign in with now you should be in the root directory we're actually gonna go back one directory by just we can double click on that thing that's right there and we're gonna go to a www folder which is gonna be under bar so we're gonna find all of this we're gonna look for var which is at the bottom we're gonna go to w-w-which conveniently is at the bottom as well and now you should see that we have this folder web app which was actually what I have created from the terminal so what I'm gonna do now is open up this folder and inside here is where I'm actually going to put my flask application now I'm assuming you have already created a flask application that's why you're going to be deploying one but there is a few kind of nuances that we have to think about when we do this so I'm gonna find my flask app I'll be right back and I'll talk about how we add that ok so I found my flask application you guys can see these are the files need to be using so I have my static folder my templates folder views an it and then users sqlite3 now yours might look slightly different than this obviously if you create a different flask app things that you have to make sure are the same you have to make sure you have a static folder you have to make sure you have a templates folder and you have to make sure that the starting point of your application is from an it PI so underscore your net underscore underscore PI that needs to be the file that's gonna start your application so just make sure that you have those three constraints so static templates that an it file and then you should be good to go and so long as this flask app runs properly and there's no Python when you're doing it on your kind of own development side so not from the server this will work fine so I'm gonna drag these files into my web app so it's as easy as that this is the file transfer just from my Windows Explorer you can also do it from this left side here where you can just find the files and transfer them over this one I find easier what I'm actually gonna do is something weird now where I create a new folder or new directory so like this and I'm gonna call it the exact same thing that I've been calling my other stuff so in this case web app I mean create another folder called web app and I'm gonna take all of these files so everything that I dragged in and I'm gonna put them inside of that folder so I guess I could have created that folder first but regardless what we're gonna have our structure looking like is we have this web app folder inside of here we have another folder called web app and then inside we have actually our flask application and again that starting file from an it PI I'm really hammering the same cuz I want to make sure you guys don't mess up this process because this is where I went wrong a few times before so again project directory web app another web app folder your applications inside of there we're gonna put some other stuff in the outer directory so now that we've done that we can close this and just so you know if you do want to update your flask application the way that you would do it is going into Wynn SP and then just changing your Python files to update you know the new flask application okay so now that we've installed all the Python pack is what we're gonna do is actually set up what we call a new virtual host now I don't really know exactly what this does to be quite honest with you but I'm pretty certain that this is just the way that your website is kind of going to be displayed some settings for it and this is what flask needs so well that's what we're gonna do so to do this we need to create a new configuration file inside of a specific directory we're gonna type is sudo nano and in this case I gotta look at my other screen to make sure I don't mess this up et Cie slash apache2 slash sites - a vailable like that slash and now you're gonna type the name of your file which is gonna be web app dot Kampf now make sure that this is gonna be the same as whatever you've named those folders so if you name that folder app then you're gonna make sure this is app comp okay there has to be consistent now we're gonna go ahead and hit enter like that and now it's gonna tell us oh this directory does not exist well that would mean I typed this incorrectly so let me make sure I spell ah available correctly my apologies on that so anyways take exit out of this you hit ctrl X you can see kind of the commands are down here this little up means control believe on Mac command will work as well and to say you're gonna hit control s now what we're gonna do is copy in a block of code now I'm gonna take this from my other screen but I will tell you that this is in the description so you guys can find it and to paste something into this window what you do is right click on your mouse so I'm gonna right click and you're gonna see that all this code pops up again this is available in the description so don't worry you can copy it from there now what you need to do essentially is don't touch anything in this file other than the server IP now notice though when we look through here everything is referencing what I've named my folder so in this case you can see we have WSGI script alias and it's in our directory so of rww and then you see web app that's the name of the folder that we have our application in and then it has another directory where it actually has web app web app and this is gonna run our Python files right so inside of here and you can see all of these different directories we're referencing the static folder over here so make sure that if you've named your folder something else you're gonna change all of these to reflect that so wherever you see web app you're gonna change that to be whatever you named your folder okay so now that we have server name what I need to put here is actually the IP address of my server so I'm gonna grab that from will ánotá chizz right here again you should already know that because we've seen that and I'm gonna paste that in by just right-clicking and we'll make sure that we keep the space and consistent and fix that so to navigate this file I'm using arrow keys and now what I'm going to do is save this by hitting ctrl s on my keyboard and then exit by hitting ctrl X now we're kinda in the homestretch here we're almost there there's only a few more things we need to do what we're gonna do now is actually activate this so to activate this we're gonna say sudo and then in this case a to en site web app now this should hopefully activate this we'll see what we get this is an abling site web app to activate the new configuration you need to run this so what we're gonna do is run this command I'm gonna copy that paste that in there so system reload Apache - once you do that you should be good and sorry that's systemctl my bad on that you shouldn't get any output if that's working there's no errors we're good to go I'm just going to clear the screen and we'll move on to our next steps so we're actually on the second last step that we need to do here we need to create now what we call a WSGI file now this WSGI file is kind of going to be the starting point of our application and it's gonna lead and kind of import the other Python file it actually stores our main application now I know this is kind of confusing but just follow along with me we are currently in VAR w w slash web app make sure you're in this directory when you're doing this we're gonna create a new file I'm gonna say sudo nano and in this case we're gonna call this file web app dog ABI WSGI now notice this has a lowercase a now if you decide to change this name you'll have to go back to that other folder or that other file that I just modified that had all that text and change where it says web app wsgi to be this appropriate name okay so that's why I've recommended you just keep the names the same but if you're changing them you're kind of gonna have to go in there and fix them for yourself so let's go ahead and hit enter and now what we're gonna do is type a little bit of Python code now I'm actually just gonna copy it in to save us some time again this codes all gonna be in the description so you guys can grab it but what we're gonna do is copy this and paste this in now here what we're actually going to be doing is we're gonna be trying to load in our application from that interior web app folder and from there we're gonna load that init script and we're actually gonna run the application so that's what this is attempting to do essentially now notice that it says from web app import app as application so this is Python code but what this is gonna do is look at that folder web app which is the next folder in the directory it's gonna look for that an it dog Pi file and then from that a knit top high file it's gonna look for app now app is actually the variable that we've used to store our flask applications so when you say and I'll actually just type it here app equals and then in this case flask like that it's trying to reference that so if you've named this variable something different when you're setting up your flask application then you need to change that accordingly in this file so from web app import app well if your folder name is different this is obviously gonna be different same here right if your folder name is different you're gonna have to change this WW to be the correct path and then from app same thing if that variable is different you're gonna have to change the name now we've referenced this or imported this as application and now what we're gonna do is set the secret key of our application so app add your secret key you could just leave it like this I'm just gonna make mine tech with Tim you guys can again do whatever you want for the secret key so attack with Tim I'm gonna save that file hit ctrl X and we are almost at the moment of truth we're actually going to start running our application now just notice if I type ls' here you can see kind of the structure that we have we have web app and then we have web app WSGI and if I CD into web app and do LS notice that I have that an it up high static templates user it's abused all of that okay so let's CD back what I'm gonna do now is reload appache and if everything's gone correctly our websites actually gonna be up and running and we'll be able to visit it we don't need to run any Python scripts we should actually be good so I'm gonna say sudo service and in this case Apache to restart this will restart the Apache web server which hopefully should allow us now to configure these changes and then access the web sites we're gonna hit enter no errors that's always a good sign so let's grab our IP address from lenôtre ghin and now notice I'm on my main computer I've gone out of here I'm gonna type this in and when I go to this page and I hit enter you can see we are directed to our flask application so that is essentially how this works this is how you host the website this is how we get it up now I know I didn't talk about domain register or anything about I'm not gonna go into this because he deals with DNS and there's all this complicated stuff and I'm not an expert on getting a domain name up here but if you are interested in learning how to do the domain stuff there is tons of tutorials online that can probably teach you to do this and now that you have your web application set up and running all you need to do is just change it so it runs off of your actual domain so anyways that has been interesting a flask application I hope you guys enjoyed this video and if you did make sure you leave a like as always if you have any questions leave them down below and yeah this is you know as easy as it is to get your application up and running on a website
Info
Channel: Tech With Tim
Views: 92,371
Rating: undefined out of 5
Keywords: tech with tim, how to deploy a flask app, deploy flask app, deploy flask app linux, deploy flask app ubuntu, deploy flask app on ubuntu server, deploy flask app on apache ubuntu, flask app deployment, flask app on linux server
Id: YFBRVJPhDGY
Channel Id: undefined
Length: 22min 12sec (1332 seconds)
Published: Thu Nov 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.