How to Deploy Python Django Application to a Linux Cloud Server (One Hour Django Project Part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys what's going on so in today's video I wanted to pick up on where we actually left off last week and last week what we did is we created this really cool little shortening go or you're off shortly Django application within one hour and the whole point of this is to put in a long URL address and to get something shortened based on your custom domain or however you want to set that up now we built that on our local server within an hour so I got some questions since then and that is how do you actually put this on a cloud servers like blue node or digital ocean or vulture or something along those lines so in today's video I'm gonna be walking through some of that stuff now what I want to focus on today are our learning objectives at least for today's gonna be we're gonna focus on how to set up a Linux server you can use a little annoyed or digitalocean I'm gonna personally be using the node we're gonna install the relevant dependencies we're gonna walk through some of the minor edits that we need to make within our project then we're gonna transfer our Django project over to our server I'm gonna show you how to do that as well we're gonna be using G unicorn and nginx you can also use Apache there's the different other types of frameworks you can use but I prefer G unicorn and nginx just because I'm used to it we're going to test this in development so we're gonna go ahead and do something called server Gina corn application in development on our virtual server and then we're gonna go ahead and test it in production now I'm going to talk a little bit about SSL and security and DDoS but we're gonna be doing more of that in the future and then you're gonna be wrapping up so that is sort of the action plan for today so what I did is I created this page or github that will walk you through how to do it on almost any Linux server for that matter and as long as you follow these instructions you should be able to get your django code up and running but we're gonna actually do this and we're gonna build it out from the start I'm actually gonna be using the node today though you can do this on digitalocean I've done it on that as well and it works just fine so let's go ahead and start deploying this I also want to take this opportunity to talk about levers not AI what levers not AI is it provides a skills gap for people within organizations or people that may be this place in capabilities that are upcoming in the future like AI machine learning and software development approximately 800 million jobs that are gonna be displaced in which case there's gonna be a lot of upscaling that's going to be required and if you want to get ahead of the curve and learn a little bit more about some upskilling possibilities check out levers dot ai and also don't forget to sign up for the newsletter for some awesome and exclusive content so just as a reminder this was the code and the breakup of the URL shortener application and again if you haven't seen that video it's almost important that you watch that video first otherwise a lot of this is not going to make sense today so just as a refresher we built the views dot pi the whole intent of this file or this project was the ability to be able to take in a long URL and make a custom shortener for yourself right now the service ID that or the service location we're using is just something like localhost but what we're gonna do today is we're gonna change this to the IP address that the live node server gives us and we're gonna deploy it make a couple more modifications to the settings PI file that are important and then we'll just take it from there and so let's go step-by-step and get this deployed so now for those of you who are new to any kind of cloud platform this is basically what the load node dashboard looks like I've got a temp server running up here that I put up yesterday just to test some other code out so I'm gonna be deleting this anyways but we're gonna go ahead and create a brand-new Lenovo they call it a node which is the same thing as a linux server from scratch so if you're using one node what I like to use is a bun too and I'm going to use a bun to nineteen point ten for this project we're gonna use an anode which is five dollars a month and we are going to call this test server and then finally we're just getting given a password for root now if you guys are new to the node I will put a link in the description for you guys to check it out I believe you get some some free credits with that as well so check it out you can use that you can use digitalocean I'll put some links down below so you guys can get started with some some free credits so that you can try this out yourselves now after we do that we're not gonna worry about SSH keys just yet the backup option is really really good if you're gonna be using something on a permanent basis so if you've got a permanent server you always want to use this backup option because it does automatic for you and you there's there's gonna be times we're gonna goof up your Linux server I've been having that option is just really really important but for now the way this works is this is gonna bill me up to five dollars a month or whatever my usage is per minute or per second I can't remember how Leno does it but let's create our server oh and I have to pick a region so the region we're gonna pick is we'll just pick Dallas but you may want to pick something that's closer to the audience you're trying to serve so what it's gonna do now is it's gonna start provisioning this it's gonna start basically provisioning it then it's gonna start loading it so I'm gonna fast-forward until this actually gets loaded and then we'll get started again all right so it looks like our test server is ready to go just some hardware specifics this is a one gigabyte one CPU 256 storage one gigabyte RAM which is plentiful for the project that we're going to do this is a very important IP address for you to remember because we're gonna use it several times so I'm gonna go ahead and copy this I'm gonna leave this page open just because I'm gonna be referring back to this page several times and I don't want to write a town right now and I don't have a great memory so I'm gonna keep coming back and copying and pasting this but let's get started on some of the steps that we need to get accomplished for this to work so we're gonna be using terminal a lot and that's just the nature of how Linux servers work it's it's a lot easier to work in terminal than it is in anything else but I'm gonna make this bigger so that we take up most of the screen real estate but we can see what's happening here as well so in order for us to log into the server what we want to do is we want to do SSH root at and then that IP address now if you have a Windows computer I'm doing this on a Mac these instructions will work on a Mac and Linux give a Windows computer I believe the tool to use is called putty you can use that and follow along some of the commands might be slightly different a lot of this is written for Linux and Mac but most of them are the same but you can always just do a quick google search to see what the comparable command would be though the directions and these steps are going to be the same so let's login we're gonna say yes to this and that's just this just put say a fingerprint of this on our computer so that we know that this is gonna be associated with our computer this SSH I'm gonna put in my root command or my root password sorry the ones that we set up in the low node and clear this screen and anytime you do anything new the linux computer the first thing you'll want to do is you want to do apt update and apt and then tak yes so what this basically says is let's go ahead and find all the updates within this server then let's upgrade them and i'm gonna automatically authorize it to upgrade everything now you don't have to put sudo in this because we're already in the root folder later when we create a limited user we're gonna start using this su sudo command but let's run this now this takes a little bit of time as well I'm gonna let this run and then as soon as it's done we'll get back to this alright so the other thing we're gonna have to do is we're gonna have to set up our hostname so I'm going to type in hostname CTO then we're gonna type in set hostname and I'm gonna call it test server so the other thing we're gonna have to do is we're gonna have to set our IP with our hostname and sometimes if you forget you or actual IP you can type in host nation hostname tak I and that'll give you your IP address right there and so now we're gonna go and type in now /e TC ford slash hosts and here I'm gonna basically copy and paste that and we called it test server beautiful alright cool so there's a few more things we want to do so first I like to install some of the main dependencies right off the bat so that we're gonna type in sudo apt install I want to install in pip system-wide so that we can use it virtual P&V and then something called ufw which is the uncomplicated firewall and once that gets installed and what we're gonna do is we're gonna log out of our root and then we're gonna go ahead and log back into our user now this should run fairly quickly we want to be able to run some pip commands especially when we get into our username so that's important at the same time I want to run this entire package in a virtual environment just because if I want to run multiple different applications on the same server this is a good way to do it and then obviously I want to make sure that I'm blocking specific ports and enabling specific ports now we're going to be using G unicorn and nginx and so nginx is going to be the forum that's actually going to determine all the ports that need to be open and closed alright so the next thing we're gonna do is before we log out is we need to go ahead and add an another user and we call this a limited user so I'm going to say add user sots and the whole purpose behind this is route with route you have a lot of permissions and you don't want to make any kind of unintended changes so it's always good to add in a limited user and then use sudo whenever you need so it's gonna ask you to put in a new password which I'll do I'm splitting in a random password it's gonna ask all this information this is all optional I generally don't fill this out is this information correct yes then the last thing we want to do is when we want to go add user take that username and then type in sudo and that allows us to give sudo permissions to this username all right so now that's done what we're gonna do now is we're going to go ahead and exit out of this so we'll type in exit and that brings me back to my regular terminal from my local computer and so now we're gonna log in with my username so we're going to type in SSH that's hot that IP address which I forgot already so that's why it's helpful to have this open okay now I'm gonna type in the password that I just created and now I have the ability now you can see that it's actually didn't say root anymore it says my name at localhost perfect let's clear this out and now let's do a few other things now I'm gonna go ahead and give some original or some initial commands for the UFW because I want to open and close certain ports so I'm going to say sudo UFW default allow outgoing we're gonna put this in and you should only have to put this in once so the policy has changed to allow we're gonna type in sudo ufw deny incoming now it's important you put all of these commands in sorry that should be pseudo ufw default deny incoming so then the next one we want to do is we're gonna type in allow SSH so pseudo ufw allow SSH okay that's done remember when we do our Django testing we always do it on port 8000 so for now we're gonna open up Ford 8000 we're gonna close it later and we're gonna tell nginx to do all the port mapping and all that stuff so for now we're gonna open 8000 we're gonna close it later now to enable all of this or gonna type in let me just clear this we're gonna type in sudo ufw enable yes and if you get logged out of your SSH you can just go back and login and now we can check the status sudo ufw status all right so we basically allowed SSH which is port 22 allowed 8000 and that is it so that's basically how this works ok so the next thing we're gonna do is we're gonna fire up another terminal on our local machine this time and what we're gonna do is we're going to go ahead and create some SSH keys so SSH key Jen and we're gonna use 494 0 9 6 for some extra security I'm just gonna leave this as the default I already have these keys I'm gonna say yes to overwrite them for now I'm gonna leave these empty as well I don't need a phrase but if you want you can put one in so it basically says that I've got these essays because that's my private key this is my public key that's my fingerprint and now what this gonna allow us to do is login to our server without having to put in a password again alright so the next thing we need to do is we need to go ahead and push these up to our server so I'm going to type in SSH copy ID that's at that IP address and so now it's going to be using these new keys no matter what so now we're gonna go ahead and try to SSH into it so let's open up that other terminal let's exit out of here and now when I go SSH so it doesn't ask me for our password so now basically it's using SSH keys now having said that I could still log in with a password so the next thing we need to do is we're gonna have to go in and change something in the actual SSH config so we're gonna type in sudo nano et Cie SSH SSH D underscore config all right so here's what we're gonna change we need to go down here and we need to say permit route login that means no so first of all I don't want the route to get in because I don't want anybody logging in if they don't have SSH keys that includes route and then the second thing we need to do is we need to make sure that we type in password authentication is know so that means not only can you not get in as a route but if you're gonna type in a password you can't get in the only way for you to get in is to have SSH keys so we're gonna control X save that yes now this is completely optional but I always like to just do a sudo reboot I just like to reboot my server to make sure if I forgot to enable something or check the status or do whatever this should all do it in the background and then once it's up and running we're gonna log in with our username again and now what we're gonna do is we're gonna start pushing our Django project to the server alright so after giving a couple of minutes for the server to boot we're gonna go back and log in again just by using our ssh command we should be able to log in again without a password which is perfect and again that's extra security because only this computer with these SSH keys can log in and you can always push up another set of SSH keys when you look at it on your server they're actually gonna be something called authorize keys and you can just call it authorized keys - or whatever it is but anyways the next thing now we're gonna do is before we start pushing the project up we're gonna go ahead and create a virtual environment so this is no different than how I've created virtual environments in the past so we're going to type in virtually and V the V E and V and I'm gonna specify that I want it to be Python 3 so it's gonna go ahead and create this so let's just let this go ahead and do its thing alright so when I LS I have a ve env now some people like putting their V and V in the projects I like putting it just right next to my project the only reason why I like doing that is because if I want to port that folder over I don't want to take everything in that virtual environment over with it I'd rather create a new virtual environment on the new computer and sort of take it from there but that's just personal preference okay so the next thing we're gonna do now is I've got my local directory here where my projects it's it's called the ul shortener project what I'm gonna do is I'm gonna type in an SCP command now there's multiple ways to do it there's also a tool called cyberduck if you haven't heard about it it's a tool from Mac and what cyberduck allows you to do is you can go ahead and log in to your server with this and you can use a GUI to push things up no I'm not gonna use cyberduck today but it's very very user friendly you put in the same information you put in your IP address your username password tell it where your SSH keys are sitting and that's it like it'll recognize your SSH keys anyways but that is another way to do it but we're gonna use the good old fashioned linux command of how to do this so now you got to do this from your local server not from your server because we're not from your your cloud server and the reason is is because we're gonna be pushing it from our local server out to the cloud so we're gonna run this on our local server so here's what we're gonna do we're gonna type in SCP R for recursive because we want to recursively move our files over I'm gonna take this path put it over here I'm gonna say that I want to send it to my my cloud server and then I'm gonna specify that I wanted to sit in my main directory or my root directory so once I do that as you can see it's starting to now send all of my files over I don't want to send my ve and V it's just sending over my Django files so when I hit clear let's go back to our actual server on LS now you see that I have my V env and my URL project okay so the next thing we need to do is I need to go ahead and log into my virtual environment because I want to do everything moving forward in my virtual environment so in order to do that I'm going to type in source V env Ben activate and now what we want to do is I want to go ahead and install all of my dependencies so for that I have all my requirements text in my URL shortener app or shortener directory so when I LS here you see that I have a requirement text to do that I'm just gonna go pip install or cursive Lee requirements txt now it's gonna install almost everything with the exception of G unicorn and the reason why G unicorn isn't in this is because we never use G or a unicorn in our test environment on our local server because you don't need it what we're gonna do now is we're just gonna go ahead and install G in a corn so pip install G unicorn well that's it so now Gina corns in there so if I do a freeze on this you'll notice that it has all the dependencies that we need we need django Django rest framework - unicorn you are a live three requests and all that so it has everything I believe that we need although I just realized that it's actually missing it's missing Django bootstrap 4 and I know why that's actually missing and that's because I use a round quest file but that's okay the one that is on github is a correct one it has all the information and the right dependencies in there sewed Django bootstrap 4 and then also Django krispies forms cool so all of that stuff is there which is great and just to show you let me just quickly go ahead and pull up the project that's on github so when I bring this over it's got all the right dependencies in there so it's got Django for us framework it's got Krispies forms bootstrap for I wonder if I'm missing anything else beautiful soup for we didn't really use it but I guess I had it in there for whatever reason doesn't hurt to put it in but I'm not going to put anyway it's the one that's on github has everything that you need which is perfect okay so now let's go ahead and make some changes to our settings dot PI file alright so we're gonna go LS because I need to know where I am so I'm gonna type in CD and basically I've logged into my ul shortener project then I'm gonna LS into that and now I'm gonna type in sudo nano settings dot pi so there's a few changes we're gonna have to make here one is I need to go ahead and change the allowed host so remember when we have our loud host right now it doesn't it doesn't recognize anybody to have access to this Django project so we're gonna create a string and in that string we're gonna copy and paste our IP address so that's the one thing I need a change the second thing I need a change and this is also in the Django docks and also in the digital ocean and low note docks as well but you have to put in a static route and the static route is basically there so that it can host all your images because in development it works a different way in production you actually have to specify the location for all your files so we're gonna type in static route OS path dot join and I'm basically saying put this in my my baster file that's where I want it to be you'll see it show up in just a second so let's close that up and that's pretty much I believe all you need to do yes it is all you need to do for this to work now what this is going to do is we're gonna run a command called collec stat in a second but after that you're gonna see that there's gonna be a static folder that's gonna show up so let's get out of this save it yes we're gonna CD out of here and so right now you don't see anything called static over here but now I'm going to write run Python managed up hi collect static oh and if you notice I made a mistake that should say path not whatever that is so let's go back and change that so this isn't remember this is all learning even some of the most experienced developers are gonna go through some of these challenges so if you ever get frustrated and you're saying to yourself I just can't figure it out you're not alone everybody goes through it you just got to keep at it so we're gonna go into sudo nano sorry and let's go ahead and change that and at the very bottom we'll change it back to path sorry I should have caught that but that's okay save it again we're gonna CD one backup because I need to access the managed PI so we're gonna type in that command again Python manage that PI collect static and now what you're gonna see when i LS you're gonna see there's an actual static folder now which wasn't there and if we go into this you'll notice that it's basically all of my images that I had in there so now it knows where to look for when it's looking for images so that's just what that does okay the next thing we're gonna do is we're gonna go ahead and install nginx so I'm gonna type in sudo apt and I want this to be system-wide so I'm okay to do it this way and not in the virtual environment which is why I'm running up instead of Pip and then this is just some other requirements that I forgot to put in and again this is all gonna be in the github Docs so you can just copy and paste so the way that I set it up is it's set up so that you can go and copy and paste it as code and directly into your terminal you're not gonna get any funky errors with it okay so now that's been done the next thing we need to change is remember I need to go ahead and tell my url app so I'm gonna go to my shortening path or or the or the shortening URL so now I'm going to go into sudo nano views PI and remember when it actually shortens the path as you saw in the last directory we were using localhost and then he would say Ford slash whatever that link is gonna be we're gonna put in our IP address here so we're gonna go ahead and do this and that's that so now it's gonna say four five seven 99.1 three fort slash whatever the short name address is okay so that clears that up we're gonna clear this terminal up again now the next thing I want to do is I want to see if I can actually bind the G unicorn to run my project using the whiskey file which is in my settings folder all right so now we're gonna try binding this we're gonna type in G unicorn bind 0.0.0.0 40,000 and I'm just gonna copy and paste this from my github page just because I don't want to miss bail URL shortener for some reason I always do that and then we're gonna hit enter so now what we're gonna do is we're gonna go to our actual URL page which is right here and it's basically if this works it's open this on port 8000 for us so let's go ahead and open up another instance of this now if I just run it regularly without a port number you notice it's just gonna hang it's not gonna do anything because remember in our ufw originally we basically said block everything other than 40-thousand and so now you'll see that it actually runs on port 8000 you're gonna notice a few things interesting is that because I'm running this again in a development like environment using the G unicorn it's not going to display any of the images but rest assured we saw in the static folder the images aren't there so when we actually put this into production using G unicorn they will all show up the other thing is you'll notice here that it says not secure and that's because we're using HTTP in a future video I'm going to show you how we're going to be using HTTP by giving this an SSL certificate and also looking at ways to minimize any kind of of ddos attacks as well but that's for a next video so let's keep moving forward because we want to make sure that we can get this working not on a port 8000 like this we want to make sure that we actually have it set up in a production standpoint so let's go ahead and get out of this we're gonna kill that and once I kill it you'll notice that it dies out so it doesn't work anymore okay so the next thing we need to do is we're gonna get out of our virtual environment so I'm going to type in deactivate and you'll notice I'm no longer in my virtual environment and for those of you who are wondering whether you're in one or not if you have the V env in front with the brackets you're in a virtual environment otherwise you're not so let's clear out of this okay so the next thing we want to do now is we want to create our system MD file for jewnicorn so for that we're gonna type in sudo nano et Cie system D system /g Unicorn dot service okay so we're gonna go ahead and copy and paste this code in my github page this is very similar to what's on digitalocean on the node and you're just gonna go ahead and update your username here you can also use like variables to do this as well I'm not doing it in this case I probably should have now that I'm thinking about it but that's okay but you're gonna go ahead and update your username and your project name and if you want you can create variables here say username and project name and just have them as variables across this but for some reason I don't know why it into it but it is what it is and we're gonna go ahead and change this to username as well and you're gonna put your youõve project here here and here or whatever your project name is and those three locations so the next thing we're gonna do is we're gonna go ahead and enable this and then test to make sure that it's working so system CTO start gee unicorn okay then we're gonna go ahead and do enable G unicorn okay perfect then we're gonna check the status of G unicorn okay so it looks like Gina corns working everything seems to be good so we're good from that perspective we're gonna go ahead and reload the daemon so we're gonna do sudo I'll just do this and then type in dae daemon really reload and then finally restart unicorn all right and once that's been done you can check the status again if you want looks like all is working so we're good so G unicorn recognizes your project that's able to find so was able to find all the relative files to host your project okay so the next thing we're gonna do is we're gonna set up nginx to work with G in a corner so here's what we're gonna do all your nginx files are gonna be in one folder and that's going to be sudo nano it's gonna be an et Cie port slash nginx board slash sites available and then we're gonna call it the URL shortener project and again I'm copying and pasting it because for some reason I always goof up how to spell my own project name but it is what it is okay so now this we're gonna again go back to github we're gonna copy and paste this now remember this is only serving your HTTP traffic on port 80 later in a future video like I said we're gonna go ahead and make sure that we add in HTTPS as well so it's gonna open up port 443 but for now let's just do it on port 80 so again I need to go back and get the IP address here because there's a few things I'm gonna have to update so right now server name says you can put in your IP but if you also have a host name you can put that here as well like fun comm or whatever it may be or Leivers that AI or whatever it is then you're gonna go ahead and update this again and again you can use variables as well but I didn't and the last thing you're gonna do is you're gonna update the username here you can update your project name one two and that's it so we should be good I please let me just a little check yeah okay so we're gonna exit out of this save yes done okay so now we're gonna link and test the nginx configuration so I'm just gonna copy and paste this rather than typing this entire thing out but it's basically this command I'm gonna hit enter and now we've linked everything again on github check it out now you want to check to see whether or not your configuration is fine so sudo engine X T so it says my configuration syntax is okay and the test was looks successful if not it will give you the error code and you can look it up now we want to do is we want to go ahead and change those firewall rules so I'm going to type in sudo ufw delete allow 8000 so I no longer want traffic to go over port 8000 I want nginx to do all of that for me so sudo ufw allow and we're gonna put this in quotes nginx full so it's gonna full permission to do this that's done now we're gonna do sudo system CTL restart gee unicorn and then finally we're gonna do the same thing with nginx and that should be it so now we're gonna go to our server and I'm not gonna put in port 8000 I'm just gonna put in my IP address so don't have my server copied there just gonna put in my IP address and what its gonna do is it should automatically direct me to the shortened URL path and then actually host all of my files as well so now this is actually on the public Internet you were to go to for or 5.7 99.1 3 you'll be able to use this and it's fully functional as well so if we go HTTP I don't know Rogers com it's gonna say okay so this is the URL path we're gonna open up a new file here or a new tab type that in boom takes me to Rogers comm and again if I put Rogers comm in there again it's gonna say the record already exists let's try one more so I'm gonna go to HTTP Leivers da di we're gonna go ahead and try this as well and this is gonna board me to the HTTP site for Leivers dad III which as you can see it's secured that's perfect and if I go ahead and try to put that in there again so if I type in HTTPS Leivers da di it's gonna say it already exists so everything's working just fine you can go ahead and update all these links yourself but that is how you take your project and you host it up on to the cloud what we're gonna do in the next video is like I said I'm going to show you a method of how to enable some kind of DDoS protection so you don't get a denial of service at the same time we're gonna add in it let's encrypt free SSL certificate to work with this as well so that it is gonna be secure so guys hopefully you like this video if you did please consider liking and subscribing and I will see you in the next video until then stay safe and see you next time bye bye [Music]
Info
Channel: SATSifaction
Views: 5,206
Rating: undefined out of 5
Keywords: python, django, linode, django deployment, django deployment nginx, django deployment ubuntu, django web development, how to deploy django, django tutorial, deploy django, web development, django deployment tutorial, digital ocean django, python tutorial, python django tutorial, python django, how to use django, django 3.0 tutorial, django linode, linode django deployment, django linode tutorial, nginx, django nginx, gunicorn, ubuntu, Python Linux tutorial for beginners
Id: hYMTvK5MpQI
Channel Id: undefined
Length: 33min 39sec (2019 seconds)
Published: Sun Apr 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.