Deploy Flask Application on Ubuntu VPS using Nginx

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there my name is anoff and in this video i'm going to show you how you can deploy your flask application on an ubuntu vps using nginx and unicorn so at the moment i don't have a spare vps where i can show you this so i have spun up a ubuntu uh server on virtualbox and the process is going to be the same if you have a vps from volterr aws or google cloud azure dish lotion et cetera so right now i have this really really simple flask application it just renders the index.html file and there's some css so if we run this application to do python3 app.pi now since i'm on linux i'm using python 3. you see this is running here and if i go to google chrome and i type localhost localhost 5000 it's taking me to this flask application it says welcome to my flask app now we have this application but we want to deploy this on an ubuntu server so the first thing we are going to do is we are going to have a vps running now since i'm using a virtualbox vps that's running on my computer here are the credentials for that um vps or vps or that virtual machine which i'm using so i'm gonna open up my terminal where i'm going to use the ssh client to connect to that vps now if you're on windows or something like that you can use putty but since i'm on linux i'm going to be using the normal ssh client that linux comes with so to do that i'm just going to run ssh and the username for my virtual machine or my vps is ubuntu so ubuntu at and then the ip of this vps and if i hit enter it's going to ask me for that password for the vps's password now the password for this i've just set it to one two three four five so i'm just going to enter that password here and if i hit enter it's gonna say that i have connected to the vps so we've successfully connected to that vps now the first thing we need to do is we need to install nginx and we need to set up a reverse proxy for our flask server so the first thing we need to do if this is a fresh vps we need to run apt update to update our package indexes so it's now gonna update uh its package indexes and it's not gonna take a while now what we need to do is we need to install nginx so we need to do sudo apt install nginx and this is now going to start installing the nginx web server so i'm going to hit yes i'm going to hit y and then hit enter and now it's going to start downloading nginx okay so nginx has been installed and now what we need to do is we need to create a configuration for our nginx web server now this configuration will basically allow nginx to set up a reverse proxy for a flask application now you can search online with reverse proxy s but basically the reason we are using a reverse proxy is so that the flask server or the unicorn web server that we're going to be using for this is synchronous and it is really vulnerable to dos or ddos attacks but since nginx and asynchronous um we can use nginx as a reverse proxy as a layer of defense in front of the flash web server so let's create an enginex config and i'm just going to use nano slash etsy nginx slash sites enabled and we're just gonna call it flask app so we have this and now let us actually write our nginx config oops i need to do sudo before this and boom yeah we can now start writing so the first block we are going to need is the server block this is a default engineering server block and we want to listen to port 80 and we want the location to be like anything so by default it's going to be slash and doesn't matter just keep the location slash and it's going to be the same for like oh everything else and we're going to use a proxy underscore pass and then http uh and then the uh host name of the link server so by default the flask application is going to be hosted on the server so it's going to be localhost so 127.0.0.1 and then the port 8000 now the reason we're using a thousand is because we're going to configure the unicorn the wsgi web server to use the port 8000 now we also want a few headers so proxy set header and that is for the host so the ip which is making that request to host and then we also want to um set a forwarded header and that's going to x forward dead 4 and that's going to be proxy underscore add x forward dead 4. i hope my spelling is correct and yes it is correct so i'm now going to save this on nano using control x y and then hit enter now we have this running so to disable nginx's um default configuration we need to unlink or just you know remove that default configuration from the enabled sites so by default if we now go to google chrome and if we type in the ip address of our server if you take a look at this it's going to say just welcome to nginx that's not showing our flask application so we can just type unlink slash xc nginx sites enabled and then we can just remove default the default site which is this welcome to engineering and we need to run pseudo before this okay and boom we have this working now now we can just do sudo engines t to check if our syntax is correct in the entrance configuration and it says unknown directive proxy app proxy app oh yeah forgot we need to actually um proxy add this should be proxy pass why did i type pass i'm sorry not but i type app now we can run this again and it says the syntax test is correct so you can do sudo engine xs reload so this has reloaded now if you refresh it's going to say bad gateway and the reason is that nginx is trying to connect to the local host on the local host bought a thousand and it's not being able to connect there because we don't have a web server running there and for that we are going to be using unicorn for this so let's actually exit out of this and this is our um home directory so we can now put our files our flask application we can transfer that here now for that we need something called an ftp client or an sftp client and the most popular one is filezilla now from windows you can use winscp but i just like filezilla and we can use this to transfer our files so i can just go to documents and this is my flask app so i can just connect to the host now this is going to be my host uh here username is going to be ubuntu password is one two three four five and the port is going to be 22 because i'm using sftp so the default for sftp is 22. if i hit quick connect it's going to ask me if you want to trust this host click yes and then i'm going to click ok it's now going to connect here and boom we have connected to this host now i want to transfer this flask app folder to this um home directory so i can just drag this over here and it's now going to start transferring this and it says all files have been successfully transferred so if we type ls here you can see we have this flask app folder if we go to cd flask app and we can now just nano app.pi and you see we have our app.pi running here now we need to run this with our um unicorn web server but before we do that we need to install pip the package manager for python so we can just run sudo apt install python uh install my bad python 3 pip it's going to install pip and this will take some time [Music] okay so we have successfully installed pip and now we can install all of our dependencies in this requirements.txt file now we have this requirements.txt file here so we can just do pip3 install dash r requirements requirements.txt now if you hit enter this will now start installing flask and any other dependencies that you have in this requirements.txt okay so once we have installed this now what we need to do is we need to install unicorn but before doing that let's actually try and running this flask application so python 3 app.pi and it says this is running so now if we go to this port and 5000 it's going to show that this cannot be connected to and probably you need to um [Music] enable or allow for 5000 so i've allowed this port and if i uh go over here for some reason i can't seem to connect and yeah this is actually normal because it's running on this host and not host zero zero but if we change this to if we change app.pi and change the host to 0.000.0 if we run this now you can see that it's showing welcome to my flask app now this is something that i forgot to do is set the host to 0.0.0 and the reason we set the host to zero or dot zero is what i like to call it is because like it adapts to any ip address where this flask app is being hosted so uh we can't just leave this over here since this is a development server and not good for production deployment because if multiple users try to connect to the server they won't be able to do it so we can stop this and that's why we're going to use unicorn so that other users can connect to the server and to install unicorn we need to do sudo app install unicorn now going to uh ask to install unicorn and press yes okay so we have installed unicorn now and now we can just do unicorn three so this is the default unicorn three or is this oh my bad we need to install uh unicorn three not the normal unicorn so i'm just gonna remove the normal unicorn yes uh my bad so in this video app installed unicorn three and that's going to ask to install the python 3 version of unicorn and now we can just do unicorn 3 um and we're going to uh set multiple workers now the worker nodes are basically like if your application has tons of traffic it's best to have multiple workers and a good number to start off with is always three now we need to specify which file um contains our flask application so we have this app.pi here so you're going to say app colon and then the variable which is our app object so this is our app object the flask underscores per name so uh we're going to do this and if we hit enter you see by default unicorn automatically connects to port 8000 so if we uh go to port 8000 now it's gonna refuse to connect because you see this is now hosting in the local host um host so 127.0.0.1 but if we go to just this nginx server you see it's saying welcome to my flask app now why because you see in our nginx config if you go back a little in our nginx config it is setting a reverse proxy to this localhost port 8000 and since nginx is also being hosted on the same machine it tries to connect to localhost and it is successful hence we are able to connect our flask app and this is actually a good layer of security because this actually like saves this flask application from being ddosed so if you're not refreshed multiple times it's still going to handle it properly and if we refresh and check the network tabs if we go to all and you see the server is running on nginx 1.14.0 on ubuntu so we have successfully now um hosted our flask application on nginx but one thing we need to do is we need to keep unicorn running 24 7. now how do we do that because if we now exit from this ssh this will not run anymore so if i actually uh close this i won't close this now but it's not going to run anymore to do that what we're going to do is when we're running nginx we want to add another flag to our um command so that's going to be demon so what this is going to do is it's going to run unicorn in the background and now unicorns running in the background you don't need to worry about it now if you refresh you see this is now running perfectly so nginx is doing a reverse proxy to the server and if we want to close unicorn that is also very very easy to do so we can just run the uh we can just run top and we can see which service is uh unicorn so which servers unicorns being run with so we can just make this a bit bigger and we'll just scroll down now this is not the best way to do this but there are also other ways you can do it as you can see we have unicorn 3 here but one thing we can do is we can just do sudo p kill dash f unicorn three now this is going to kill every service that is i would that has the name unicorn three so if we run this and now if we refresh it's going to say bad gateway because unicorn is not running so forget about the top command if you need to stop unicorn just do p kill and unicorn is completely fine with this it's not going to crash anything and if you want to run unicorn on the background again just run unicorn three dash dash workers equals three app cooling app with the demon mode now for refresh you see it says welcome to my flask app so that is how you host your flask application on an ubuntu server vps now even if i close this vps session if i exit from this it's still going to run i'm refreshing and this is running perfectly so that is how you host your um flask application using nginx and unicorn and if you enjoyed this video please subscribe to my channel and like this video i really appreciate all the support and yeah that's all for today have a good one bye
Info
Channel: DevGuyAhnaf
Views: 1,662
Rating: undefined out of 5
Keywords: flask, python, nginx, ubuntu, vps, deploy flask app, flask nginx, deploy flask app on ubuntu, ubuntu flask, ubuntu nginx, gunicorn nginx, flask gunicorn, ubuntu gunicorn, nginx flask, python web dev, python deployment, web application deployment, ubuntu server, flask vps, ubuntu vps, python application hosting, host flask app, host flask app with nginx, devguyahnaf, coder's system, ahnaf zamil, nginx reverse proxy
Id: BpcK5jON6Cg
Channel Id: undefined
Length: 16min 18sec (978 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.