Deploy a Django web app with Nginx to Amazon EC2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to my next video tutorial which will be on deploying a Django web app was Amazon ec2 nginx and G unicorn all right so make sure that you're logged into your AWS account so there is a prerequisite for this tutorial you need to at least have some basic understanding of AWS so make sure that you have some idea the next thing that you need to ensure of course is that you're logged into your AWS account um also make sure that you have a repository that you want to clone because we will be cloning um a project here so I have a sample project here as you can see here which I'll be cloning um within our Amazon ec2 instance so basically you need a GitHub repo set and ready and you also need to be signed into AWS so make sure that you've got that set okay so the next thing you want to do is we want to head over to ec2 so you can type ec2 here so for those of you that are wondering so ec2 is probably the most popular service on AWS which allows us to provision virtual servers in the cloud known as ec2 instances so we can go ahead and click on ec2 okay so let's click on that all right so let's just wait for the dashboard to refresh itself and the first thing that we want to do is create a security group so a security group is basically a virtual firewall okay that consists of inbound rules and outbound rules and these inbound and outbound rules determine how internet traffic um is routed Inward and outward in terms of security principles okay and what the what internet traffic is basically constrained to as well all right so let's go ahead and create our Security Group so first of all you want to give your Security Group a name I'm going to call it my SG and you'll want to add a description as well it used to be optional but now it's mandatory so what I do here is I just put it in again my security group sorry my security group name so go ahead and add in the security group name in the description you can add anything that you want and of course a bit more information here about our security group so as I said it's a virtual firewall for our ec2 instance and it will control inbound and outbound traffic great so let's scroll down so as you can see here we have our inbound and outbound rules now the outbound rules is of course correlating to traffic going outward now this should be all traffic it wouldn't make sense to have strict rules on this unless we're working with a VPS or um making use of anything well with the private VPC Etc so let's just leave this as is all right so in terms of the inbound rules we need to set up three inbound rules so the first inbound rule is going to be based on SSH so let's look for SSH okay and SSH is going to operate under Port 22 and we want to allow ipv4 addresses to connect to SSH from Port 22 and I and ipv4 addresses are designated by the following side or block so essentially this is going to allow us to SSH as the term is called into our ec2 instances so we will be able to programmatically access our servers by using SSH so this is very important so make sure you have this connected next you want to add a to add a rule called HTTP so the rule is going to be based on the type of http and we're going to actually add two of them and I'll explain now why okay so we need two inbound rules here for HTTP and HTTP operates under Port 80 and we want to allow inbound traffic from all ipv4 addresses which is designated by the following cider block and from all IPv6 addresses which is designated by the following cider block okay so we call these here ipv4 and IPv6 addresses and it's in a format known as a cider block okay and basically like I said we want to connect to our application on HTTP on Port 80 from either an ipv4 or an IPv6 address and we want to programmatically access our ec2 instance so essentially we want to SSH into our instance and configure it with SSH on Port 22 from our ipv4 address okay so make sure you've got these three inbound rules set and ready to go and also double check that your security group name is in place along with the description this is mandatory and that's all you need so make sure you've configured everything and once you're happy with that you can scroll down and say create Security Group okay there we go so our security group is created now for those of you that don't have great memory I'd recommend just writing this down so um it depends what you called your Security Group I just called it mysg so it'll be quite easy to remember so make sure you know your Security Group great so we can now head on back to the ec2 dashboard and what we want to do now is create our first ec2 instance so here we have instances running and that is zero so we can go ahead and click on instances okay and here on the right you can see a orange button here that says launch instances so you want to go ahead and click launch instances okay it just takes a moment excuse me and let's get started so first of all you want to name your server so I'm going to call this my server I want to keep it very simple very easy so I'm just going to call it my server next we need to choose a base OS image or a Ami or Amazon machine image so this is our operating system essentially and we're going to use Ubuntu so make sure you don't use anything else make sure you use Ubuntu because of the configurations that I'll show you soon enough and we're going to use the free tier eligible so Ubuntu the version 22.04 LTS long term support then you can scroll down and under instance type you want to choose T2 micro which is on the free tier eligible so you'll automatically be selected for you you just have to double check all right next we want to scroll down and here under key pair we don't want to log in with a key pair for now so we're just going to click here and say proceed without a key pair we don't need to do this because I'll show you a better way of sshing into our ec2 instance next you want to scroll down and here under firewalls security groups we don't need to create one because we just configured a security group so you can say select existing Security Group and here you want to select your security group and the one that I created just now is called mysg so select your security group and you can scroll all the way down and here on the right here under summary you just want to check everything and make sure so we are creating an ec2 instance that makes use of the Ubuntu Ami or base OS image we are using the t2 micro uh virtual server type or instance type the firewall is mysg and that's the main things that you need to bear in mind so once you have feels that you can go ahead and say launch instance okay so it's just uh launching the internet there we go anything's been launched you can click on instances now okay and I have mine pending so don't worry about the one from earlier I'm just doing some testing so here's the one that we are waiting for so it's currently pending so let's wait for it to turn into the running state that should be fast and then what I'm going to have to do is once it's in the running State oh there we go so here is our server as of this moment so it's running but the status checks are still initializing now you can skip ahead and move on but I would advise you to wait still until the status checks have been passed so you'll see here two out of two of the green check mark um saying that it has passed okay so we want to make sure that our instance is completely configured without any issues so I would recommend just waiting for about five to ten minutes until the status check has passed and once it's been completed I'll show you what it looks like once it's done and then we can move on with the next set of steps so make sure that you've got this all set and into place and then we'll move on to the next steps alright so let's just wait for the status check right so you should now see here if you refresh your dashboard here you should see now that the status checks have now been passed two out of two and that your instance is still running which is great so what we want to do now is just click on the check mark here for our instance and we just want to just double check everything's okay so we're using the Ubuntu platform and my Ami is based on Ubuntu the Ubuntu image great asset so just double check that right so now we can click on this instance ID here okay and do bear in mind your public ipv4 address but don't worry you'll see this again soon once you connect so we are now going to connect to our ec2 instance programmatically you could say so you can say connect and we're going to make use of the Amazon ec2 instance connect service and we can go ahead now and connect to our service okay now do bear in mind here that it opens another Tab and that it's going to connect to but keep in mind your public IP address here this is our ipv4 address which we will connect to soon enough all right so we can now see that we are now connected to our ec2 instance now the reason that we're able to connect to it is because remember that inbound rule that we created early on which was to allow us to SSH on Port 22 okay ec2 instance connect Works based on that EC on that SSH inbound rule that we configured so now we have access to our ec2 instance now do keep in mind that the other two inbound rules that we defined okay will allow our users to connect to our application itself on 480 via ipvule 4 and IPv6 addresses okay so I just wanted to make that a bit clearer great so I'm going to say clear and I just want to zoom in a bit here for you guys that should do okay so just a few commands here so if you say clear that's just of course going to clear the console usually I type CLS but since we are making use of Linux we need to say clear okay maybe I can zoom in a bit more uh that yeah that might be better okay so the first thing that we need to do is we need to obtain information about the latest package versions that are available for our Ubuntu system so what you want to do is type in sudo space apt Dash get update okay so we want to see all the available updates available okay so let's see so it's just reading for us done okay so everything has been transcribed the next thing that we want to do is download and upgrade all of these available packages so I'm just going to type clear and here I'm going to say sudo space apt Dash get upgrade so now we're actually going to upgrade our system and what you want to do just want to zoom out here what you want to do now is you just want to press yes so that will be designated by a y and you can press enter so now it's going to install and set everything up now you will see a few prompts will come up here about um the demon being the demon being updated or upgraded so don't worry about that I will explain in a moment so there we go now you'll see this prompt a few times so saying demons are using outdated libraries so you don't need to worry too much about this all you need to do is press enter on your keyboard now you're going to see this a few times all you need to do is press enter on your keyboard you don't need to do anything just press enter that's all you need to do okay perfect so that's all set up now let's type clear and the next thing we want to do is just check the current python version so here I'm just going to say Python 3 dash dash version okay Python 3.10.6 and that's a pretty good python version quite stable okay now the next thing you want to do is we want to create a virtual environment so before we can do that we need to install the pi sensory virtual environment package so we're going to install um essentially the PIP install virtual EnV package so the package for creating virtual environments we're not going to create the virtual environment yet we just want to install the package so we can say sudo space apt-get install Python 3 Dash v e n v okay so sudo at Dash get install python3-vnv so we can go ahead and install the python virtual environment package not set it up just install the package and you can press enter perfect and you now want to press yes for y and it's going to install that package for us and again we'll get a prompt and we can just press enter again on our keyboard and there we go great let's say clear and the next thing that we want to do is we simply want to type in the following command and that's going to be LS okay so we can see we've got nothing here just yet so let's Carry On Now what we can do is we can create our own virtual environment so I'm going to say python three space dash n space and I'm going to say venv and I'm going to give my virtual environment a name so I'm going to call it eenv for in Virtual environment so or environment so now you're creating your own virtual environment now so previously we installed the package now we're actually going to use that package and create our own virtual environment so once the app use that you can press enter okay so just take a moment there we go now let's activate our virtual environment so we're going to say source EnV forward slash bin forward slash activate so go ahead and enter in the following commands and we're going to activate our virtual environment that we get so you can now see here on the left that our virtual environment is now active and that we are within it so while we was in it we can install our Django package so we can say pip3 install Django there we go so just only Django and all the sub packages associated with it perfect let's type clear there we go now we want to clone our Django project so I have my sample application here that I want to go ahead and clone so what I'm going to do here is I'm going to type git clone and I'm going to copy my project here the URL copy and I'm going to right click and I'm going to say paste then next to it I just want to put at the end dot get and space so I'm going to say get clone I copied my URL from my project and then I just entered on at the end of it dot get at the end so I'm going to clone my Django project right here so once you're happy with that go ahead and press enter so it's cloning the project there we go perfect all right so let's go ahead and type in LS and now we can see we have our virtual environment and we have our Django project here perfect so let's clear this up Okay so let's make it clearer here for you guys so the next thing we need to do is install nginx so I'm going to say Suda apt get install why nginx so this command is going to allow us to install nginx on our system so sudo apt get install Dash y nginx okay so let's install nginx perfect okay so you'll see this prompt again just press enter again there we go so I just want to close that right there we go all right so now what we can do is type in clear and there we go right at the top okay so let's just clear this up a bit here and just make it a bit clearer for you guys okay so what we want to do now is we want to install G unicorn now G unicorn is used for allowing our Django application to communicate with our web server and hence Bridge the connection so we can say pip install a g unicorn or I don't know how it's actually pronounced but there we have it unicorn has been installed and if you want to validate this you can type in PIP list so as long as you're in your virtual environment if you type in PIP list you'll be able to see here all the packages that are installed in your virtual environment so it's a good validation method here all right okay so what we can essentially do next is we can connect to our ec2 instances ipv4 address so that's the next thing we want to go ahead and do right now to do this you can head on back to the previous tab and here on the public IP address you can copy that and you can just paste it in your URL and press enter and there we go we can now see that nginx has been installed on our system perfect so we can now see it's also and ready to go let's head over back to ec2 instance connect and the next thing that we want to do is we want to install and set up supervisor now supervisor is going to ensure that our application keeps on rather than running smoothly in the background so let's go ahead and install supervisor so to do that you want to type in sudo apt-get install supervisor okay so sudo app dash get install supervisor and press enter okay okay you get the prompt again just press enter and let's type in clear and clear this up there we go all right so next we will go ahead and create a configuration file that will allow supervisor to carry out its processes effectively so to do that we first need to head over to the supervisor directory as follows so that will be at CD forward slash Etc forward slash supervisor forward slash comp dot d forward slash so pay now are the following commands so CD space forward slash Etc forward slash supervisor forward slash conf dot d forward slash and press enter great so now within that directory we can go ahead and create our g-unicorn configuration file so to do that you need to say sudo touch G unicorn or goonicorn Dot comf and that will create that gunicon.com file for us and in order to edit or modify our configuration file we can then make use of the Nano command so we can say sudo Nano G unicorn unicorn.com and enter so here we are within our G unicorn file here junicorn.com file so what we need to do is edit this configuration file so what I'm going to do is pin the code um in the description no I'll pin the code in the comments section below and you can simply copy and modify the code according to your project the only thing that you'll need to do is you're going to have to modify your project name so I have my project here so this is my G unicorn file here and I will zoom in here for you and explain a bit okay so this is our g-unicron file here that we need to configure now the only difference is as follows so my project name is called Elevate so let's head over here so my project is called Elevate as you can see and I configure Elevate here and I configure it here as well so that's for my project so obviously these two parts here you need to change with your own project name so if I can zoom in even more yeah you need to change these up here and the next thing you need to make sure you change here is this part here so we need to say how we are connecting our Django application to our web server we're connecting it with G unicorn and we need to specify in our G unicorn file where that connection is coming from in our Django app so here it's from elevate.wsgi now you're probably wondering where do I confirm this now usually it will be your project but if you want confirmation head over to your project settings.pi file so I'm heading over to the settings.pi file and you want to look for wsgi so here's wsgi and here you can see if I zoom in a bit here you can see here mine is elevate.wsgi and that's going to correlate to whatever your G unicorn is here so you have three things you need to change so wherever you see Elevate here you need to put in your project instead everything else needs to be exactly the same because what we're essentially configuring here is a few workers to organize the unicorn and start up the process to allow supervisor to auto start Auto restart and also to configure logging files if there are errors or any kind of issues as well so that's essentially what's going on here so like I said I will attach this to the comment section below so you can go ahead and just modify it so I'm just going to copy this here and I'm going to head over to my Gmail current file here and I'm going to right click and say paste there we go so it's all in place now you're probably wondering how do I save this so to save it you just need to say control and uh so once you say Ctrl o it's going to say file name to write G unicorn.conf that's perfect you can just go ahead and press enter there we go so it's been saved now the only thing you need to do now is you need to exit from here so you can just say control hold Ctrl and say X and there you go now if you want to go back in there you can say sudo Nano G unicorn .com and you're back in there okay so just keep that in mind so what you can do now is you can just go ahead and say control X and you're out okay so just remember those commands perfect the next thing we need to do is create a log directory now which is going to append those files that we saw in g-unicron to alert us of errors or if we have any errors or any issues so we can just create a directory here and we're going to say sudo MK der which means make directory space VAR forward slash log forward slash g-un corn okay so you want to go ahead and add in the following right there so sudo make directory for slash VAR forward slash log forward slash GE unicorn and once you're happy you can press enter perfect we must then tell supervisor to read from our configuration file so from our genocrine file so we can do this by saying sudo supervisor and we need to type in CTL so remember supervisor the CTL space re-read and we can now see that g uni is available so Junior confined file is available now what you can do is you can go ahead and tell supervisor now to start G unicorn in the background so to use this you can say sudo supervisor CTL and you can go ahead and say update okay and go ahead and say update there we go so now it's added a process group for us now we want to check the status and see if everything's running properly and without any problems now to do that you need to say sudo supervisor CTL and you want to say status and there we go we can now see it's running make sure that you don't see starting or back off or failure that usually indicates that you've configured your genocron file wrong so make sure that you see running and you can go ahead and type it again if you want for pseudo supervisor CTL status and it's running great and you can see the uptime perfect so you've got everything configured here for your G unicorn.com file perfect well done so let's clear this up and we can go ahead and move on to the next set of steps right so the next thing we need to do is CDL of our current directory until we are within this Etc directory here so we're in Etc supervisor.com in this directory and we just want to go out of it until we are in the ETC part so we can type CD and add a space and we can say point point or dot dot okay so now in supervisor let's go back one more level so CD space dot dot and there we go we are now in ETC all right let's type clear and make sure you're in the following directory here perfect now we want to type LS okay so that gives us a lot of directories here now we want to head over to the nginx directory as you can see here on my screen we want to head over to nginx so here at the bottom let's just clear this for the moment and now that we know we want to go to nginx we can say CD nginx perfect now let's type Alice and reveal the files okay so this is a bit clearer we want to edit the nginx.com file so we want to edit our nginx configuration settings and to do that we can just say sudo Nano nginx.com so remember Nano is for editing or modifying a file and we want to edit or modify nginx.com go ahead and press enter great and the only thing we want to do here is we want to edit the user so as of this moment the user is www.data this is incorrect we can just say root so I'm going to go ahead and just backspace all of that and say root okay that's perfectly fine okay we can just say root you can of course configure this to another user if you wish I just feel Roo is a good way to go so there are many ways to do it this isn't just a one-way room but you can reference to other names if you wish but I just want to keep it brief and simple for you guys okay so that's perfectly fine so remember if we want to edit a file we need to press Ctrl and O now it's going to say file name to write nginx.conf perfect go ahead and press enter saved now we can go ahead and press Ctrl and X at the same time and there we go so we've configured now our nginx com file and the reason we added that user to be the root user is because we don't want to have any permission denied errors so we need to go ahead and set a user with permissions and by default the root user will have complete permissions so we won't have any issues once we go ahead and make additional changes and settings all right we can now type clear and then we can just press enter and there we go perfect next we need to create a configuration file for our Django application within the sites available directory now if we type Alison again we can now see we have this site available directory here so we just want to see the internet directory so we can just say CD sites Dash available so make sure you spell it correctly here and go ahead and click on that perfect now we can just go ahead and create our configuration file known as our django.conf and then modify it with Nano so let's create that Django Kung Fu configuration file so we can just type in sudo touch django.com create that file perfect now let's say sudo nanojango.conf and now we're going to modify it or edit that file per se perfect now yet again I do have a django.com configuration here so it's going to be very simple okay let me zoom in for you here now don't worry I'll also put this in the comment section of this video so this will be roughly the django.com configuration file at your youth ojango.com so the only thing you need to add in here is your server name and of course you need to just put in your project name here so mine is called Elevate okay so as you can see here and my project is elevate or the project name per se great so let me just modify my settings in here so I need to get my server name so in this case we're just going to use our public ipv4 address here I'm going to copy that okay and I'm just going to head into django.com and paste the server name here perfect and that's all I really need that's pretty much uh just about it so let's go ahead and copy all of this head over to ec2connect and here is the folder so this will be django.conf right click say paste and you may want to scroll up and there we have it all in place so essentially what's going to happen now is we'll be able to connect to our Django application okay on the on Port 80 and the server name well um technically our ipv4 address we're going to be connecting right here okay so make sure you've got the in place now technically if you wanted to make use of https technically you would listen on Port 443 and server name you put in your domain name such as www dot or um your website.com Etc so just keep that in mind but we would just want to keep something simple here so we don't overdo it so that's all you need to do and once you're happy with that you can press Ctrl uh o at the same time and we want to write file name to write django.com for yes you can press enter and what you can do now is you can go ahead and just press Ctrl X and there we go if you want to double check your configuration head back over to pseudo nanojango.com and you can just have a look at your configuration see if you're happy with it like I said don't worry I will attach um the necessary code in the comments down below so don't worry okay so once you're happy with that control X out and there we go so now you can just say clear perfect so we've got everything set and ready to go there the next thing we need to do is actually test our configuration we want to make sure that everything is working as it should so to do that you can type in sudo nginx Dash t all right so we can see here our nginx the configure our nginx.com for nginx configuration file syntax is okay perfect and we can also see that the test also was successful of this nginx.com file perfect so what we want to do now is go ahead and run the following command so that will be sudo Ln okay ajango.conf okay and you can go ahead and add a space and then you want to say Etc nginx forward slash sites Dash enabled so essentially what we're doing here is we are going ahead and enabling our site we want to get our site live so we're just putting the final command in place and you can go ahead and just press enter so sudo Ln django.com space forward slash Etc forward slash nginx forward slash sites enabled and you can go ahead and press enter perfect now the last thing you want to do is say sudo service nginx restart so you just want to restart the service get everything ready and set and ready to go and once you're done with that you can go ahead and refresh your page and there we go so this is my application a very simple one and we can see we're connecting to the following ifpv4 address and there we go we've successfully deployed our application with nginx g-unicorn and we've also used to supervisor service in the background to keep it running now a question I got in the comments um a on my last deployment to Amazon ec2 was the fact that they wanted their application to keep on running so in this case because we made use of G unicorn nginx and supervisor especially that allows us to technically go ahead and close this ec2 instance connect console we don't need to keep this running anymore so if I were to close this console here okay and let me just head over to my instances page here for example and what you'll notice is let's just wait for it to refresh here if we head on back we can see our application is still live it's not breaking or anything because we have processes running in the background thanks to supervisor so that is something that we configured early on but yeah that's it guys that is a simple way in which you can deploy your Django web application with nginx to unicorn and make use of supervisor to just keep your application running even though that you you are not within your application itself within the how can I call it you're not keeping it manually up and running it is running in the background with all the processes okay of course if you delete your instance your website will be down but that's kind of common sense but the point is you don't need to keep your terminal running based on a command it will automatically run um for you so you don't need to worry about that okay guys so that's it on deployment on Amazon ec2 with nginx and G unicorn so I hope to help you and of course there's a lot more things you can do of course you can go ahead and add in inbound rules for https and you may need to configure a few things register your domain name but this at least will give you a good entry point all you really need to do if you want to connect the domain name is you just need to change your server name to your domain name change your Port um to Port 443 usually and you should be good to go of course there will be probably a few more things you need to add and manipulate but I'm sure you guys can figure that out but anyway this is it guys and of course as always thank you for the support and until next time [Music] foreign
Info
Channel: Cloud With Django
Views: 32,117
Rating: undefined out of 5
Keywords: Cloud With Django, CWD, cloudwithdjango, Deploy a Django app with Nginx, Django, Deploy a Django web app, Deploy a Django web app with Nginx to Amazon EC2, Nginx, Deployment, Live server, Django deploy, Deploy django, django developer, Development, Django developer, Amazon EC2, EC2, Amazon EC2 Instance, Django with Nginx, Apache, Learn Django, Basics of Django, Learn AWS, Learn the basics of Django, Learn the basics of AWS, Deploy, Live application, Django for beginner, Python deploy
Id: 7O1H9kr1CsA
Channel Id: undefined
Length: 35min 26sec (2126 seconds)
Published: Thu Feb 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.