How To Deploy Django on Ubuntu 18.04 using Apache and MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so welcome and if you are watching this I'm just assuming you've played around with Django a bit you've done some local development with their local development server you know the Python managed stop I run server you got maybe a basic app built and you're ready to launch that to the world okay here in this tutorial we're going to do it it's a pretty advanced tutorial because we're we're working with a bunch of different technologies here we're working in a boon to you know Linux OS we're working with Apache which is a web server obviously we're working with Django we're making apache and django talk to each other using WSGI we're using MySQL for our database and we're keeping all our depay Chthon dependencies wrapped inside of a virtual environment ok lots of stuff going on lots and lots of command line stuff that we're doing but so if you don't have much experience with that it's gonna be a little bit difficult tutorial you're probably gonna have to pause a bunch or rewatch certain things but I have this slideshow here I'm going to post a PDF version of this check the links for the description of that so you can always reference that and then also I'll be I'll be uploading as well just a list or history of all the commands that I did in order check the description for that as well so without further ado let's let's do this ok I'm not gonna I'm not gonna run through all these slides step-by-step right now we're just gonna come back and kind of reference them as we get to where we need them ok so step one we need somewhere for our source code to live we're gonna use digital ocean for that digital ocean is a company that's a they provide VPS as virtual private servers among other things and what we're gonna do is we're gonna use them to create a virtual private server which is just basically our own tiny little computer that other people can access and we're gonna have full route access which means full administrator privileges and it's going to be a good time so you can use this link it's also in the description it is a referral link in case that wasn't a hundred obvious it is so if you click on it and you sign up I will probably get a little bit of money at some point down the road but you're also gonna get $100 in digitalocean credits and you're also getting this tutorial for free so hey you scratch my back I'll scratch yours so here it is click on that you'll go to digitalocean sign up register and then log in and you should get to let's see here you should get to a page that is some somewhat similar to this depending on when you're watching this video if they change this at all but this is my kind of homepage and we're gonna create droplets so it says right here doodle ocean droplets are flexible linux based virtual machines that run on top of virtualized hardware each droplet you create creates a new server you can use so virtual private server ok now you may be thinking what if I come down here and click this big old Django thing don't do that because this will also create a VPS but it's going to create one with a specific version of Django a specific version of web server and a specific version of a way to make those talk okay as well as a specific version of a database so if you if this is exactly what you need to use in your project great and do it and it should be configured for you but I don't want to use 1.11 I don't want to use nginx I don't want to use unicorn so I'm just going to create my brand-new droplet all by myself so I can configure it exactly the way I want it yep so you can click over here on the Left sidebar manage droplets or create click down here on create droplets what I'll do and it's gonna come up with something like this so the first thing you want is an image ok you can hover around you can click on different things to explore a bit but I'm looking at a boon to image I'm looking at 18.04 x64 that's what I'm using now we have to choose a size so for our purposes this most basic one a five dollar-a-month one's gonna work great for us okay that gives us one gigabyte of RAM one CPU and 25 gigabytes of hard drive space that may not seem like a lot of hard drive space but I guarantee you your Django project is nowhere near that big ok keep scrolling down now this isn't a tutorial on security so we're not and reliability so we're not talking about backups we're not talking about block storage or we're not doing anything down here with SSH keys this tutorial is about just how to deploy a django app so that's what we're gonna focus on we can talk security and efficiency in other tutorials all right choosing a data center region you want to choose this one that's closest to you the reason is we're gonna be connecting via SSH so we're gonna be connecting to our server through the internet so it's through the internet so we want it close to us helps reduce a little bit of lag I'm gonna go with New York and number one here going down again we're not worried about SSH keys or any of this additional stuff if you know what it is and you want it go for it but I'm not gonna cover it and then I need to create one droplet so I'm gonna click create here and you see the name here I probably should have renamed it to something a little bit better I don't really need to do it in this case you probably don't need to do it rename this to anything just because it's your only server so far but if you're managing a bunch of different servers you want to name them something that's easy to read all right so when through its little installation process and I notice here it gives me an IP address so I can click on that it'll copy it right to my it'll copy it right to my clipboard there and I can open a new tab put it in there and absolutely nothing's gonna happen it refused to connect because we haven't done anything to our server yet so now we need to actually connect to our server and get it up and running ok so that is the next step here so what you should do is you should pause the video and you should go check your email because if you just created a server digitalocean should have sent you in details now that's not login details for the website that's login details for the server and we need to connect to it and login ok so pause the video go get those login credentials save them somewhere and then resume speaking of that I need to pause the video and get mine alright so I have my password and I have my my username is just gonna be route by default that's kind of your administrator and now we got to talk about how we're going to login so let's look at this here connect to the server right if you're using Linux or Mac you can use a command line tool SSH and so this little dollar sign you'll see a lot through this that just means hey it's a command prompt so go ahead and put that in at the command prompt or the terminal ok SSH with the space followed by your username in this case mine is root the @ symbol and your IP address okay and then here you go here's an example SSH root at blah blah blah blah blah okay it's gonna prompt you do you want to add this to your known hosts or something like that and then it's gonna ask you to login so I'm gonna do that using this which is a boo to bash shell and I want to SSH root at and let's get my IP address which was this guy right here you can also get to it by just clicking right here on your digital oceans account panel give it a second here and that's what I'm talking about the authenticity of the hosts can't be established ok gives you a little fingerprint are you sure you want to continue connecting yes I do then it says hey it permanently added this to the list of known hosts so that's good means we won't get this warning again now it's asking for my password that password that did digitalocean emailed you so I'm gonna put mine in oh man my copy and paste isn't working so looks like I have to put this in manually I'm gonna pause the video and put it in there we go you're gonna get a kind of welcome screen like this which shows a little bit of information about our server talking about some packages so you'll see this every time you log in and it's asking for me to change my password so I'm just gonna go ahead and change that which means I have to enter the same thing again and apparently I entered it wrong again so let me connect again there we go that was fun typing in that long pass for multiple times so finally got it logged back in and entered correctly and then it asked me to enter and retype my new passwords if you're wondering why you're typing stuff here nothing's showing up that's just a security feature you're entering a password it doesn't want it's not gonna show it to you so make sure you type it correctly okay so now we're connected this is the SSH I'm gonna do a real fast for you windows people using a thing called putty so I'm gonna close out of this and the link to install putty is in the description but it's gonna come up with something like this right here okay now you want to click up here in session put your hostname or IP address there so I'm gonna copy this put it in here port 22 is what you want that's your default SSH port so you also want to make sure your SSH thing right here is selected and then you just hit open okay same thing is saying hey the the host key is not recognized do you want to add it same exact thing that happened before now I'm just doing login as root and then I have to give the password and same exact welcome screen I'm just going to go ahead and make this a little bit bigger for us so it's easier to see change settings let's see windows appearance here we go there you go I should be a little bit easier for you follow my commands all right so now let's get going here now I'm if you haven't worked in the command line before it's gonna baptism by fire here but hey you're gonna need to know it so may as well start learning now LS list of contents of my directory what I want to do is I want a CD which means change directory and the Ford / there means go to the root level directory so I hit LS and I can see all the directories that are in my system or LS dash L will show them in a list okay all right now that we've connected to our server let's look at the next step here which is apply software updates so our little dollar sign symbol right command prompt bash terminal right so you want to do apt space update space - and symbols and apt upgrade so let's go ahead and do that app update and apt okay this is how we make sure our server is completely up to date I'm gonna pause and I'm gonna let it go through this whole thing and I got this at the bottom so 69 packages can be upgraded but it says hey invalid operation ugrad upgrade so it's just a little spelling error here so I pressed up to get my last command and it's upgrade not you grade so redo that again pause in the video and for any of these prompts right here just go ahead and hit yes y for yes and pause in the video okay if you get this screen just go ahead and do whatever the default thing is here it looks like they want us to keep the local version so sure go ahead and press ENTER and it should should go through that all right so everything's hopefully updated everything's done I got a few more prompts I just went with whatever their default value was and hit enter okay now I'm gonna hit ctrl L which is going to clear the screen you'll see me do LS a bunch but now what I want to do is look at our next step here which is step 4 created directory structure okay so where do we want our project to live this is just an option it's it's pretty easy to set up in terms of a tutorial there's many different ways you can organize your code or your your architecture for your web application and all its dependencies but this is just what I'm gonna do okay so I want to make sure I'm in the root of my system which we saw that before the CD for change directory and a forward slash and I'm going to create the following directory structure here okay I'm gonna create a a directory called tutorial I'll just call mine but you can put whatever your project name is inside of that I'm gonna have three directories I'm gonna have site I'm gonna have Django and I'm gonna have off and then inside a site I'm also gonna have two more directories logs and public so I'm gonna go ahead and do this all right root level directory so I'm gonna make directory and I'm gonna call it tutorial and I'm the CD and a tutorial change directory into tutorial LS there's nothing here as expected I'm just gonna say hey make directory site make directory Gengo make directory off so now if i LS I have three directories in here off Jenko in site as expected I'm the CD into site and I'm gonna make directories for logs and you can change chain these all in one lines home to do that make directory logs and public and there we go logs in public are there so that was a pretty fast step let's look at the next step which is installing pip and setting up virtual environment okay so pip is a package manager you have probably seen that at least by now especially if you watch any of the Django tutorials we've done because Django is a Python package so you can use pip to install it and then virtual end virtual environment right is used to manage Python packages for different projects technically you don't need to use it here because we're only serving one Python project but it's considered good practice regardless so we're gonna set it up anyways and here are the commands to do it sudo sudo is kind of like your administrator so it's saying hey run this command as the administrator so sudo apt install Python 3 - pip and sudo pip 3 install virtual end and then we'll run through this create virtual environment ok so sudo apt installed Python three pip do I want to continue yes so it's 247 megabytes is gonna take a second pause in the video okay now that we have Python three pip installed control L to clear my screen and I should be able to say hey pip three fries and that's gonna say hey look at all this stuff I've already got installed for pip so this is kind of what comes default with it Django isn't there but at least I didn't get command not found so therefore pip is working so now I want to see say pseudo pip three install virtual and IND okay so there we go it's a small file and it created it for us so now we want to create our virtual environment this is where our kind of install of Django is gonna live now I'm gonna do that and I'm the CD back up to the root level directory so CD forward slash and then I'm gonna go into my directory I created the tutorial again yours might not be tutorial whatever project name you're working with and I'm just gonna say virtual end so this is the command now we give it a name the env and just to be safe I'm gonna give it the dash P option and tell it to use Python three so if I have multiple versions of Python I can tell this to use something else I could say Python to say Python 3.4 whatever but this is gonna use whatever Python 3 is as a command so press that and you'll see it's installing so it's using my Python 3 interpreter which is perfect that's what I wanted it's set everything up now I need to activate it so I'm gonna say source the env which is the directory I just made and just to prove it to you do an LS there it is right there so source V and V going to ve and B going to the bin directory and activate there you go and notice at the front of my commitment there I got the ven D displayed so I'm on my virtual environment we can see it we can see pit fries and we previously had all sorts of stuff pit fries and now I have nothing which is perfect that's what we want so we want to install Jango it's a package it's a Python package pip is a Python package manager so I can simply say pip install Jango and let's see so it's giving me Jango to point one point four so a good reason why we're not using those one-click app one click apps digitalocean gives us because I want to use a different version of Jango among other reasons why we don't want to use those I mean they're good but we're not gonna use them ok so that is step 5 let's go ahead and look at step 6 ok step 6 we want to create a Django project so this should be pretty familiar to you make sure you're VNV your virtual environments activated so pit fries we just did that we also installed django so that's great now I'm gonna change directory into that whatever your project name is in the Django folder we created and then just run this command Django admin start project and whatever you want to call it then a few more steps we'll talk about when we get there so let's do that ok so CD into Django I have nothing in here so Django admin start project and then I'll call it just tutorial so created that tutorial directory I'm the CD into that and let's see what else this thing wanted us to do change directory into the created project where manage pi is our root level directory and add our server's IP address to settings dot pi the allowed host content ok so now we have to talk about how we can type how we can actually edit miles directly from the command line okay so I need a CD into tutorial right where I'm where my settings are cuz I need to edit this file now you may be familiar with vim if you're familiar with vim use it but to be a little bit more user-friendly I'm gonna use Nano so Nano settings dot pi K Nano is a kind of text editor you can see your commands that you can do down here so let's find our aloud hosts constant and we just want to come in here and add a string and we want to give it our IP address let me make sure I get my IP address over here and I copy and pasted so I pasted using the right click on my mouse there okay and now I want to control X to exit and then Y to write it okay now just to make sure that it actually R it written it actually wrote I'm gonna go back in one more time just to make sure and there it is so it's actually it actually wrote perfect okay so we added our server's IP address now we need to run our development server so you're not going localhost anymore this time you're doing 0.0.0.0 colon 8,000 so the port 8000 okay so 0 0 0 0 colon 8000 then we're gonna go to that in our browser alright so let's see python managed dot pi i need to be up one directory huh this thing is running extremely slowly today go up one directory we're managed up pies and python managed pi run server zero point zero point zero colon 8,000 it is wrapping around on a line but there's that's one command there ok perfect i'm not gonna worry about migrations or anything because we're not going to use the SQL like database so we can actually go and look at our server now you may want to try to go to this IP address and that's not going to work you just need to replace all these zeros with your IP address so in my case it's gonna look like this let me open up a new tab here and I'm gonna say hey go to my IP address port 8000 there we go it worked so there is my django installation being served from my server at that IP address okay but obviously we don't want our users to have to do this we want them just to be able to put in our address there and have it work which right now this isn't gonna do anything for me so let's move on to the next step okay the next step is to install MySQL which is a kind of production level database and that way we're not using that DB SQLite 3 which for small projects is probably ok but anything of scale or anything is gonna require a much better database so we may as well do it now ok it's pretty easy ok from the command prompt from the terminal for an enter sudo again for our administrator apt and then install MySQL server and then we're gonna say sudo MySQL secure installation it's gonna bring up a bunch of prompts basically we want to say yes to all those prompts and then we're gonna run through some Maya school commands so let's see that ok so I was in here I killed my server using control-c I'm the CD back to the root level directory not really need to but I'm just gonna do it anyways and we're looking for sudo apt is our command install and then the name of the package MySQL server ok 162 megabytes is what it looks like down here I'm just gonna hit yes and I'll pause the video and come back all right looks like MySQL installed without any errors that's perfect so now I'm going to do sudo MySQL underscore secure underscore installation is going to kinda give us walk us through some security steps okay valid a password plugin basically is going to only allow us to create strong passwords I'm gonna say why and I'm gonna say hey I want strong passwords so I'm gonna go with two for strong and now it wants me to set a new password so I'm gonna do that okay so so say that password is only a 50% but I'm just gonna continue with it okay remove anonymous users so you can read about here so by default MySQL installation has an anonymous user allowing anyone to log into my school without having to have a user account doesn't sound good to me so we're gonna go ahead and remove that so just say yes here we go so normally the root should only be allowed to connect from localhost okay that ensures that someone cannot guess at the root password from the network so disallow remove login yeah yeah we don't need it so may as well disallow it okay remove test database so by default MySQL comes with a database named test that anyone again can access does it seem very secure let's go ahead and remove that so yes and then finally reloading the privilege tables will ensure that all changes made so far would take place immediately maybe if we don't do this our changes never get applied so they don't really have the effect so we want to reload our privilege tables so go ahead and hit Y for yes there we go we're all done so now we need to create a user and a database okay again we're not gonna go super in-depth into MySQL and all the different commands and different stuff you can do with them but here's the commands we need right here so we're gonna create a username and we're gonna say hey username at localhost identified by and then we're gonna give it a password then I'm gonna create a database and we're gonna name it whatever we really want in this case Django project I'll probably just call it tutorial but whatever this is the name of the database that you want then you've got to grant all privileges on that database we just created to the user we created okay Jango is gonna connect via this user to this database so a this database has to exist be this user has to exist and see this user has to have all the privileges on that database right because Jango is gonna automatically crud create read update delete database entries for us so that through this user so that user needs all those privileges and then we're gonna flush those privileges so again that's kind of like a quick reset resets all those privileges to make sure what we just did takes effect so let's do that all right control l2 clear my screen again now the command to get into MySQL is MySQL ok so there we go since I'm logged in as root it's letting me connect right away and let's see so we have our MySQL prompt so we need to start working with those commands so create dad create user username in quotes at localhost in quotes identified by whatever password we want let's see I'll go with this just random password and hopefully it passes Oh redo that again you can put whatever you want here but if it's not strong enough it's not gonna let you do it so there we go your password does not satisfy current policy requirements which is from that MySQL installation so let's try it again create user username at localhost identified by and then we put another password in quotes here end it with a semicolon so the password I'm going to use let's do this see if that's good and may as well pick one on in there okay still doesn't satisfy the current policy requirements so one more time here create user username at localhost identified by there we go so that finally worked now you may want to copy and paste this and put it somewhere okay so I click the write cool right button again and that kind of helped me on my mouse copy you'll notice it also pasted it which is putty thing and that's pretty annoying but if you can't do that you may have to just manually write it down so write it down keep it somewhere safe because we're gonna need it here in a minute and if you forget it it's not a big deal come back here and just create another user alright the next thing is we need to create a database so I can do show databases and it says these are the only databases that come built-in with my school we're gonna create one so I'm gonna say create database and I'm just gonna call it tutorial the name here doesn't really matter it's just we're gonna need it again here in a second because we got to tell Django to use this database and then finally I'm gonna grant all privileges on my database tutorial dot my asterisk for also all the tables inside of my tutorial database to the user at localhost okay queer okay zero was affected perfect so now I finally need to flush privileges and there we go now we're done so that's a little quick crash course and creating a user MySQL creating a database and granting the privileges on that if you are curious you don't need these capitals these these commands here don't need to be in capitals but it's a it's a general convention that people will do this so you'll see it all the time alright that takes care of step seven which is this we installed MySQL we did our secure installation we created you user gave it this kind of crazy password and then granted all the privileges on our database to it and flush those privileges to kind of reset them make them take effect immediately so the next thing is step eight we need to tell Django that we're gonna use MySQL and we're gonna need to say hey you're gonna use this username you're gonna use this crazy long password and then hey you're also gonna look for this database okay now to do this we need to first install this MySQL client python package so it's a Python package and here are commands to do it first we need to install the Python 3 development kind of packages so that's apt installed Python 3 - dev then we need to install the MySQL client kind of development packages so that's apt install Lib MySQL client slash dev and then when we get all those installed we need to install MySQL clients be a pip so if you try this one first pip install MySQL client it's gonna give you some errors so let's let's even look at those errors I need to exit out of MySQL and let's see let's do a pip freeze ok I only have Django and py TZ in here so let's what happened right now if I just do pit install MySQL client here you go you're gonna get this command Python setup I egg in fulfilled filled with error code blah blah and it's like I have no idea what this stuff means okay cool all that's kind of going on is we didn't install the dependency it needs in the correct order so that's back here and our step 8 so let's say sudo apt install Python 3 - dev ok so that's good and sudo apt install lib s Lib MySQL client - dev and hit yes so up here I actually already had Python 3 dev installed which is good now I need to install the MySQL client and that's looking good so now I should be able to control L to clear the screen hit up a few times to bring up my history and pip install MySQL client and voila voila voila whatever it is wallah it is done so now I have my SQL client installed I can do app it freeze and there it is right there alright so now that's the kind of first part what's the next part is step 8 well we're gonna create this file MySQL cough again the name doesn't matter whatsoever they can be you know my favorite Kitty dot cen F or my favorite Kitty dot text whatever you want but I'm gonna use this convention MySQL CNF and this is exactly what I'm gonna add ok and then we're gonna use this file and we're gonna tell Django to connect with the settings stored in here so it's a little more secure that way I don't need my database my user name and my password in my settings dot PI file I can keep it in this MySQL file on my server so it's not stored in github or anything else like that so let's do this ok so where am I it looks like I'm in my root level directory I'm the CD into my project folder the root project folder there tutorial and that's why I created this off folder and in here I'm gonna Nano and we call the what MySQL about CNF ok so we're gonna say hey not container we're gonna say client in brackets then I'm just gonna say database equals whatever your database name was you created with in a MySQL command we did a second ago mine was too and I'm gonna say you're my user and I'm gonna put these in quotes as well mine was just user name maybe you put yours like Django user or project to use or something like that and then password is going to equal to that long password that I had let me copy it bring it over here and paste it there we go so that's looking good and then finally the default character set I do not believe this is required I'd have to check the documentation but um as well throw it in there anyways there we go now control X Y Enter and there we go Maryana control access area all right so LS and I can cat and there we go it prints out so I got tutorial username that crazy long password of random characters pretty much utf-8 character and you said everything looks good so now we need to restart MySQL and you do that just by sudo systemctl restart MySQL sudo systemctl all one command restart MySQL there you go alright so now let's look at the next part of step that was eight a moving onto eight beat so we need to tell Django to use this file so we have that database as constant in the settings dope I previously we had a set to SQL light now we're gonna say hey your engine's gonna be this MySQL back-end so Django DB dot back in stop MySQL and then your options are gonna be this read default file and that's where we're telling that hey go into the root level directory that's why that forward slash is right there so it's an absolute path so root level directory of our operating system get going to tutorial go into off go into MySQL CNF of course gonna have to change this path right here for whatever your directory structure is and then we're gonna create a super user make migrations run server and we're going to make sure that everything is being or make sure that our MySQL is working correctly so let's check that out all right so I need to CD into one directory up that's what the two periods means hey change directory up one directory I need to go into my Django I need to go into my tutorial and in here I need to find my settings dot pi which is inside of my tutorial folder there we go here's the project package there's my settings dot pi so I'm gonna nano it scroll down we're looking for the database constant there it is databases and I'm just going to comment this out maybe I don't want to delete it because I want it there just in case something goes wrong for testing it's easier to remember and I'm just gonna do it so databases equals default all right this thing is running slow and it's really starting to annoy me here so I'm just gonna delete all this here I wrote it in a text file so I'm just gonna copy and paste it in there you go so databases it's a dictionary alright open and close ok default is my first keyword it's also a dictionary you have an engine keyword and that's being a Django DB back into MySQL your second key is options ok and that's also a dictionary and the first key in that dictionary is read underscore default underscore file and then the path to that file we just created that MySQL CNF file or whatever you called it so make sure it's absolute so it should start with a forward slash and then make sure the directory structure and the path is absolutely correct and then ctrl Lex yes I want to save it and enter ok now what I want to do CD backup to my root level directory and I want to say manage dot PI check make sure I didn't make any syntax errors which apparently I did it's on line 94 right before off password validator so let's look at that Nano tutorial settings dot PI let's see what I did wrong here templates wsgi application oh you know it looks like this is not a closed off dictionary let's see so this bracket closes that one this bracket closes that one yep I'm missing the one to close this database up there so even with copy and paste I messed it up alright trolex and let's go ahead and write that now let's see if manage dot pi check works all right perfect now I want to create a super user ok so and let's see before we create a superuser let's just see what it does create super user okay it's gonna give us an error right because it says hey to toriel which is great it doesn't have this table so right right off the bat it's perfect that we're getting this because it's not getting a hey you can't connect error so you may see something like this so what I'm gonna do is I'm in nano and I'm gonna say my root level directory off my SQL cough and let's just change my username to have multiple A's let's go ahead and try that command again manage not pi check so that's working good let's try to create a super user and now it's saying hey access denied right so if you see access denied it's either an issue with your username or your password so double check those make sure those are correct if you can't get it after a couple minutes just bite the bullet create a brand new one and try it but if you don't see that and you see what I got just a second ago that's perfect it means it that it means your app can actually talk to the database let me get my MySQL comp set up correctly again crazy super user so there you go that auth user Django is trying to go into this off user table and create a super user it can't find it so that's because we need to migrate first so migrations build all those tables now there actually should exist you could go into MySQL and figure it out so MySQL alright show databases there's my tutorial under use tutorial and I'm gonna show tables there you go so those are all the tables it just built previously this was blank all right exit there now I need to create a super user manage that pi create super user all right I'm gonna leave it blank to use it as root sure that's fine why not email address I'm gonna leave blank and give it a password not my crazy super secure mysql password there we go all right now let's go ahead and check our website again so I'm gonna say Python managed I manage a PI run server IP address zero point zero point zero port 8000 everything's working good no migration warnings here so that's good let's check it in a browser so it's right here oh yeah that's cuz I don't have the port on the end so put the port on the end that's Django so that's looking good now what I want to do is make sure get the forward slash admin make sure I can log in all right just again double check in my MySQL so I think we said root and then I gave it this password here and I get the Django admin panel perfect so they were go now we got Maya school we got Django working and we got it serving from the local server okay the next thing we want is we don't want to have to our users to put this port in all right because in real life this IP address instead of being IP address would be a website URL like google.com or Project full-stack calm okay and we don't want them to have to put this port in so we need Apache to do that so that's step 9 install and configure Apache all right install and configure Apache so this is kind of the most complicated not installing it but configuring it gets a gets pretty complicated and it's very easy to get into errors with this and I'll try to kind of point out a few common error spots as I go okay first of all obviously we need to install Apache okay so sudo apt install we keep seeing this can and apache2 and then we're gonna use this thing called WSGI to talk back and forth between apache and Django you may have noticed in your previous Django experience that there's this file called WSGI dot pi well we're gonna use that so basically Apache is a web server we're gonna tell it to go to WSGI dot pi and then WSGI pi is going to say hey launch django so what axe is kind of like a middleware between them there's a link in the description too it should be the WSGI project you can read more about it there but I'm gonna run this command back up my server control C to kill that server and then I'm gonna say that command sudo apt install I'm gonna say hey install apache2 and while you're installing things may as well go ahead and install lib Apache to mod WSGI pi3 yes to install all this stuff now let's go back to our browser okay right now if we go to this just hit enter nothing's gonna happen right because we killed that development server but what happens if we just put in our IP address whoo Apache to default page so there we go that kind of is our confirmation that hey Apache 2 is installed and if you really want to nerd out you can read some more about it here there's also the link to the Apache 2 documentation in the description of the video but I'm gonna tell you what we need to do to get this thing going okay now all your your Apache installation all the things that we really care about lives in this thing so CD to the root go into Etsy et Cie Apache 2 and you'll see all sorts of stuff here ok we're really looking for what's in this site available so I'm gonna CD in two sites available come on now see these sights available there we go and in here zero zero zero default calm okay so this is kind of what we expect to see and this is what we're going to modify to say hey use our settings for our project so I'm gonna nano into that okay and you see there's a lot of stuff going on again this isn't gonna be a real in-depth Apache class we're just trying to think of what we can do to get our app up and running so I'm gonna delete a bunch of this stuff all these comments I'm just gonna hit ctrl K that's kind of delete line if you go a little bit too much and say like you delete the server admin you can hit control U and that's gonna bring that stuff back alright delete the rest of this stuff down here and let's talk about what we want this thing to look like okay now again we probably might want to create our own comm file and not go not modify the default one but we're just gonna do it this way right now cuz we're just trying to get this thing up and running okay this is what we want the server admin a document root we can leave this same okay error log in custom log we're gonna change that we created that logs directory so we're gonna go ahead and we're gonna say hey I want my logs to live in there this should have a logs directory after Nathan so project names site logs access log project names site logs err or log okay and then we're gonna have to say remember how we talked about in needs that WSGI dot pi so we need to tell apache to go talk to WSGI dot pi so this is kind of what we're we're doing that or one of the parts we're doing it so you'll see this a lot in apache where it looks almost like HTML right or XML you have a directory almost an element we're in that directory element we're gonna put the path to the directory containing WSGI dot pi so not the absolute path to the file okay but the absolute path to the directory contained in it okay so you should not see a WSGI dot pi on this line you should see the directory it contains and then we have this files kind of element here and that's where we say hey WSGI to PI and require all granted okay so this is common Apache stuff then we need to be able to tell it to hey use our virtual environment and here's all our WSGI settings this first one right here daemon process you have to give it a name and you need to make sure these process groups and your daemon process group that first kind of argument there is the same okay and then this Python path this first thing is going to equal and then that's the absolute path to the directory containing dot pie okay so this is the path that it should be starting to look for stuff in absolute path directory managed dot pie is so again the directory not the actual manage dot a file then the Python home is going to equal wherever our virtual environment is the root level of our virtual environment okay so remember mine lives in tutorial the env yours is whatever your project name is and then the env if you did it the way I did it okay just remember this should not be like ven slash bin slash activate it's just wherever the root level directory is VG env okay we talked about the process group and then finally this kind of script alias here okay one forward slash followed by a space followed by the path to the directory containing WSGI to pi finally WSGI dot pi so this final one is the only one where you're pointing 100% to the file WSGI dot pi as opposed to just the directory so as you can see this is a lot of stuff that's going on okay and this is where almost all the errors okay come from it at least the really hard to debug errors start to come from this so it's very important you pay very careful attention to these syntax here making sure you're getting your project renames right making sure if it's a absolute directory from the root which all these are okay making sure they start with the forward slashes making sure your spelling is correct again that goes back to making sure your directories are correct okay and then following this here's an example pretty much the one I'm gonna use right here so let's go ahead and do this alright first thing I'm going to do is I'm gonna get rid of this Apache log der here and I'm gonna say I wanted to use tutorial and then I called my sure to call my directory logs now it doesn't matter that we haven't created this error dot log file yet as long as this directory is correct it's um mysore not my school Apaches gonna create it for us so same thing tutorial logs actually I think its site logs yeah should be site log so tutorial site logs tutorial site logs access login error log so that's good alright now I need that kind of directory element we talked about that thing that that thing that go ahead and tells us where WSGI dot pi is so directory and in the path to the directory containing WSGI ok not the file so that's for mine that should be tutorial I think I created the Django directory and there I created that project tutorial and in there there's another kind of tutorial package level that has a settings dot PI the URLs dot PI and the WS gif dot pi so that's that go ahead and close that look how slow this thing is killing me okay now I create the files WSGI dot pi and files close it now I just say hey require all granted okay so hey in this directory you can go ahead and access this file it's all granted to that WSGI dot pi so now Apache can connect to that file so that's perfect so now we just got to do that kind of crazy WSGI daemon process all that stuff so let's look at that WSGI daemon process got to give it a name I'm just gonna call it tutorial and then the Python path so this is gonna equal where the managed dot pi is the directory kay so for me that's absolute path to tutorial to Django to tutorial and that's where my managed dot pi is and on the same line just put a space there and it's Python home and this is pointing to your root level of your virtual environment which for me is tutorial ven V and got some weird wrapping stuff going on there okay so hopefully I didn't create or make any spelling errors in there syntax errors we'll check here in a second now we use WSGI process group and all we do for this is give it the same name we put right there so tutorial then we do WSGI script alias give it that root symbol there the forward slash and then a space and then this is the absolute path to your WSGI dot pi so it's pretty much the same thing right here just with WSGI dot pi on the end of it so tutorial Jango tutorial and tutorial followed by WSGI dot pi and the virtual host let's hope I didn't mess that up that's a lot of typing again it's super easy to make terrors in that file if you do make errors they'll pop up in that error log so we'll go look at that here in a second all right now we need to check our syntax right make sure make sure I didn't enter anything at least syntactically incorrect maybe I have directory structures directory names and correct but I want to make sure I didn't make any syntax errors so at sudo Apache CTL config test and you're gonna see this that's fine but you're looking for that syntax okay so alright let's see what happens now I need to do sudo I need to restart Apache for those server settings to take effect sudo service Apache to restart ok good no errors go back here so it was previously going to Apaches default page let's see what happened and look I'm absolutely amazed that I did that without any errors that is 100% luck I almost always run into some error right there so let's look the most common error I run into is normally because there's a lot of typing down here normally I get something wrong down here most of the time I actually leave this WSGI dot pi off so I'm gonna do this just so you can kind of see where to go look maybe you get a little bit of information if you run into errors okay I'm gonna restart that and go back here ok forbidden you don't have permission to access the route on this server now that doesn't really tell us much so what you can do is check the error logs and remember we set that error log to something specific so I go CD into Django we put it in site and then it was in logs and there is that access log so you can you can look at that and that's gonna tell you who's accessing the server but here's my error log right here and it's saying hey client denied by server configuration and then it's this so again it doesn't really give us too much information but at least just by looking at this at least just by looking at this we know that it's something with Apache itself and it's something with a our server configuration at least we've kind of narrowed the problem down maybe at least a little bit alright so now I need to get back into Etsy Apache to sites available and I need to nano that default comp and put it back to what it was and we're almost done one final step here which I'll show you what we need to do here come on let me restart the server there it is okay so now watch what happens if I go to admin look at this so I have no CSS it's ugly right you can even look over here go into your console see all this stuff is 404 errors okay so that's the next step and the final step okay so basically in the in the default kind of Django run server it's creating it's serving its own static files well it's not going to do that for us so we need to tell it using Apache alright we did this static files are not loading we need a place for our static files to live on our server so again this is just a practice that I do from time to time not required you can put them anywhere you want but just make note of the directory or putting them and I'm gonna put them right here project name site public static so I'm gonna go ahead and create that alright root level directory going into my tutorial directory going into my site going into my public and in here I'm gonna make a directory static alright perfect so now that that directory exists okay I need to also add it to the settings dot pi I need to tell Jango hey your static route where all your static files live are that directory right there the same one I just created so up one directory up one more directory CD into Jango see the ended tutorial should be one more level yep CD in tutorial and I need to nano my settings dot PI all the way at the bottom here I'm just gonna say create a new constant static route they should talk about it here at this URL if you're curious of what that is but static route and the absolute path again to that directory so tutorial I think we call that site public static all right now we need to tell Django hey all those static files you normally use okay I want you to put them into that static route that directory we just created and that's up one directory and then Python manage job PI and we need to say collect static there you go see 119 static files copied there I can even go in there tutorial site public static and there you go my admin so it's all the admin files their CSS fonts image all this stuff is just copied there which is perfect that's what I want and now you may be thinking awesome cool let me come back here I'm gonna go back here too that's not it that's my nano cheat sheet because I don't really use it too often here we go you may say hey maybe I'll just go here again yeah that's not working what's going on well Apache now needs to know to look for static files in there as well alright definitely gets a little confusing so you can see what we need to add to Apache that 0-0 default comp file okay you need to use this alias keyword so that directory kind of thing going on again so hey go into wherever our static is and then require all granted cool but we need to put this little alias right above it so this is what it looks like okay I need to go to Etsy apache2 sites available and nanos your zero default now I'll just create my directory first and where is that going well that's going to tutorial Django site public static I think it was will see if I get this wrong and I really fresh that browser and I get nothing that means something's going on with this alright and here I need to say hey go in there and require all granted and then finally I need to add alias okay it's gonna say hey static anytime you see this I also mean go into here which is the directory pretty much the same exact this directory right there so tutorial Django site public static alright control X control Y and enter now let's just go ahead and restart our Apache to sudo service Apache to restart okay yeah should probably check my syntax to so that's sudo Apache CTL config test syntax okay perfect go here to my browser enter something's going on so I'm getting 403 forbidden okay that's why it's very important to look at your console so for 3 forbidden for these directories here already so when I run into this 403 forbidden first thing I think is probably my configuration files bad second thing I think is maybe Apache itself doesn't have permission to access that directory okay typical file file permissions stuff so let's go with the first hopefully answer or option all right so I just added this let's look at this alias static so that's good okay I want to see static it's gonna look in this folder which is tutorial Django site public static tutorial Django that's the problem I need to remove this Django or hopefully that's the problem it's definitely a bad path that's the correct path tutorial site public static not tutorial Django site public static see if that worked and I need to restart it any time you change a setting there you need to restart it sudo service Apache to restart and now refresh there we go now it's working all right except my internet is slow very slow tonight I need to restart my router or something alright so there you go super long tutorial but we got through it all I got a Django site being published I got it serving static files so if you're doing this if you're if you're continuing development anytime you change your Django files you're gonna need to restart Apache and anytime you change your CSS files you're gonna need to collect static and I don't think you need to restart Apache I'd have to check again a lot of stuff going on here I can't memorize anything everything but if you uh if you change your static files like you make some CSS changes and then go ahead and collect static and see what happens alright so lots of stuff going on again I'll post the the presentation link to that somewhere in the description just go ahead and look for it as well as they listed the commands should be in there as well and alright thanks
Info
Channel: Project Full Stack
Views: 27,697
Rating: undefined out of 5
Keywords:
Id: Xjdv31k-Kf4
Channel Id: undefined
Length: 67min 42sec (4062 seconds)
Published: Mon Dec 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.