Nginx Mastery | Getting Started with Nginx | Docker | Docker Compose

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're thinking about deploying django python  php react the likelihood is that you're going to   need to deploy and configure a web server unlike  anything there are plenty of technologies to   choose from whether you choose nginx apache sun  google cloudflare openresty there are plenty of   web servers that are going to help provide  you achieve your goals of serving your web   service according to statistics nginx continues to  be the most commonly used web server with around   about 32 percent of all sites using it nginx  can be utilized for a range of different needs   whether you're just trying to serve a web  page whether you need reverse proxying caching   load balancing there's a whole host of different  services that nginx can provide let's just take a   quick look at where nginx fits into a general  request response cycle for serving a web page   a user from a browser would make a request  maybe that's a post or a get request for   some sort of web page so this is where nginx  comes into play you can see this is a vital cog   in that cycle of retrieving data from the server  nginx will handle the request from the browser   depending on your implementation you may have  some intermediate server which is going to allow   you to in this case translate or communicate  with python uws gi server so at the back end   you may have django python react php running you  may have some sort of file server and database   and then that data then gets served back  through engine nginx back to the browser   so hopefully this diagram just emphasizes in order  for your application to perform effectively it's   not just going to rely on your application it's  also going to rely potentially on the web server   your ability to configure for high performance  maybe load balancing for security for traffic   management there's a whole heap of information  and setup here potentially that needs to be   considered before you start deploying your  application at scale so with that said   welcome my name is xander welcome to the nginx  mastery series where initially i'm going to   deliver a three-part series uh first of all  getting started with nginx and then we'll move   into nginx architecture in the second tutorial  and then thirdly look at serving static content once we finish this three-part series i would then  continue to deliver content about nginx looking   at very specific areas and aspects managing and  administrating nginx services in this tutorial for   beginners we start by starting an nginx server and  here we're going to be utilizing docker so we get   straight into building and working with nginx you  will need to have docker installed so i'm assuming   you have installed docker desktop if you're new to  docker then just go over to google type in docker   desktop and download that so we'll go through the  basics of setting docker up with nginx and then we   have a look at managing the nginx service  and then finally serving custom web pages   so this is a little bit more detail exactly what  we're going to be covering in this tutorial so   getting started with docker and nginx essentially  that's what we're going to be covering in the   first three tutorials this is the first tutorial  here so we go and start off by pulling the image   down from docker we'll run nginx container and  then we start to look at how we can start and stop   utilizing docker how to verify your installation  make sure everything is working okay then we go   through some basic service management start stop  and have a look at some of the basic commands we   can utilize and then finally we'll then move into  starting to learn a little bit about the nginx   default folder how can we serve web pages directly  from nginx and then we go through and dockerize   that using volumes and then create our own custom  page and then finally build our own nginx image okay so that's the plan for the first  tutorial so let's get started so   like i said if you haven't already got docker  installed you are going to need to install   that i won't take you through that process  simply go to google type in docker desktop   download it for your distribution and then  just go through the installation guide if   you're running windows or mac it literally just  is a case of next next next if you're on linux it   can be a little bit more tricky but there are  some good documentation on the docker website so i have visual studio code open in the terminal  here now you don't need to use visual studio code   for this it's just uh it's just nicely um  centralized everything in the same place   so i've opened up a new folder here the first  thing i need to do to start working with nginx   you don't have to do this of course but here  i'm going to be utilizing docker that makes it   a little bit more palatable for all operating  systems and it can be a bit of a pain in you   won't necessarily want to have to install nginx  on your own machine and obviously that allows us   to contain our eyes engine x and work with  it and we obviously can save the image to   docker and we can then share it with everyone  else it just makes it a lot easier to work i think   in containers rather in our own operating  system so let's get started by pulling down   nginx the official nginx image from docker so if  you are new to docker and images essentially what   we're going to be doing here is utilizing docker  hub so the developers of nginx have created an   image which basically has everything we need to  get started utilizing nginx so essentially what we   need to do is download that image and then start  it in a container which is essentially running   the image and then that inside of that container  will have everything that we need to actually run   the service so essentially it's running  a small computer inside of a container   and in this case we're just going to be  running the nginx service within that container   and that saves us from having to download  install into our own computer and so on   so you can see that we have the official nginx  image here it's got one billion downloads plus so   it's quite popular so we're going to go ahead and  just download that if i click on this image here   this is in docker hub you can see that we're  provided the actual command that we're going   to need to pull that down so let's go ahead and  do that so just place that into there so it's   around about i don't know it's around about 100  meg so that's downloaded i'll go over then to   docker desktop and go into my images and you can  see that i've now downloaded that image and it's   now available so all i really need to do now is to  run this image uh with a few different attributes   parameters pass in a few parameters and  then our nginx server will be running   so let's go ahead and do this now imagine  that you're new to docker here so we use   docker to instruct that we want to use a docker  command and then what we want to do is we want   to run that image that we've just downloaded  okay so we do want some sort of interaction   so it is basically going to instruct docker  to allocate a pseudo tty connection or   essentially it's going to allow  us to have an interactive um   bash shell in the container so we can interact  with our image through so our container   through a shell so i t right so after that  um let me think we're going to need to that's just our m so here rm we may want to run  this command again so this is essentially going   to remove any kind of stopped containers that we  might have or clean up the containers and remove   the file system if the container exists already  okay so next up we want to run this service as a   as a persistent kind of background process so  we're going to use a slash d here for demon   and so we're going to run it as a  demon and then next up let's go for a p so we're going to need to define the ports that we  want to use so first of all we define the port we   want to access it from our own computer so we're  going to use port 8080 you can put use port 8000   if you want let's just go ahead and do that and  then we then want to define what port we want to   access inside of our container so we want to kind  of map that over to the port 80 in the container   because that's where our server is running  by default so if you're not already familiar   with this our web applications tend to run or  the web tends to run on port 80. so therefore   our server needs to be listening on port 80 so  it's expecting information for import 80 so a   http request for example so that then allows us to  interact with our container from our machine 8000   that gets then translated to the container  to port 80. and then we can give it a name   so let's call this name website for example and  then we then want to define the image we want to   use and the image is called nginx so we use that  so that's going to start then our that's going to   then grab this container this image and create a  new container running the image so we now have our   container running and it's running on port 8000 so  let's go into the browser and let's open up a new   tab here and then let's go to localhost that  was 8080. that was the wrong site 8 000. and   there we go so we're welcome to nginx and you can  see that everything is now working for those who   are new to docker you can see that when we go  into docker desktop here we not only do we have   access now to the image we can go ahead and delete  that we can also access the container start stop   restart so there's a whole heap of different  tools here we have available to interact with   our container in this tutorial we'll be utilizing  the docker extension in visual studio code and you   can see once you've got that installed so go over  to extensions type in docker here install that   once you've got that installed you can see that  we have access here to our images and containers   and that can make it easier to access the files  within the container for example uh so you can   see we can access all the files within this  container and i can also then right click and   start and stop and restart attach a shell so i  can use a shell directly here and it does make   it a little bit easier in some cases to work with  the container directly within visual studio code   so whatever you want to do very easy to start  and stop this we can simply just right click   and stop or go over to docker desktop and stop  or in addition to that we can use commands and   we'll learn them a little bit later to start and  stop our docker container or our instance now   typically if we do stop engine nginx service that  will then stop the container so we'll see that in   action later but that's generally how you you're  going to start and work with your docker container so let's talk a little bit about verifying the  installation well we've already seen that once   we've installed the image or downloaded and  started the container we can use our browser   and then hop over to the port that we define in  this case we defined port 8000 and you've seen   that we can then access our service we can also  use curl if you're using windows you probably   won't be able to do this directly but we can just  say for example local host co localhost um 8000   and you can see that we're returning the  information as we would do in the browser   um but here in the terminal and you can see  we're returning the the html for the page so we can also check the version of  nginx by running an nginx command here   the v flag so to do that what we're going to  need to do is right click here assuming you've   got this installed the docker extension is stored  we can attach a shell so that's going to give me   command line access directly into the container  so i can do the same thing if i'm using docker   desktop here i can select the cli that will open  up a cli into the container right so from here   i can then type in nginx mv and that will then  give me the version of nginx that's installed   verifying that we've installed nginx if we wanted  to go a little bit deeper and have a look at the   actual services that are running um what we can  do is use docker top web so that one needs to   be done in our bash shell so docker and then  top and now our service is called website so   let's call this website and you can see  from this little list here all the services you can see all the services are running here  and we go into the detail of this you notice   here we've got four nginx workers and we have  an nginx master process in the second tutorial   we'll go into a little bit more detail exactly  what that is and how to manage these workers   and and what this master process actually does  but it's a it's a good insight at the beginning uh   just looking at this initially as to where we're  going in the second tutorial and just verify   exactly what nginx services are available  okay so now we've verified the insulation   let's talk just a little bit about basic  service management commands so starting and   stopping so here we have some ng and genex basic  commands start and stop so these commands again   these are going to need to be performed  actually in the the shell the interactive shell   in our container so let's go ahead and just type  in these commands so nginx start now it's already   started of course so we don't actually  need that command so we also have stop   so notice at the moment it does say invalid  command so let's type in service nginx stop   so this time notice that it does actually work  and you can see that the terminal will close   blah blah i'll notice here on the left  hand side i do have my docker container   or my docker extension running and  notice now the container has stopped   so that when we do actually stop the service  it does actually then terminate the container so if you do want to restart then you just need to  go through the the same process that we did before   now i'm just going to close the shell that  was the shell corresponding to the container   which doesn't exist anymore and then let's just  clear all this here so if i press up if you're   not familiar with this this was the previous  command we utilized to actually run the container   and you can see that that then runs and we have  exactly the same instance so remember what we   have here is an image that we've downloaded that  image has everything pre created so when we make   the container we're essentially using that image  to build the container so every time we build the   container at this point it's going to be exactly  the same the only differences that we can make are   the parameters that we can pass in so for example  here the name and the ports that we want to use   now if you want to utilize this further you can go  over to the nginx image or the information sorry   on the docker hub have a look at nginx  see if we can quickly find it you can find   more information about it on the hub here so it's  going to provide a little bit more information   about the commands that you can utilize and  how you can interact with this image by default so you'll find that on docker hub and then type  in nginx so just to talk a little bit about some   of the nginx commands so we can get familiarized  with that so here let's go back into our container   so we're going to attach a shell again we can run  these commands here so here we can type in engine   x and then h so that's going to provide us um  or it's going to show us all the nginx help menu so we've already seen uh or utilized v  so that provided us the version now there   is a capital version so let's go ahead  and do that that's going to provide us   basically a little bit more information  so it's going to provide the nginx version   it's going to provide other information such as  the build information some of the configuration   arguments and the modules are built into the  nginx binary so a little bit more information um   by utilizing the capital v now eventually we're  moving to creating our custom configurations   so potentially we're going to need t here so  this is basically going to check the syntax like   it says here and test our configuration file to  make sure that everything is potentially in order   for us to actually um start the server and work  with the server so we also have the capital t   and again like the capital v that's going  to provide us a little bit more information   so in this case what we're looking at here  is a printout of the validated configuration   so here for example you can see that let's see  if we can go back up here you can see that the   server is listening on port 80 so it's going to  be listening on port 80 we know that because we're   passing if you remember port 8000 on our machine  over to 80. and the server name is localhost   that's why we typed in localhost and then  port 8000 which then gets translated to 80   here on the server so that's where the  server is listening so it's accepting   requests from this port and this server name so  you can see some other information like access   logs which won't be covered in this tutorial and  also some locations so next up we're going to be   talking about locations or default locations and  that's where nginx looks for data to surf directly   to you so what we saw originally if we  go back to the browser was this welcome   nginx so this page is an index uh this  is an index.html page as a default page   that was found in this folder and we can see here  that nginx here is looking for two files to begin   with so nginx will automatically serve a page  when we type in domain.com whatever domain it is   it serves a default page and this page here is  going to be or set up to be html or html index   so those are the first two pages that nginx looks  for and of course this is all configurable so this   service works with signals so we'll learn a  little bit more about signals in the second   tutorial but we can utilize the s flag to send a  signal so let's for example send a signal to stop   and you can see that will then stop the  process so the s-flag allows us to send a   signal there's many different signals that we  can send stop quick reload reopen for example   so like i said we'll learn a little bit more about  that in the second tutorial but that was the last   flag that we could utilize okay so let's move on  to the default folder so i've already introduced   the default folder we've already seen that in the  configuration file and what that potentially means   so let's go eve back into our shell here we're  just going to run the server again now again   you're going to need docker extension to see this  and this just makes it nice and easy to work with   the folder inside of the container so this  is a new container we've just started again   so if i go in here you can see that we can access  the files in the container so this very much looks   like a linux installation um these files are  synonymous of kind of a linux installation here   and you can see that if we traverse into the  folder so if you remember we need to go into   the folder and look for the default directory  so that's user share ngx nginx sorry in html   so let's go into that so user where are we uh  user and then inside of here we've got share   and then we're looking for nginx so  it's all in alphabetical order of course   and then inside of here we have the html folder so  this is the root directory the default directory   nginx looks to serve web content and you can  see we have this index page now we can go ahead   and open up so we can download and  open so let's go ahead and open   and you can see that we have this page here  and let's just get rid of all this here and we'll just end up with that so we've just  customized it slightly so i press save there   although i do have auto save turned on it's always  recommended so with that done let's go back into   let's go back make sure our container is working  so our container is running so let's go back into   the browser here and refresh and you can see  that does have an instance instant um change uh   that does sorry change the the outcome so you can  see that we are actually working with this file   on a live server so what we want to do now is kind  of copy this folder over to our our root here so   that we can work more interactively because we  don't want to have to keep going into that folder   to make changes we want to essentially be  able to work directly in our route here in   our own folder and copy everything across so  this is where we can utilize docker volumes   so next up what we're going to do  here is we're going to start to   utilize docker in a slightly different way  to start to lead towards building our own   nginx image so that we can start to prepare our  custom version of nginx with our own configuration   in preparation for us to then  actually serve that or deploy that   onto our own server because what we've got at the  moment is we've just got the basic nginx image   so if we go back here into our images this is  just the default image with the default setup   and of course we want to customize that in  preparation for actually to serve our own website   so let's uh start by going into our explorer here  so back on the um the folder here so that takes   us into the folder that we've connected to in our  desktop so i'm going to create two new files here   so docker compose.yaml file so configuration here  for our docker service our engines x service and   then just because we're going to lead towards this  anyway i'm going to make a docker file as well   so there's no file extension  for that just docker file   uh so we'll start with this the docker file so we  are going to be utilizing in this case the nginx   image right so that's pretty much the base  image that we're going to use for everything   and that let's just define the the latest version  so that's the image that we're currently using   right so that provides us a little bit of context  just for now and like i said we'll move into that   in a little bit more shortly right so  let's go ahead and define a service   okay and that service is going to be called nginx  and then we're going to go ahead and when we build   we want context so basically what we're doing  here is saying that when we build this service   we're going to utilize this docker file and this  dockerfile is basically just saying use this image   for now so that's the image that we're going  to use all right so this is like the how we're   going to build our image so we're going to define  some of the different parameters so essentially we   are going to start the service from this file so  we need to define the ports like we did with the   command here but we're not going to use this  command anymore we're just going to have this   static if you like in this file so let's just  define the ports again and then this time we're   going to define the volume so what we're going  to do here is we're going to create a new folder   no a new folder i'm going to call this html and  i'm just going to map this html file folder sorry   over to the over to the default folder in nginx  container so we're going to map our folder here   html folder over to the nginx html folder that's  the plan right so what we're going to need to   say is dot slash html slash that's our folder here  and then we're going to need to map that across so   colon slash user so this is just the path on the  container where our folder html folder is found okay so we've now kind of mapped across  our folder here to the container so what   we need to do now is just make sure that our  container has stopped so let's just stop that   so that's that stop now okay so with  that done i think we're now ready to   start this so what we can do is we can just  type in docker compose and then we can just   say build although in this case we probably  don't need to do that so let's just say up now notice this time we get all this other  information so basically this is the what you're   looking at here is basically the output from the  container so we don't have to look at that notice   it takes up our command prompt so we can kind of  hide all that if we want to if you remember what   we did was we started our container as a daemon  so we can do exactly that so use the d flag again   and you can see now we've run the service but  it's running as a demon in the background so   everything is up and running so notice here we've  got this container here called intro engine x and   nginx so that's kind of the the name here that  we're utilizing for this container and so let's go   back into our browser here and restart and notice  that we get forbidden so why is that the case well   what's happened here is that we've mapped across a  folder called html to the html folder in nginx but   the problem is here there's nothing inside of this  folder so in actual fact what we've done is we've   basically made a copy of this folder over here and  it's removed all the files in the initial image   so therefore we have nothing inside of this folder  and that's why we're receiving a 403 because   there's no file there and we don't currently  have a setup by default that allows us to view the directory so we get  forbidden right so let's go back in   inside of here we'll create a new file  inside of our html folder let's call this   index.html now as soon as we created this  file it's mapping it across to the folder   so we are kind of we've created a link so we are  kind of copying that across to our container sorry   so let's go ahead and just say hi so we're not  going to need to worry about the html tags here   so that's now saved a refresh and we receive  hi so you can see that we're directly working   now through our volume on our local machine this  is all stored on the local machine but it's been   copied across to our container and you can see  that it's working directly okay so let's just   put something a little bit more functional in  here so i've just copied and pasted this piece   of code here hello from engine next container  so that should just display just like that so it's irrelevant what exactly you type here  um but we've got something there in place so   we can identify the page right so we've created  uh we understand about the default folder now   we've created our volume so we then served our  custom page so let's pretend now we're ready to   serve this to our own server so we want to  now build a new image so that we can deploy   that image into our actual server that we want to  run our website in so let's go ahead and do that   so what we have then is a folder here html  folder with our index page that we want to   serve on our live server so we're going to  make an image so we're going to need to make   sure that when we build an image we want to  actually put this in our image so let's go   back into here so remember this is kind of the  setup details um that is run when we actually   build a new image so first we're going to download  this image obviously we've already downloaded that   so secondly we want to make some sort of  customization in this image so we want to copy   from html index.html we want to copy our html page  over to the image folder so nginx html index dot   html so we're going to copy our file over to this  um location here when we build a new image just   to zoom out a little bit there we go so that's the  second step so now we just need to build our image   so let's go ahead and do that so run docker  and then build and then use the t flag and   let's call this web server and then dot so we  give that a go and then let's have a look so   let's go into our docker let's go into images so  we should have our second image and there it is   so that's the image that we've just  created so essentially what we've done here   is we've downloaded engine x before that's the  default image we've taken this default image   we've copied our new file so we manipulated that  image or a new image to include this file our own   files and then we created our own image from it  so now what we can do is run this web server image   so let's get back into our containers here  just going to stop this container right here and i'll just go ahead sorry and remove that okay so let's go ahead now and  start our new web server image so we use a docker mark command for  this so docker run i t again rm d   p for ports so we're going to run this on a  let's do an 8080 port this time just to show   a little bit difference so let's give  this an a name um let's call this web and i'm going to run the web  server image this time okay okay so i forgot i the that there we go so now we've run our new  image so this image should come as standard with   the the index file that we put on there when we  built the image so when we refresh you can see it   doesn't work because we're using the wrong port so  let's go ahead and use the right port and there we   go so we now have a custom nginx image all ready  and prepared for us to then potentially deploy on   our own server and there we go an introduction  to essentially nginx or utilizing the default   nginx image of course there's a 100 000 more  things for us to learn but hopefully that is   a good start for us to work from now in the next  tutorial we're going to be looking at the nginx   architecture so for us to really understand  nginx we need to have a basic understanding   of what's happening behind the scenes so the  nginx processes we've already seen a snapshot   of that in this tutorial when we looked at the  processes that are running the master process   and the worker processes so we're kind of drilled  down a little bit into that and how that works and   that will give us a better understanding like  i said of nginx and how it works for us to then   more effectively manage our nginx service as we  start to scale the services for multiple users   so that just leaves a big thank you to all  the channel members if you want to support the   channel further then please consider becoming a  channel member or following us on patreon if you'd   like to we do have a number of different socials  twitter facebook tick tock and discord so by means   follow us on the socials so thank you again and  i look forward to seeing you in the next tutorial
Info
Channel: Very Academy
Views: 55,753
Rating: undefined out of 5
Keywords: Nginx, Nginx beginner's, learn nginx, working with nginx, nginx docker, nginx docker compose
Id: 7tGhir27ZJo
Channel Id: undefined
Length: 34min 14sec (2054 seconds)
Published: Wed Apr 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.