How To Deploy Django on Ubuntu 21.04 using Apache and MySQL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there hope you are doing great i'm doing good uh in this video here we're going to talk about deploying django using ubuntu 21.4 apache obviously django and uh my sequel mysql here i just wanted to do an updated version of this uh for basically 2021 i know the last video i have i think uses django 18.4 so it's about two years old a little outdated so i figured i'd update it so here we go all right step one set up a server so what i'm going to be doing here is using digitalocean as my vps my virtual private server provider you can use whatever virtual private service provider you're at that's out there i you know i use aws you know amazon web services and i also recommend lenodelnode.com as well but just in this case i'll be using uh digitalocean you can click this link it'll also be in the description of the video should basically give you a hundred dollars of uh digital ocean credits um obviously it's a referral link so i get a little bit of a kickback scratch my back call scratchers type of deal uh you're gonna register you're to log into digitalocean and then we're going to create a new droplet so when you're creating a new droplet there's multiple kind of different configuration options and i'll go over this but this is kind of the very first step this is what digitalocean looks like you know left sidebar here top nav bar for our navigation um i'm just gonna click up here in the create so i'm gonna click create and then droplet and that's going to create a new server for us so what we really want to look for is we just want to use a base ubuntu image you can if you want to explore the marketplace which has a bunch of pre-configured virtual private servers but i just want to start from a clean slate so that's just the way i prefer to do things so i'm just going to go with a just a clean ubuntu 21.4 server 64-bit there choose a plan i'm going for the cheapest one here uh obviously this is we're just kind of playing around uh with the local excuse me local development whatnot so we just need a very cheap kind of basic plan so shared cpu basic cpu options you want to click this one which is regular intel with ssd and then just uh the cheapest option five dollar a month unless whatever you're doing you need a more advanced one that's up to you data center region you're going to want to choose whichever one of these is closest to kind of your location it'll make things a little bit faster i'm close to new york so i'm just going to use a new york data center one three doesn't really matter um choosing your data center regions down here starts to matter if you're talking about having a bunch of droplets and servers and whatnot um interacting with each other then you kind of want them very close to each other and on the same region as well but again that's more advanced stuff we just want to get this thing up and running so i'm choosing the data center reader that is close to me uh let's see vpc network yep don't care about that don't care about these additional options unless you really want them uh let's see so authentication for this i highly highly recommend you use ssh keys especially in production however this is just a quick little tutorial video focusing on deploying django not not focusing on server security so i am just going to use a root password here uh and that way i'll be able to log it from root i already got this randomly generated password i created and that way i'll be able to log into root from any basically terminal or putty or digitalocean also has a access in here as well with this root password if you do this you definitely want to keep that root password handy alright other than that finalizing create we're going to create one droplet if you want to create multiple you can but i'm going to create one droplet here and the name of it is uh you really want the name to be something descriptive like this whole ubuntu s1 vbc that doesn't really make sense to me um i really like to have my host names have something very descriptive about the content they're hosting because if i'm running 20 30 40 or more you know droplets i just want to look at the name of it and have an idea of which one that's actually you know running you can do tags here it doesn't matter you can select a project here if you want uh that's the projects up here on the left it doesn't really do much other than it assigns a droplet to a project or just kind of puts it up underneath that project makes it a little bit quicker to access but really doesn't do anything if you want to enable backups production level it's probably good for our little you know demonstration deployment video we could care less about backups but if you do want to do backups digitalocean will give you system level backups once a week so with that i'm just going to hit create droplet and you're going to go here it's going to bring up this view sometimes this takes a few seconds uh you know 30 seconds a couple minutes for it to fully run through all its processes to create this server the droplet so i'm just going to go ahead and pause the video for a second until it's done all right so it looks like it's done you'll see here it shows me my droplet name it shows me an ip address so copy paste this keep it handy because you're going to need it for uh several different things we're doing here other than that if you want to get more information about it you can click on it and you can read more about it so you get access logs you know turning things on and off rebooting you can all do from here resizing so at some point in the future if you're running a project here and you're like oh well you know i'm getting a bunch more users and i expect it i need a little bit more memory or a little bit more cpu power uh you can just quickly and easily do it from here it shuts your shuts your droplet off and then resizes it it's pretty fast though so it doesn't really cause much interference with your with your users then you can do networking all sorts of other stuff but let's see that brings us up to we did this we did that set up the server we did all this so that brings us up to basically connecting to our server so there's a few different options to connect the easiest one is going here to the digitalocean panel clicking on the access and hitting launch droplet console this is the easiest way to connect so that's what i'm gonna use for this video however if you do want to use putty if you're a windows user you have putty installed um you're basically going to fire a putty you're going to click session and then you're going to enter the ip address of the server make sure you're on port 22 because that's ssh and then you'll hit open and let me bring up my putty so this is my putty and i can just go ahead and click hostname is the ip address i just created port 22 we're connecting via ssh all that's good and i hit open this pops up it's off screen but it popped up with a little pop-up asking me if i wanted to trust it i hit yes and i know this is really small i apologize in the text there but we're going to log in as root and we're going to use that root password we used when we created it and there you go so i'm connected that way the other way you can connect is through any type of command prompt that allows ssh so right now this is git bash and if i just do ssh the root at the ip address it's gonna do that same thing putty said do i want to trust this yes i do i know what i'm connecting to who uh and now what is the root password got it over here so i'm just gonna copy and paste it and i'm connected that way so there's multiple different ways you connect but for this to make things easy i'm just going to use the digitalocean dashboard here and launching the droplet console all right so when we first connect the very first thing we want to do when we connect is we basically want to uh we want to apply our software updates so we're going to run this command the apt update and app upgrade and then after that's all done this normally takes a while a couple minutes um and then after that's done we're going to want to reboot you don't have to reboot but it's just good practice so if i come down here and i say app update and app upgrade you'll see it goes through a bunch of things it's going to prompt me a couple times i'll wait for the first prompt here there you go you can read about what it's trying to install and whatnot for most of these you just really just hit yes to all of them you'll have a couple more prompts that come up again i'll wait for one more just to show you and then i'll pause it actually i'll pause the video and wait till it comes up all right here's an example one of these pop-ups you'll see you'll probably get a few of them um and you can read more about it but for most of these you can just select the default option and that's going to be fine so you just go in here you select it and you just hit enter if you're doing any kind of more advanced stuff where you really kind of care about what those are you can read more about it but for us this is a brand new clean install um we're just updating it so we don't really care we're just going to keep all the default stuff that ubuntu wants us to all right so i'm going to pause the video again until this is all done all right it uh apparently dropped out there we go so it's basically all done and all this is saying that a new kernel is available all it's really saying is hey you should probably reboot you don't have to but if you don't reboot now you're gonna get kind of this thing constantly popping up through the rest of this process so i'm just gonna say hey yeah we're gonna do it we're gonna restart some services there just the default options and then finally i'm just going to once this is done here i'm just going to do a reboot all right consoles disconnected because the virtual private server our server is shutting down and rebooting it'll probably take a minute or two before it's fully rebooted and available for me to launch it again alright i gave it a minute or two and now i'm gonna go in here and i reconnect it so it's good everything is updated and we're good to go we're good to continue so now we're gonna go down and we've made our software updates here step three step four we're gonna create our directory structure there's a lot of different opinions of where you should have your code live on a server we're not going to talk about all the different opinions and pros and cons of each one for this we're just going to create a super simple directory structure we're going to have a we're gonna have it live at the root of our server so we're gonna first cd to root and then we're gonna create kind of this setup so we're gonna have um we're gonna have basically a inside of our project here whatever you want to call it i'm calling my django slash or dash project but you can call yours whatever you want inside of that we're going to have a site directory and a source directory inside of the site directory we're going to have logs and public directories and inside a public directory we're going to have media and static why what are we going to do with these so site basically holds our server logs and all our public data the source directory is going to hold the actual django code the django project um the logs directory in the site is going to hold all our access and our error logs from apache and then the public folder in here we're going to have media static that's going to host all our static files like our css javascript whatnot and our media file such as you know user uploaded user generated content is going to live in there so that is our directory structure there's many many different ways you can set up your directory structure but this is the way i'm going to do it it's important if you're going to follow this tutorial to the t that you kind of do it this way because a lot of your apache configuration settings are going to rely on mainly where your source code is and then also your media and static files are very important um for apache that it knows where to find those so i'm going to create this here let me bring up my terminal again all right so we're gonna cd to root oh that's really big okay there you go let's cd to root and in here i'm just gonna make dir django project and then i'm gonna cd into it and uh that's i spelled it wrong didn't i give me a second let me remove that move jingle project to django dash project there we go now i'm going to see the into django project make this a little bit bigger here all right so now i'm in my django project directory right so i got this upper level django project one done now i need to make our next level the site and the source so let's do that make door site and source all right so if i do an ls that's all in there now what i need to do is i need to make inside the site i need to make logs in public so let's cd into site and let's make dur uh what would we call it logs in public all right then inside a public we need to see the end of that and make uh that media and static so cd public and maker media and static all right that should be it let me cd up up cd up up again so back to the django project if i do a tree i can't do it so let me just install it real fast it'll be fast here i like tree it's a good command all right so if i do tree now there you go i got site logs public media static and source so that's my directory structure exactly what we said we're going to have there so all right next next we're going to install pip and set up a virtual environment pip is python's package manager virtual environment basically allows us to um have separate isolated environments for multiple installs so you very easily could run multiple django projects from this one server they may rely on different django versions or different product package versions so you want to keep those different kind of installs isolated we're going to do that using virtual environment so what we're going to do here is we need to do this the sudo apt install python 3 pip or just i'm already logged in as root so i don't need the sudo i can just do apt install python 3 pip and it'll take a minute to install so i'm just going to pause it all right so that is installed so i have python 3 pip installed so if i do a pip 3 freeze it's going to show me basically everything that i have installed right now which is all sorts of stuff that comes by default but what i do want to install is virtualenv it's a package that's going to allow me to create and manage these virtual environments so you do pip3 install virtual m pip3 install virtual m this install should be fast there it is so it's installed uh now if i do a pip3 freeze i should see virtual end listed there right there so i'm using 20.8.1 in this video for my virtual end version all right uh now we're gonna actually create a virtual environment so we're going to make sure we're in whatever project name you used again i'm using django project and we're going to say virtual m v envy so we're going to create a new virtual environment and then we're going to basically say we're going to call the virtual environment folder v env if you want to call your virtual environment folder something else you would change this right here and then this dash p basically says hey the python the path that i want you to use or the python version i want you to use is python3 you most likely don't need to do this however you will have python 2 installed on your server as well by default so we just basically want to make sure our virtual environment is make sure we're using python 3 as opposed to python 2. wrong button there so let's do that here we're going to say i'm in my uh project folder where the site and the source live so i'll just do virtual m v and v for the name of the folder and then the dash p for python 3 version it should be pretty fast you can read more about what it does but basically just copies all your python files over there if i do an ls now there is my venv and to activate it to actually work on this so right now if i do pip 3 freeze i'm still on kind of my root level my global python environment i don't want to do that i want to activate that brand new super clean virtual environment we just set up so to do that i do this source v env or the name of your virtual environment folder go in the bin directory and run the activate script so i do source then then activate and you'll notice now at the start of my command prompt i have the name of my virtual environment in parentheses that tells me i've activated i'm working on my virtual environment now if i do a pip3 freeze i get absolutely nothing so i'm working on a very clean python install which is awesome now let's install django so i do pip install django at the time of this recording it's 3.2.8 so you could just do a pip install django like this uh pip install jingle like that it'll install the most current version i'm just gonna force it to 3.2.8 doing that uh because that way if you're watching this in the future who knows django updates and there's some changes to the wsgi files or something that kind of throws a bunch of errors into here so if you're following this to the t just make sure you're working on 3.2.8 but really it probably doesn't matter if you're doing this later in the future and there's a django version which breaks this configuration setup let me know and i'll just create a new video showing how to do it for that new version um all right so now if i do a pip 3 freezer i can just do pip at this point but pit freeze there's django installed and i got a bunch of other stuff that comes with django some of the dependencies there so that's step five we've installed pip in our virtual environment we're getting closer all right so now let's create a django project so we're i've already done this step making sure my virtual environment is activated and django is installed now i want to cd into my project name and the src or the source so i'm going to go okay well cd django project source right now there's nothing in there it's an empty folder so i'm going to do this command right here and before you type it don't forget that that little period in there super important at least for the way i'm setting things up because it's going to change some of the paths if you don't do that it really doesn't matter but your paths will need to be slightly different than what i have in my configuration files so django admin start project you give it some project name which i'm going to call so i'm going to call my project name tutorial and then the period means install it in this current directory if you don't have that django is going to create a new directory with this name and then install the project inside of that so that's all that dot means it says hey create the create the directory here so if i do django admin project tutorial dot i do an ls and now in my source folder i'll have a manage.pi and a tutorial if you forgot that dot you would have a folder called tutorial you'd cd into that you'd have another folder called manage.pi in tutorial so that's just the difference there inside the tutorial i'll cd into that and that's where my asgi more poorly my settings my urls and my wsgi live all right all right so now that we have django installed we have a project created it's living in our directory structure let's actually access it so we're going to add our server's ip address to the settings.pi so i need to go into that settings.pi let's see that's in tutorial and i'm just going to go nano settings dot pi and here's my settings.pi i'm going to scroll down and i'm looking for this allowed host so i need to add either this which means every all i p addresses are allowed to access as all hosts can access us or normally you just put the ip address of your container or your droplet so i put the ip address for my container in here or my vps my droplet i'd save this control x y to save and then press enter to exit now that i've allowed this host mike server to access it let's go ahead and run it so i just do this python manage shop hi run server zero zero zero zero colon eight thousand so this zero zero zero is important if you just do the manage drop high run server let's look at that if i just do the manage shop pi run server i need to go up a directory yeah there's manage not pi if i just do run server it's going to start it at 127.0.0.1 which i'm not going to be able to access from my web browser so i need to quit this by hitting control c and i need to say hey run server but run it on 0.0.0 0.0 and port 8 000. now i should be able to go to my ip address in a browser so open up a browser just a second all right and there it is so i opened up my browser i went to my ip address and i went to colon 8000 so my ip address a colon and 8 000 and there is my django uh project so everything's working there now what we need to do is we obviously don't want to just have to type in our uh port number every single time so we're gonna get rid of that here but it's going to take us a little bit so we got to this point where step 6 is done we created our django project we saw our django welcome page step 7 is to install mysql alright so the first thing to do there is let's close out of our control c out of our server or python server django server i should say and then we're going to do app install mysql dash server mysql is going to be my database if you don't want to use mysql you don't have to do this you can skip a few steps here but i'm going to pause this and let mysql server install all right mysql is installed normally when you install mysql the next thing you'll do is it's mysql secure installation just kind of does some little security uh you know enhancements if you will to mysql it's super fast so we're gonna do it anyways just mysql secure installation is the command you're gonna run for this you're gonna get prompted a few times you can basically select yes for all of these so you know validate password do we wanna you know mysql to validate passwords we do for this um for this tutorial i'm going to set it to load just well i guess it doesn't matter because i should have a strong password for my user anyway so actually i'll just set it up to a strong so that's number two uh set a new password so i got a random password here let's see if it likes this i'm gonna just paste it in oh yeah apparently like that that's good enough do i want to remove anonymous users yes i do do i want to disallow root login remotely yes i do do i would remove my test database and access to it yes i do do i want to reload my privilege tables yes i do so that's it that's mysql secure installation uh very simple so you may as well do it now let's create a user and a database so we're going to go into mysql so you just type in mysql as your prompt as your command and you should enter the mysql command line interface so now we're going to create a user this is going to be the user that our project or django project is going to use to to connect to our database and then we're also going to create our database for our project then right here we're going to say hey grant all privileges so that database we just created grant all the privileges on that so that means create read update delete you know all sorts of other privileges that deal with database systems to that user on my project basically saying this user that i created has root level access can do everything has all privileges for this database right here then finally we're going to flush our privileges and all that basically means is reload the privilege table so that these changes that we just made right here take effect uh let's see so i'm gonna copy and paste some commands in here because i already have them saved on the little browser little notepad next to me so let me bring them up all right here is my user paste that in so create user django user in quotation marks at localhost they're always going to be at localhost unless you're connecting remotely identified by whatever password you want here's just some random password i generated so that was okay so now i need to create my database for the project create database tutorial then i need to grant all privileges on that database let's see grant there we go grant all privileges on tutorial dot star which means dot all so the tutorial database and all the tables inside of it grant all privileges to that to my user and then finally we just flush our privileges which reloads that privilege table and then we exit that's it you could go back in here you could show your databases if you wanted to to see that there's our tutorial if i use the tutorial database and i show tables there's nothing in there because we haven't migrated anything over using django yet but we do have mysql installed and everything's good there not trying to get this thing out of the way there we go all right so we need to connect mysql and django together now it's very important that you install these two packages using apt prior to trying to install mysql client if i were to install mysql client right now using pip and i'll do it for you i'm going to get all sorts of errors pip install mysql client and let's see i think i spelled it wrong pip install mysql client okay all sorts of errors right there that's because i don't kind of have the the packages you know at the system level downloaded so to if you see this it's just it's a very simple fix you just need to make sure you install these first so i'm going to do i only do this apt install let's see what is it python 3 dev there we go all right so python 3 dev was installed the next one we need is lib mysql client dev so app install lib mysql client dev yes to acknowledge that should be a fast install so i won't bother pausing it and there you go so now that i have those two installed now if i go re-run this pip install mysql client now it's going to work there you go and there are different ways to connect django and mysql i'm just using mysql client because it's easy and it works and i know how to do it so there it is all right now we need to basically tell python or excuse me django where to connect to our database so we need to go into settings.pi in settings.pi there's a constant called databases it's a dictionary and it says hey here's our default database we need to basically go in there and make it match this so let's bring up our editor here and let's go into a cd to where our settings are cd tutorial and let's nano settings so here is my settings i'm going to scroll down all the way until i see databases which is right here and all right so the engine is going to be the database engine all we have to do is change this from django.ddb.bakins sqlite3 to msqlmysql name is going to be the name of the database in this case i called mine i believe it was tutorial uh the next thing that's up is going to be the user which is the username that we created to connect mine was djangouser the next up is going to be password i think it's password maybe it's pass yeah it's password and then that's the password i created for the user which let me bring up my cheat sheet here and copy and paste it in it was this right here okay uh these next two are optional um they basically default it's the host which is localhost and the port uh which is 3306. those are defaults so you should be able to leave them off but i'll just put them in here anyways um if you were connecting to a remote host so like you had a completely separate server that was only responsible for running your database you'd have the ip address for your that other server here and then the uh the port would be 3306 unless you're running mysql on a non-standard port for whatever reason all right control x yes we're going to save so y and enter to exit all right now that we've connected we've got this set up let's go ahead and a check our uh django syntax so need to go back up to where manage.pi is manage.pi check okay syntax looks good so now i do manage pi migrate and it's going to migrate all sorts of stuff over and then finally we'll create our super user so manage.pi create super user i'm going to use my leave my loot leave my username as blank there we go that's what i'm trying to say and so it's gonna default to root email address we don't care about password i'm gonna set just mine to password just literally p-a-s-s-w-o-r-d right now it's gonna say hey do you wanna bypass that it's too common yeah i do this is just a quick little tutorial default build that's going to be destroyed once i'm done with it so i don't really care about having an insecure password for you know 10 20 minutes all right uh so super user was created successfully to show you let's go into mysql again and let's show our databases let's use tutorial and show our tables so now i've got all these tables here and if i want to see that super user i just created i can say select all from auth user which is where django stores your users let me tack on a dash g to list it out vertically and there you go username root password you can see it's hashed right um the datejoined was today is staff is one because it's a super user is active is one by default um and is super user is one so everything's working we migrated stuff we created a super user and we inserted that into mysql so everything looks good there now let's check let's actually try to log in so we're going to go in our browser go to our ip address colon 8000 forward slash admin make sure you don't forget the forward slash admin we're going to log in to the django admin panel so here i am i'm just going to say tack on forward slash admin to that url oh uh one thing i forgot before we do that is we need to run our server so python manage.pi run server 0.0.0.0 colon 8 000. there we go now i should be able to access it perfect all right root is gonna be my username password was just straight up password and i have access to the django admin panel right here everything's working we got our site hosted uh we have it connected to a database and uh we are able to interact with the database so everything's everything's going good all right now we just need to install and configure apache 2 so that i no longer have to manually type in the port at the end of that url or if i have an ip address or excuse me a url and dns set up correctly i won't need to type in my ip address i just type in the url for the website all right so let's get started here we're almost done this is kind of the last step all right install and configure apache 2 so let me close out of my django server and we need to do this so apt install apache 2 and which is going to install the apache 2 web server and then lib apache 2 mod wsgi dash pi 3. funny name there long name uh but and i'll just do yes to acknowledge all that the mod wsgi dot so wsgi is stands for websocket gateway interface excuse me web server gateway interface basically your apache 2 only understands text if you will you know raw text python has all sorts of python code in there it needs to do something so we basically need something to live in between apache 2 and our django python application so we have this thing called wsgi your web server gateway gateway interface it's a standardized convention that web store web servers use to forward request to web applications not super important all we need to know is that apache 2 is really going to route all its requests to this wsgi and then wsgi is going to run them via python and django all right so let's see here we installed it let's check our apache installation so if i go back to my server here and i delete everything including the port and just do straight up the ip address i should see this my apache 2 ubuntu default page that tells me apache 2 is installed and working correctly the next thing we're going to do here is we're going to go to etsy etsy being etc it's at root etsy apache 2 sites available there's a file there 0 0 default.conf and we're going to update it to serve our python file because right now that if i go etsy what is it apache 2 sites available right now if we go in here the reason we're seeing that default page in the web browser is because it's set to serve this so this is the default configuration for apache we're going to change this because when i go to my ip address i want it to serve the django project not that default page this is the portion of the project which will probably cause the most error slash headache the some of the error messages can be a little um hard to con hard to figure out and then you also sometimes have to worry about permission errors and or permissions and whatnot file permissions so normally if you have a problem in the build it's going to be in the hookup between wsgi and apache but i'm going to hopefully show you some stuff that'll help you here's an example of what you should be putting in but let's go ahead and i'll talk about it line by line first thing we can do is we can literally delete all this so nano i'm just hitting ctrl k which deletes all the lines all right so i say all right virtual host anything at port 80 which is you know the standard port 80 which is fine there first things we're going to add are our logs so our error log this is the apache error log and it's going to say hey when i get errors where should i log it well we created the log file so let's go ahead and or log folder so let's say hey when you have errors go into the django project site logs and i want you to i want you to log them there under the error.log file now the custom log which is everything else like your access log and whatnot i want you to do the same thing same exact location basically at least a location but instead of the error log let's call it access log and we'll see what these two look like and what they're actually showing you error log and access log are going to be very crucial for debugging in the future all right now what we need to do is we need to say we need to tell um apache where that wsgi file is and i'll save this real fast and show you this is really going to be if i cd to my root cd to my django project cd to the source and cd to let me see the name of tutorial this is where wsgi dot pi live so if i do a pwd for print working directory this directory is where the wsgi pi lives so this is where we need to tell j or apache hey go in there and run this file so let me do a nano etsy apache 2 sites available 000 default.conf honestly what i should have done prior to editing this i probably should have copied the zero zero default.com to like a zero zero zero old comp or something just so i could have saved that you know default apache configuration file but not a big deal all right so we need to say directory and the path to that wsgi file so not the actual wsgi file i'm not saying go into django project source tutorial wsgi.pi up here no i'm just saying hey here's where the file is located close my directory tag and i'm going to say require all granted basically apache needs to be able to access and run that file so it needs to have good permissions for that if it tries to access this and a that directory doesn't exist or apache doesn't have the right permissions i want it to basically throw me an error right off the bat all right now what we need to do and this is very this is kind of the three lines that make or break the project normally okay this can make or break it if you don't know where your wsgi wsgi dot pi file is but this is normally what's going on or what's going to cause you the most errors wsgi script alias okay the first parameter here is really the url that when apache sees a specific url it wants to serve this so this is basically saying root so anytime apache sees root url which is just our you know website or ip address what do i want it to really do i really wanted to go into the django project source tutorial and run that wsgi.pi file so your wsgi script alias the first parameter should just be root unless you're doing some other stuff and the second parameter should be the full absolute path to your wsgi.pi all right the next things you need to do is a wsgi daemon process okay you give it a process name this can be whatever you want i'm just going to call it the same name as my project okay the python home is going to be this is going to point to our virtual environment path so mine's in django project the env notice i'm not going further i'm not saying django project v-e-n-v bin scripts or anything like that i'm just saying here here's the root of the virtual environment the next thing i need to do is python path and the python path is basically going to point to the directory where manage.pi lives so mine is django project source and that's where my manage.pi is so the very last thing we need to add here is wsgi process group kind of not really important to talk about we just really need this here and we need it to have the same name that's up here for this statement process all right with this these three lines remember wsgi script alias first parameter should be root unless you're doing something else more advanced and this needs to point to the full absolute path to wsgi.pi daemon process has some random name it's not really important but the python home is going to point to your root level of your virtual environment folder python path is going to point to whatever folder holds the manage.pi and i have a little error right there see django prj pr c t whatever there you go django project all right so if i save this and we can check our apache um excuse me our apache syntax by doing what is it apache ctl config test i think it is uh so i got some error custom log takes two or three arguments a file name so something's wrong in my custom log oh i forgot the uh the second argument here uh it's the the path to the file we want the log to be logged at and then i got to do combine that's really it for that i should fix that so let's try it again apache ctl config test syntax okay so that's perfect we don't really care about this error right there at all now we need to restart apache so you do what is it service apache 2 restart should do it yep now let's go ahead and let's go to our url notice i don't have any ports running if i hit refresh uh i got pretty lucky in the fact that this worked right off the bat um if you have something wrong such as let me you know go ahead and change my um configuration file real fast and let's just say i have something like a path error so my python home doesn't point to my pro django project to just or my virtual environment correctly i could get something like this all right so it's thinking right now something it should be breaking itself it's still thinking obviously something's not working because this is just constantly spinning so we could check our axis log so i could do tail slash f which means follow and i would say django project log oh what is it site log access log so that something is kind of weird there we could check our error log uh it says right here we're unable to find some static file so something's going on so let's go ahead and re-change set our nano back up to what it's supposed to be django project then save it check our syntax and restart apache all right apache is restarted refresh this and it's working correctly one common error you'll also see is if you don't have this python path set up correctly so this is pointing to something other than or you don't even have it you don't even have the python path let's restart apache this should give us like a 500 error yep perfect and why did it give us a 500 error let's look at our access log so tail slash f uh django project site logs error log okay it says module not found no module name tutorial and you can look at it so this tells me if i see this no module name tutorial this tells me my wsgi file is working or at least the path is working correctly because otherwise i'd get a different error which i'll show and show you in a second so if you see something like this normally it just means you need to point your python home or excuse me your python path to be the directory that manage.pi lives in python path is going to equal to what is a django project slash source i think is all i need there should fix it yep there you go the other one kind of common one you'll see that should raise some flags is if you have if you don't have your python home set at all so again that's your virtual environment set it all or it's to the wrong thing restart apache this is going to give me an error i look at my errors by looking at my server log and this so module not found no module named django mean means something's wrong with your virtual environment up here no module name tutorial so one of your actual project directories that means there's something wrong with your python path oops so we'll fix those real fast get it back to where it was working python path is good python home needs to go to our virtual environment which is just at oh let's see django project slash then all righty so that's all working cool the very last thing is static files i'm going to do static and media files all at once here but let's see what happens if we set our i'm the cd to i guess i'm already here so let's take our settings.pi and let's change our debug to false because you never really want to run debug as true in environment or in production debug should always be false but when we do that and anytime you do changes to your code using this setup you're going to have to restart apache for those changes to be reflected so restart apache this right here off the bat is once apache restarts i'm going to get not found this can be very confusing um the reason we're getting not found is because i do not have a url for my root path all right so and what django does basically if it's in debug mode and you don't have anything pointing to uh the index page it'll just create you that default working page that default you know page so what i need to do is path and then we'll give it some view i need to define that view and give me a second i need to pause this because actually i don't remember how to define a single function based view in python and django all right i went to the django documentation and just kind of copied some of their stuff but so here it is this is just a simple page that's going to return just some random text that says you're at the polls index who cares and then we're saying hey we define now a path for root or basically nothing and it's saying anytime you hit root go ahead and run this index so now that we do this we wrote some code let's restart our server or apache server now when i go here it shows me my you know my my view for that page but what i was getting at for the static files the one not is if i go to admin you can look and you can see it's obviously not serving my files correctly i have no css or anything i can look here and i'll get all sorts of you know 404 errors and whatnot that it can't load specific files so to fix this the reason this is happening is django web server and debug will serve all your static files for you however in production it will not when debug is false it will not so what we need to do is we need to we saw this here we already made the directory static it's going to live at so we need to go into our settings.pi our settings.pi here and we need to create this static root variable and then i'm also going to create a media route and then we're also going to have to update apache to say our con conf file to say uh you know this is where our our static files and our media files are located at so let's do that real fast and take more than a couple of minutes and then we'll be done so let's nano settings dot pi by page down here to the end here's static url i'm just gonna post uh put my code right here so static root is gonna be an absolute path django project uh site public static i think that's what we said yep media roots gonna be the same django project site uh well i guess not the same it goes to media instead there you go so static root is going to be when i collect my static files python or django is going to copy everything over there so boom save it run it and now if i go back up to where manage.pi is so real fast i'll do an ls for django project site public static first there's nothing in there but if i say manage.pi collect static okay 128 static files copied to my static root path by ls there they're all right in there under admin i can ls and you know admin in there and see all the different files but i'm still not good because apache doesn't know about those files yet apache doesn't really know about that static directory so let's go into cd to etsy apache 2 sites available and let's nano the comp file now we just need to come up here and we need to basically say okay alias whenever you see static url what i want you to do is i want you to go into the django project site django project site public static okay it's a directory that i'm going to make sure you are granted access to directory is gonna be django project uh let's see django projects site public static yep same thing there and we're just going to say require all granted and it's all granted lowercase okay and let's restart our i guess we could do a config test that's good and service apache to restart and now if i go here to admin there we go now we're getting our static files served correctly the only other thing you'll want to do is basically do that same exact thing for your media files if you are going to deal with media files all right so you can just basically copy and paste this paste that and we'll change this our alias is going to be media so anytime hey you see root media in your url i want you to go into the django project site public media directory to find that file and that is really it service apache 2 restart and i don't really have anything i can upload here to test that the only other thing i would do before you're fully done is i'd set our permissions for the site folder the public folder to have apache have full access to it so if you cd to django project site public so this is all publicly accessible stuff static files and user generated media files so what i would really do is i'd go up one and i'd basically do a change own dash r which is this is change ownership recursive we're going to www.datacolonww.data before i do this i'll actually show you what i'm talking about if i do an ls-l now media and static folders are owned by root i don't want that i want them to be owned and accessible by uh apaches group which is www data so now i'll go back up and i'll change that whole folder so change own dash r www.data [Music] public okay now www.data holds it has access to public it's the owner if i go into public and i look at it media and static are the same just to make sure everything's working let's again just restart apache too make sure i can break anything with those permission changes and refresh and we are good to go so that is the basics of how to deploy django using ubuntu 20.4 and uh configuring your um comp file again the 90 90 of your issues are going to arise from your um from your setup here in in your com file so chances are if you run into some error it's got something to do with here most likely it's either these three lines here or it is uh let's see where's our wsgi let's see oh this actually should have said wsgi.pi but that is apparently working yeah this right here it really should have said which is fine um because i'm basically saying hey you're supposed to have access to everything in tutorial really it should have been this files and then this specific file that i wanted wsga.pi but it's obviously working without that so you know if you feel like doing this go for it if you don't that's fine and let me restart apache 2 and just make sure it's working with that change there we go okay all right that's it so cool hour long video i'll throw some timestamps in the uh in the description here to make it a little bit easier for you but hope you enjoyed it and uh like subscribe if you do if you got any questions which i'm sure you're running into some problems somewhere just drop into the discord and shoot me a message thanks
Info
Channel: Project Full Stack
Views: 218
Rating: undefined out of 5
Keywords: django, ubuntu, apache, mysql
Id: UY_UIH89elA
Channel Id: undefined
Length: 62min 58sec (3778 seconds)
Published: Tue Oct 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.