Hosting a Django Website with Digital Ocean

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this video I'm going to show you everything involved in publishing your website on the Internet so whether you are an experienced developer you already know how to build websites or whether you've been following my course which I made on Django and web development this this video is going to be kind of an all-encompassing everything you need to know about hosting your website on using digitalocean using the host provider digitalocean i'm going to show you everything from creating an account choosing what kind of server what kind of system logging on setting up ssh private keys setting up a firewall I'm just reading my list here disabling password authentication so using SSH to log in installing Python choosing the correct version of Python installing pip creating virtual environment using system sockets and setting up G unicorn and nginx which is what you use to run your server and I'm host or I guess I run your server would be the way to put it testing socket activation testing nginx registering a domain name so that means choosing a domain name that your website is gonna have just like my website Coney with Mitch comm so you get an IP address from digitalocean then you buy a domain name and you can point your name servers to that IP address setting up HTTP on your server on your website so that it uses HTTPS and not HTTP and then also object storage with Jango so this is actually done with Amazon Web Services I didn't know this but digitalocean has their own sort of object storage so it's a place to store images and what other other kind of media static files basically for your website digitalocean is something called digital ocean spaces s3 that does that but it actually uses aminah Amazon Web Services so that's kind of interesting but it's it's much easier to use than using Amazon Web Services alone I find so I'll show you how to set up all that stuff decoupling settings for security so that's like having a separate settings file instead of using the actual settings file for Jango that way it's more secure and sending emails so like in the in the course I showed you how to do a password reset so you send a password be set to the user if they forgot their password but that wasn't done using a production environment so this is going to show you how to do that in a production environment and then when this is all done and your website is hosted I'm going to show you how to build a REST API also and for those of you who are familiar with my content I'm going to be showing you how to build an Android app after that to communicate with that REST API so lots lots of stuff lots of stuff and like I said most of the things that I just mentioned I'm gonna cover all in a single video I'm gonna put timestamps down in the description for kind of the points the check points I guess if you will and you can just keep referring back to this video if you ever need to publish a website using digitalocean you know whether it's from one of my courses or from your just your own websites that you're building on your own also there will be a link in the description for a digitalocean signup link if you want to support my channel this is a completely free course that I made the only thing I really get out of it is no I mean YouTube ad revenue but that's almost nothing so there's a link for a digital ocean sign up make sure to click that below there's also a link for a link to namecheap.com which is where you should buy your domain there'll be a link down there if you use those links I get like I don't know a couple bucks per sign up or something like that if you guys use those so I would appreciate that alright so that was the intro that was long there was a lot of stuff but now let's let's get into the content and the first sort of thing that I'm going to talk about is a prerequisite so what do you need to know before you watch this video what what's what's everything you need to know basically so you don't waste waste any time so let's talk about prerequisites so the first thing I want to talk about is why am I using digitalocean to host this website there's tons of hosts out there there's you know some of them you might be familiar with is like Hostgator Bluehost I don't know those are the ones that come to mind anyway so and so now let's talk about why I like digitalocean and why I use digitalocean for any of my hosting needs today so the first thing is they have excellent customer service I've had nothing but good experiences with their customer service and this is very important for any kind of any company or any host provider in particular because you very often run into problems and being able to jump on a live chat with someone really quick and just ask some simple questions is really handy could save you a lot of time so that's point number one second actually this should have been probably the first point is they have excellent documentation like they have the best documentation it's not confusing it's straight to the point there's great tutorials to keep taking you through whatever you need to do which i'm going to be referring to throughout this video i'm going to be showing you you know the ones that I followed to do the tasks that we're going to be doing that way you can very easily refer to those in your own development on your own time so we had excellent documentation like among the best documentation I've ever seen personally I think so that's that should have been reason number one not the customer service but I guess they're sort of the same category so it brings me to the third point the pricing pricing is very reasonable my website coding with Mitch comm for example gets between a thousand to 1,500 visits per day currently usually and I've never paid more than I think 14 dollars a month for server fees now let's not including serving the static files and serving the media because that's a different cost completely that's using Amazon Web Services but just strictly the server fees is only about fourteen bucks a month and they have had nothing but good service provided to me so that's really great and that brings me to the fourth point their service their service is just overall really great I talked about customer service already but just I I mean like they're the raw service that I'm paying for I've had nothing I've had no bad experiences you know my serve my servers never gone down it's never done anything weird that I didn't want it to do it's just very been very consistent and very good so yeah nothing literally nothing but good things to say about digital ocean so that's why we are going to be using digital ocean and I'm sure if you asked the developer community if you go on forums go on reddit whatever everybody's gonna say the same thing about digital ocean now let's talk about cost so you are gonna need a credit card hosting is not free to put a website up on the internet is not free at least as far as I know it's it's not for you I've never seen anything any free service out there maybe there's like student things you could get but I'm really not sure so you are going to need a credit card because you need a digitalocean account and you need to be able to pay for your server fees like I said if your not hosting any kind of media files it's only gonna cost you about 1314 dollars Canadian a month which is I think about eight or nine dollars US a month so very very cheap like you you're never gonna do anything cheaper in terms of your development life so it's worth it it's well worth it so you can pay for a credit card you can play the put a credit card or you can pay using PayPal I believe I'm not a hundred percent sure on that I pay with a credit card and yeah like I said it'll be 13 14 bucks a month which is about $9 u.s. around there and you'll also need to buy a domain name so digitalocean is the server that the website will be hosted on that will just get you an IP address but if you want to point it to you know coding with Mitch comm or coding with coding with whatever your name is com whatever your domain you choose is gonna be you need to buy that and we're gonna be using a website called Namecheap to to purchase that and like I said I have a link down in the description of this video for to point you to Namecheap and i get like a couple i don't know like a dollar or something for a referral fee I don't know but anyway hosting the domain name itself is very cheap the one that I bought for this course just to show you is only $1.00 per year so very very low cost all right so the last thing I want to talk about is the software that you're gonna need to watch this video to move forward with this video the only thing you need is you need something to SSH or do FTP on to your server so something to log on to your server system which is going to be a UNIX system and something to send files to your server which is done using something known as FTP it's like File Transfer Protocol I believe is what it stands for so the the software that I recommend using is called MOBA X term and I'll bring it up on my screen here so this is what the software looks like basically you just use it to log into your server so you can do things you can upload files I like this because you can do both FTP and login to the UNIX operating system all from the same application some of you might be familiar with a very popular SSH piece of software called putty that's a very common one but this is just I think this one's just better like it saves your passwords you could have multiple tabs open you can it like I said FTP and SSH at the same time it's just it's pretty convenient it's free so you can just go to this is the website right here MOBA X term MOBA TechNet download it very simple and we're going to be using that throughout the video here so whether you use Phut putty and in a different FTP use whatever you want this is just what I'm going to be using so just keep that in mind alright so now we're kind of done the intro we're done the prerequisites let's work let's start working on setting up our server so like I said we're using digitalocean and there's a link in the description of this video to link you to digitalocean to give me some credit for pointing you to digitalocean thank you very much if you do choose to do that too click that link because youtube doesn't really pay very much and this is a this is a free course so thank you if you do click the link in the description for signing up to digitalocean and that should take you to this page I believe then you want to click sign up and create an account obviously I already have an account so I'm just gonna log in so when you log in this is kind of like your dashboard that you have and you're not gonna see any money up here yet because you obviously don't have anything hosted you don't even have your credit card setup or anything like that this is actually the the project for coding with Mitch comm it's just a very very small system I only use 512 megabytes of RAM with a 20 gig disk so very small and it and like I said my website gets between a thousand 1,500 visits a day and everything is excellent so don't you don't need a lot to be running your website at least with that kind of traffic so once you come to the dashboard the first thing you want to do is you want to create a new project so I've already created another new project here it's called Coney with which blog course if I was to create a new project you just enter a name a description select the purpose you can probably go website or blog which is what we're going to do and then create that project and then you should be looking at this this screen right here so you'll have pretty much kind of nothing in here and I'm not sure you're gonna want to also probably set up your credit card information so you want to go I think it's an account and you can go to billing and you can set up your credit card information but because you will need that you start your hosting obviously I'm not gonna click that because I don't want you to see my credit card stuff but that's where you're gonna want to go once that's all set up come back to your project and let's create a new droplet in digital ocean basically a server or a service that you want to use they're called droplets so I believe you can you can get it from up here too yeah you can go create a new droplet or you can just get started with a droplet right here oh and just kind of as FYI this is the documentation that I'm gonna be following for this video how to install django web framework on Ubuntu Ubuntu Ubuntu I'm not really sure how to say that 18-point Oh for basically I'm following this entire documentation I'm gonna be taking you through that so if you are curious you know here here's the link and you can you can visit that or you can just search in the tutorial section search digitalocean and look for this this link if you if you want to follow along kind of with the video or whatever so let's let's create that droplet so first thing is creating or choosing a system I'm gonna be using Ubuntu or Ubuntu whatever it's called like I said 64-bit system 18 version 18.04 choose a plan standard plan it has it to $40 a month options selected by default but we can go way back and we're gonna choose the $5 a month per option which is plenty for us like I said coding with Mitch comm only uses 512 megabytes of RAM or CPU and 20 gigs of disk so this is way more than even my website has they've actually increased the lowest possible option since I made Koenig Mitch comm so this will be plenty for you nice five dollar-a-month option we want to enable backups which cost an extra dollar per month this is a very convenient service because if you ever have to reset your server kind of start at square one or not square one but you know say you screwed up your server and you want to go back and revert to what it was like the day before that you can use backups for that so it's really handy definitely something you want to have because if you screw up your server and chances are you're going to I know I have having these backups is pretty pretty awesome looks like it does once a week though but either way better than not having it next is choosing your data center region I like to choose New York because it's more central to the typical people who are going to be using your website like so it's on the eastern side of North America which is close closer to Europe if you choose stands for in San Francisco yeah you get you know all of North America but the other side of the world is farther away so I like to choose New York because it's closer to Europe and Africa the other side of the world I'm pretty sure they do automatic load balancing anyway so it will like find people it'll use a data center that's closest to the people who are direct the traffic is coming from anywhere so I don't know if that matters that much but I choose New York every time some other options I'm not going to choose any of those we this is very important we want to use a one-time password to sign into your server and I want to use a one-time password because later we're going to be enabling ssh authentication or ssh private key authentication and in order to do that in order to enable that we need to have one-time password setup so do not click this because later we're going to be enabling ssh private key authentication later in this video so it's much much much more secure so one-time password is what you want for now and then we're gonna do that later we want one droplet you can choose to choose a name pretty much this just describes what the system properties are doesn't really matter and you can add some tags so like this could be like you know coding with Mitch blog course or whatever I'm just gonna leave this blank have no tags and you assign to a project and then you create the droplet and now digitalocean will create this system on their servers all right so once your system is ready that means that you can actually log into your server so what you want to do is you want to copy the IP address and we're gonna go to mobile X term which you should have downloaded and installed by now and you'll also have an email you'll also have got received an email with your your password to login with so I'm not going to show my email but you would just log into your Gmail or log into whatever you signed up with a digital ocean with and it will send you your SSH password which would be like a long long long of like random letters and numbers like a whole bunch of stuff that'll be your password so use that and copy that and we're gonna go to MOBA axe term and login actually I wasn't going to show you but I guess it doesn't really matter it's just like the IP address the user name and the password I'm going to change the password anyway time you're watching it so so here is what the email that you'll get so what you'll want to do is copy the IP address which you should have already we're going to go to MOBA axe term go to session create a new SSH session the remote host is the IP address we're going to go specify user name and type in root because that is the username that you were emailed as you can see there and now I'm going to copy this password so I'm copying that I'm gonna go back to my mobile xterm clicking okay and now it's gonna ask me for the password so because I have the password copied I'm just going to paste it in nothing will show but it actually was pasted in so if I click enter it's gonna say do I want to save it I'll say no and there we go I'm signed in to the server so now it's asking me for the current password again so again I'm going to right click go to paste click enter now it's saying enter a new password and I'm going to enter a new password for my server which I'm not going to tell you obviously and there we go now I'm logged in so so this is the server this is your new digitalocean droplet on a UNIX system and it's ready to go now usually with mobile x-term you have an ftp setup over here but because it's the first time i logged in it's not set up so what I'm going to do is I'm actually going to end this session and I'm going to create another a new session so that same IP address double-clicking on that it's going to ask me for the new password that I just created I'm entering it I'm gonna say yes save it and now I'm logged in and now I can also use FTP on my server so I can drag files in and basically do whatever I want this is why I like mobaxterm because you can get access to the terminal and you can get access to FTP at the same time so now that we have our server set up the next step is we're going to be setting up a firewall and setting up SSH private key authentication so we're going to disable the ability for users to sign in using a password the only way they're going to get access to get be able to get access to the server is through and a private key so it's a much much much more secure way to set up your server and that's what they actually recommend in the digitalocean documentation and just for your reference this is the one that I'm going to be following to set up the private key authentication I think this is the firewall stuff too so this is the one right here just kind of for your reference all right so the first step is we need to create a non root user right now as you know I'm signed in with the root user you can see from the brackets up here I need to create one that is a non root user because that's just how they recommend setting it up on your server so I'm gonna type add user and this user's name is gonna be Mitch so now it's saying enter a new password for Mitch I'm just going to type a password enter the tip of the password again full name I'm just gonna click enter on all these because I'm not gonna enter any of those set those properties is this information correct yes it is so now I have a new user named Mitch you can double you can check this by going into the home folder and there'll be a folder denoting the user that was created and just so you know this so basically this user that was created they're only going to be able to alter files within the Mitch folder so in home and in Mitch only the root user can change any files but Mitch this new user that I created he's only going to have access to this folder right here which is where we're going to be putting our Django project so kind of just adding more security basically to your application so I need to give Mitch some permissions so that he can actually do some stuff so I'm typing user mod - AG sudo Mitch which gives him permission to pretty much do everything it's pretty close I'm not sure what they actually are but you can basically do everything except for alter the files outside of the Mitch folder and yeah like obviously alter the root users properties but you can do a lot of stuff now it's time to set up the SSH private keys so the way that I'm going to do this is I'm going to open a new command prompt and I'm gonna generate a a public public key I'm generating a public key and then I'm using that public key to login to the server and so I'm logging in as a private key to the server so I'm going to type SSH key gen and if this is confusing just follow along it's going to make sense in just a sec here so SSH key gen it's saying enter where you want to save it I'm just gonna save it there enter a passphrase no passphrase no passphrase so enter three times and it will generate that key so that key gets generated you saw it tells you in the command prompt users users ssh ID RSA so if I go into users users SSH this is where that private key was just generated I should have actually I should have actually renamed this to Mitch I can rename this to wait ID RSA yeah I'll rename this one to Mitch because this is going to be the private key or sorry the public key for Mitch so whatever you the user's name that you just created that's what you want to name that and the you should also need to generate one for the root user so I'm gonna go to the command prompt again I'm going to type the exact same thing so I'm just pressing the up arrow on the keyboard SSH key Jen once again just gonna click to enter three times that will generate it and there's that second one I'm gonna rename this root because this is going to be the private key for the root user so I have Mitch and I have my root user and those are the sort of the public keys for their authentication so now I need to tell my server what these public keys are so first I need to I'm authentic ated with the root user so I'm going to right click on this go edit with notepad and here is that that key what that was generated I'm gonna hit select all so ctrl a I'm gonna copy that I'm gonna go back to my server go into for the root user you got to go into root going to SSH and open this authorized keys file so open with default text editor is what you want to do and that's going to open it with the default text editor that mobile xterm uses which will look like this and I want to paste in that key and click enter and or sorry and save that so that's going to save that private key or the public key as a viable authentication method on the server and now I want to do the same thing for Mitch so the root users is in the root folder but Mitch has to have it inside of this folder right here so I need to create a new file this is going to be called authorized keys click enter to create that I'm going to right click open with the default text editor like we just did once we have that I'm going to go back to Windows go into the the Mitch the Mitch file here the one that is I think it's telling me that it's a Microsoft Publisher document so just right-click go to edit with notepad once again select all copy and I want to paste that in and save that so now I have these two public keys saved on the server as authentication methods or as viable authentication methods so I can close that and of course if you're watching this I'm gonna change these keys afterwards so I might even delete the server anyway so don't try to log in it's not gonna work and now now that I've done that I can actually authenticate with those keys and you can test that using a command prompt so I could do like SSH and I can do at Earth sorry SSH the users name is Mitch and I can reference the IP address so the IP address is 67 point two oh five point one eight six point five and what that'll do is it'll look in the SSH folder on my Windows machine so it'll look inside this folder and it will look for any public keys that match something on the server so if I click enter it says the authenticity of blah-blah-blah-blah-blah can't be established you want to try and connect anyway it says yes and then asked for Mitch's password so I'm gonna type in Mitch's password and there you go now I'm logged in to my server so that would have been possible if I hadn't added those keys and you can also test it using the using mobile X term so if I start a new session here I can go create a new session SSH the remote user is going to be that IP address I'm just going to copy that from digitalocean copying the IP address pasting that in here the user's name is going to be Mitch I want to go to advanced SSH settings and inside private key here I want to find Mitch's private key so here's the window it brings up to notice I'm in the SSH folder for my computer I want to click the one that says Mitch go open click OK and it's gonna authenticate it looks like the server refused the key interesting so that's obviously wrong that should not have happened oh it's actually because I put the authorized key file in the wrong directory so I need to go to back to the root I'm in home Mitch and I want to create a new directory with dot SSH and I want to move authorized keys into SSH so I'm going to just CD into home Mitch and I want to move that directory so I can say sudo move the authorized keys file into home Mitch SSH so now if i refresh this notice that authorized keys file is gone and if I go into SSH there's the authorized keys file for Mitch so I'm going to close this session I'm going to close the Mitch session that I have open here yes and I'm going to open a new one so if you go to edit session you should still have the private key referenced right there or the public key sorry if I double click this it should authenticate using the SSH key and there we go I'm authenticated so no password is needed now it's authenticating Mitch using SSH now before I move on I want to do the same thing with the root user so I'm going to end this root user session so I'm closing this and I'm going to go to home and try to I'm going to go to it right click on it edit the session use advanced SSH settings reference a private key and click on the root user right there the root users public key sorry go ok and now try and start that session so now both Mitch and the root user are able to authenticate using SSH keys instead of using their password so now that now that I can do that and I know that I can log in with the root user using an SSH key I know that I can log in with Mitch using an SSH key now I'm going to disable a password authentication on my server now you definitely don't want to do this until you're sure that you're able to authenticate with an SSH key because if you do this and you just say the password and you can't get on with the SSH key you're locked out of your server you're gonna have to shut it down and create a new droplet because you won't it won't be possible to get back on to it so make sure that you're able to do that and also keep your SSH keys somewhere safe don't just keep it on onedrive have a backup saved on a USB somewhere have it stored you know on somewhere else basically because if you lose these you will not be able to get back on to your server so just keep that in mind okay so I'm I'm on the root user session now I'm going to disable password authentication on my server so I'm going to write sudo nano nano means it's like to edit a file in UNIX land and I'm going to reference where that file exists so that's in the et Cie folder in SSH SSH SSH D underscore it was config so that's going to open up that file so I can edit it so I just wanna I'm just using the down arrows to go down to where it says password authentication so going down right here so password authentication it's set to yes I'm gonna set it to no I'm pressing ctrl X that'll bring up this little menu down there I'm pressing Y for yes and then enter now that saves the file so if I was to reopen that file by doing the exact same thing and if I was to scroll down to where it says password authentication notice that it says no so I'm pressing ctrl X and I know that now I won't be able to log in with a password so just keep in mind that if you get locked out of your server at this point you're basically screwed you have to restart your droplet so make sure to keep those those private keys somewhere where you're not going to use them lose them alright so now we are gonna set up a very basic firewall and again I believe if you refer to the digitalocean documentation I think it's part of firewall I think it's yeah setting up a basic firewall so that's going to be just kind of following these steps here it's very simple it's pretty pretty straightforward so all you got to do is sudo ufw app list so that will show you what kind of available applications there are there's just one at this point I want to make sure that SSH is enabled so you can see that it's available now to make sure that it's enabled so ufw allow OpenSSH so it says my rules are updated I want to now enable the firewall so ufw enable this may disrupt current connections yes I want to proceed cool the firewall is active and enabled on the system on system startup and right now I want to do sudo ufw status and that will show you what is currently active so that's open SSH is active and I have an active firewall so the next thing we're going to do is we are going to install and enable Python 3.7 right now on the server if you type Python you can see that actually it's Python 3 you can see what version it has pre-installed it comes with three point six point eight so currently you could use three point six point eight but because in the course in the Django web development course we were using version 3.2 stay consistent and also use three point seven on our system so it's pretty simple to do basically there's two steps I need to install Python and then I need to tell the server that I want to use than the Python three point seven instead of Python three point six so first I'm going to write sudo apt update and that basically just updates a whole bunch of stuff on the system they recommend doing that in the documentation all the time so I'm just going to do it ahead of time once these are done all of these updates are done now I'm going to install Python seven so sudo apt install it'll be Python 3.7 and yes I want to continue that's going to install that for me and once python 3.7 is installed and I can confirm that in just a second I can confirm that by writing python 3.7 and there you go it's it's it works so it actually does something that means that you can tell up from what it says right here at Python three point seven point three that means it's installed now that its installed I need to tell my server to use that version instead of using Python three point six by default so to do that you write sudo update - Alternatives whoops as an equal sign so - alternatives - - install and I want to reference the path for where those Python versions are installed so user bin Python space Python and then user bin Python three point six point two that's the version that's already pre-installed so clicking enter there oh whoops so there should be it's not point to at the end its space too so what I'm doing is I'm telling this this file on my system to give Python three point six a priority of level two so I'm doing I'm entering there and it says that it was updated now I want to do the same thing but I want to reference the new version of Python that I installed so three point seven and I want to give it a priority of one so this first fight Python three point six is getting a priority of two this one saying Python three point seven is getting a priority of one so clicking enter so now that's that's updated in my configuration file now the last step is I need to set the version that I want to use so once again update alternatives and - - config Python so now it's going to give me an option to choose which one I want to use it says that python three point seven is selection number to see their selection number two there's Python three point seven I'm hitting Enter there and now it now if I was to just type Python notice that it uses version three point seven so the default version of Python that will be used on my server will be version three point seven we're almost ready to start setting up the Django stuff so to start using that we need pip if you remember from the course we use pip to install different Python packages or modules I think you call them so we need to install pip on our server so sudo apt-get install why Python 3 - pip that is the command to install pip for python 3 so I'm clicking enter it's going to install a bunch of stuff and when it's done we're going to confirm that pip was installed ok so now that that's all done let's check that pip was installed so I'm typing pip 3 and if it doesn't say anything you know that you have a problem but if it gives you kind of all these options that pop up you know that pip was correctly installed on your system so now that now the pips installed we need to install some Python packages with pip so the first one is going to be the Python virtual environment which we used when we when we worked with our development environment we're going to be using it for some testing on our server before we actually launch the project so I'm typing sudo H - H pip3 installed virtual environment clicking enter and that's going to install a virtual environment on my server now I want to CD so I want to navigate to the directory home Mitch so if you want to see that on the FTP that means I went into home and I went into Mitch so now I'm inside this directory right here and you can see it says home Mitch and you can type LS to list any of the directories that are inside here but there is none as these are all hidden directories that won't be listed so at this point you could either you could use the Mitch session or you could use the root user session because we're going to be inside of the Mitch directory so the user Mitch will have access to everything in here but I think I'm just gonna stick with the root user session but do do whichever you like so now we're going to start setting up our Django project so I'm gonna right make directory so mkdir this is going to be Django project directory and it's I think it's very important that you name them exactly the same as I am because later when we do the G or G unicorn set up for the socket and the service it's going to be easier for you to follow along if you name everything exactly the same as what I'm naming it so you can leave them differently but I'm just kind of giving you trying to get trying to help you out here and prevent you from running into issues just name everything the same and then maybe later when you launch your next website you can name things differently so make directory Django project now if I type ls' you can see that I have a new directory in there called Django project dir directory so I'm going to navigate into that directory so Django project dir if you didn't know what I did there I just typed DJ and then tab and it fills it in for me so I'm going into that directory now this directory is empty as you can see if i type LS so now i want to create that virtual environment so virtual ii + v i'm going to call it Django project environment so once again you should probably name these the same as what I'm naming them so a virtual env Django project environment and that is going to create a new virtual environment inside of my Django project directory so if I right LS now you can see that there's that Django project environment if I see the into Django project environment and I use LS again I have been include and Lib so if i refresh this there's that Django project environment or Django project directory Django project environment and inside here is how you activate the virtual environment so there's that activate file right there that will it be actually how you activate it so what I want to do is I can actually CD back so CD space dot dot will bring me back one directory so I'm no longer in the Django project environment I'm in the Django project directory and now I can activate that virtual environment by writing source Django project environment slash bin and referencing that activate file that I just talked about so if I click enter now we get we get our virtual environment activated and you know it's activated because this is surrounded in brackets over here so that's how you can kind of confirm that it's working correctly so the reason that I'm using a virtual environment is because we want to do some testing before we actually install all of the things that we need to on the actual server so in we're going to work on the virtual environment first confirm some things are working and then work on the actual server so I want to install G unicorn first so pip3 install G unicorn and if you don't know what G unicorn is I don't really have a good way to explain it I'm not really a UNIX guy I'm not a server guy but basically it's needed to host your server if you want more information on it just google it Google will be able to give you a better answer than I will next I want to install Django so pip3 install django and we're using version two point two point two so i'm installing that and now that we have this we have g unicorn installed we have django installed we have everything that i need to move forward and test to make sure that everything is working correctly so i'm going to do CD space dot dot which will take me back a directory so i'm inside of the Mich directory now and now I want to create that project so Django admin start project start project and it's going to be called my site so remember to name things exactly the way I'm naming them Django project directory so I'm creating a new project called my site inside of Django project directory so I click enter and I do LS now I have that Django project directory still there if I CD into Django project directory I have the manager py file and I have that my site so I can also just check that out through the FTP so if I go into Django project directory there's my site the virtual environment and the managed py file so I'm seating back into Mitch which actually I don't need to do you don't need to CD into the Mitch directory it doesn't matter because now at this point we're ready to move our files from the project that we built in my Django course in my web development course we're going to move them onto the server but before I actually do that we need to edit the settings py file so I'm gonna go into settings py open with the default text editor for the server so it'll it'll look something like this and we need to change a few things so inside of the aloud host section right here I want to allow the IP address for the server and I also want to allow localhost so there's localhost now I want to get the IP address for the server so I'm going to my digitalocean droplet I'm going to copy that IP address I'm gonna paste it in there so that's the first thing that we need to do next we need to change a few other stuff so inside of installed apps we need to add all the Django apps that we built in the course so I'm going to write a little heading here that just says my apps and there's personal is one of the apps another one is account and the other the third one the last one is blog or it's yeah it's blog not blogs so those are the apps that we built in the course next I need to set the auth user model so auth user model equals account dot account and of course we haven't dragged these into the project yet but we're going to in just a second I just wanted to edit the settings file first coming down into templates we need to reference the directory for where the templates are going to be stored so OS dot path join the base directory and then comma and its name is templates so making sure that I didn't make any spelling mistakes there os path joined base directory which is referencing the base directory up up top here templates is the name of the folder all the templates are so that should be good app directories is true and the static files stuff we also need to type in down here so I'm actually just going to copy this from sublime text from our project so I'm going into my site going into settings scrolling down to the bottom and we don't need all of these but I'm just going to copy those paste those in we don't need the media URL we don't need the media root and don't need media there so all I have is static files directories referencing the static directory there's a static directory path there and then static CDN which we're going to be removing later when we do the aid Amazon Web Services stuff but just for now we need to add those so I'm pressing ctrl s to save that you can see that it's being saved down here in mobile X term and there we go oh also actually before we move on you're you're going to want to change debug equal to false when you're in a production environment just for now I'm gonna leave it set it to true but just keep that in mind if I forget to tell you later make sure to change that to false because if it's set to true it's a kind of a big security risk they can see a lot of properties on your server if there's ever an error or something like that so only use that very selectively if you need to debug something but for now I'm going to be leaving that to true alright so now let's drag in the files for the server so I'm going to the Django project directory and I want to drag in all the files that we built that we built in the web development course that I made so I'm going to where I have the Django project so Django project coding with Mitch blog course I'm going to go into SRC and basically I just want to drag everything so I'm going to select account hold down control click on blog hold down control click on personal click on templates I need requirements txt so I'm going to drag all of those into my server right there it should take a few seconds to get keep in mind you must be on the root user to be able to do that dragging all that stuff in and I also need to edit the URLs PI file so I need to I think I just want to call this actually copy the whole thing in so I'm pressing ctrl or actually I'll drag it in so as soon as this is done I'm going to drag in the URLs PI so I'm going to my site I'm going to delete this URLs PI so I'm deleting that and now I'm going to drag in this URLs PI now I'm going back a directory and I'm going to create a new directory called static so I have that static directory that's created and I want to drag in the coning with Mitch logo into the static directory so I'm going into static I'm going into static on the server and I'm going to drag in this logo because we will need that on our website and I believe that is it oh I need to actually remove one more thing so go into my site open up URLs PI so opening with the default text editor and I want to remove the media root stuff down here and I'm just going to press ctrl s to save that and that should be should be good so there there's all the files that we need on our server now I'm going to install the requirements so all the requirements that we use for the project so I'm going to pip3 install - our requirements I'm in the wrong directory so I need to CD into Django project directory now I have access to the manage type py file so are sort of the requirements file so pip3 install negative our requirements txt and I'm clicking enter now it's going to go through the requirements file and install all the requirements for the project now it's time to migrate so python managed py i'm going to type my grades actually I might have to make migration so I'll type make migrations first looks like I have so I did make a typo inside of settings py so if I open up settings dot py go to templates it's telling me that I have oh I forgot a bracket here so fixing that pressing ctrl s to save that now I'm going to try to make my grace again as soon as this is saved so make migrations no change is detected okay Python managed py migrated that's gonna set up my database and everything that I need in there now I want to write Python magic py collect static which will collect all the static files so now if I was to refresh this I should have a static CDN folder that gets generated and there's all the static files so that's good now I want to sudo ufw allow eight port 8000 so I'm telling my firewall to allow port 8000 now I want to run my server so python managed py run server zero point zero point zero and remember this is just for testing we're just testing G unicorn right now later you will not need to actually run the server to to get to keep things working it just automatically runs using GU in our current G unicorn and nginx so now that that server it now our server should be running so if I go to my IP address so I need to copy my digitalocean IP address go to port 8000 now my website should be live ok so that's good we manually ran our server now we need to make sure that G unicorn is working correctly and it's able to actually run the server alone without us having to to do run server so I press ctrl C to stop the server and now I want to write G unicorn bind zero point zero point zero part 8000 to my site dot WSGI WS my site dot WSGI there we go so clicking enter it says no module named my site dot W so it looks like I typed something wrong my site ID I did time wrong WSGI there we go so now if I go to our server again it should still be running which it is so I can go to the admin it's kind of slow but it should still be running I don't know why it's slow when you test it with G unicorn like this I'm not really sure but the the whole point of this is just to see if you're actually able to see your server if it is that means that basically you the correct things installed and we can move forward to the next step and it took a while like I said but there we go we see our admin so we know that we're good to go and we're ready to move on to the next step so I'm pressing ctrl C to stop that and let's move on to what's next alright so the next step is we create a system socket and all the service files for G unicorn and nginx to run the server without us having to actually run the server like we just did so we we are going to be using a virtual environment anymore we aren't going to be running the server manually we're going to be relying on nginx and GE owner corn to host our server so I'm gonna I followed this this tutorial in the digitalocean documentation if you're curious so you can search this of course we're not using a Postgres database we're using SQLite but other than that it's pretty much the same so the step that we're on is down here right here so creating system D socket and service files for D unicorn so we've tested unicorn like we just did to interact with our Django application but now we need to implement a more robust way of starting and stopping the application server so to do that to do that we use G unicorn and nginx so if you're curious you can follow their guide here but I'm going to be showing you everything you need to know alright so first thing is we want to deactivate our virtual environment and I want to install everything that we just installed in the virtual environment onto the actual server so I'm installing G unicorn so pip3 installed G unicorn pip3 installed Django equals two point two point two I could have actually just installed requirements but that's fine I'll do that now so pip3 install our requirements txt so I'm just installing everything that I need to and now once that's done now we're going to set up G unicorn we're going to build some configuration files that we need for G unicorn to run our server to host our web server so I need to navigate so make sure you're logged in with the root user because we're going to be creating some files so I'm navigating to the etc' folder going into system D so wherever system system D is right here going into system and here's where we need to add our G unicorn configuration files the first one is called so I'm creating a new file named G unicorn and dot socket this one is the easy one this is the one you probably won't have any problems with it's the next one we'll see if you're gonna have any problems it's going to be with that one so this one is called G unicorn dot socket so if you want to you can copy this out on video or inside of the dock the dock that I mentioned there's basically a template here that you can just copy so the first one right here is the G your nakorn socket file so you need to copy this and that's what's going to get pasted in here so it's literally just word for word I didn't change a single thing you can change the description if you want like you could do GU in a chord socket for Django application or whatever doesn't matter but the point is that you have this kind of unit heading the socket heading this install heading it should look exactly like this it should look identical so I press control C and control s and I save that next is the service the G unicorn service file if your going to have any issues it's gonna be with this one I had I had a lot of issues trying to set this up but if so if you just follow exactly what I've been doing and you named your directories exactly the way that I did then everything should work fine here so it's called G whoops I'm gonna spell Tyrone G unicorn dot service and creating that file now I'm going to right click whoops right-click on that file go to open with the default text editor and like I said there's a template in here that you can follow so referencing that doc or you can go to ice actually I won't have it on the source code but yeah go to the docs I recommend and you can just scroll down to the lowest point you want to copy this whole thing so there's a unit heading there's a service heading and there's an install heading and we're gonna need to change some of these things so copy it and paste it in whoops but but we do need to change some things so let's take a look here so nothing in unit needs to be changed this stuff is identical you don't need to change that but the user needs to be changed to whatever the user that you created on the server is named so like remember we created this Mitch user and Mitch has a directory inside of home so home and Mitch whatever you named this user that's what needs to go in the user parameter right here the group stays the same and the working directory you need to change so Mitch is the user this is gonna be Django project directory remember if you need hopefully you named everything exactly how I did because then you won't have to change any of this this is Mitch this is Django project directory this is Django project env so home Mitch Django project directory Django project environment bin G unicorn there's G unicorn installed in the virtual environment there it's also installed on the server but that's fine we can reference the one that was in the virtual environment all this stuff is the same this is the same and this needs to be changed to my site so my site WSGI : application and that should be it so if you named everything the same as I did other than maybe the user it should look exactly like this okay so now you have these two unicorn service and socket files setup we can go to our server you can either go to the Mitch user or the root user whichever one you prefer and we are going to basically enable everything so first thing is we need to start the socket and then you want to start the service so sir you want to do sudo systemctl start G unicorn dot socket looks like I typed it wrong it should be system systemctl start G unicorn socket so that's going to start the socket next is sudo systemctl enable G unicorn dot socket so that's good and now we want to confirm that the socket is working correctly so what you can do is check for a file that will have been generated so we want to do file run G unicorn dot sock and if you get an output here that means that the file was generated you can also check just by looking at the FTP so if you go into run and then you should see that you want to go into run and shoot CG unicorn dot sock that file won't be there if you didn't set it up correctly so if you have that file you know that it should be working correctly now that you have the socket enabled and started you can check the status of the socket so sudo system CTL status G unicorn dot socket and this is what you should see don't worry if this says failed that's this is irrelevant don't worry about that the thing you want to see is that it's active it's listening it's enabled and this is enabled did you see what I see here that means that your socket is active it's listening it's ready to go it's ready for a service to be bound to it which is gonna be the next step just as kind of an FYI if if for some reason your G unicorn dot sock file did not get generated you can try doing sudo shut down slot - are now and that will that will shut down your server and it will reset it just keep in mind that when you go to log back in it might take like maybe 20 seconds for your server to restart so don't panic if you can't get on it right away but if you don't have your file you definitely need that file to move forward so try resetting your server next is the the service file so remember I was talking about that G Unicorn service file and if you're going to have problems it's going to be with the service file so now is the time to work with that so this G unicorn service so you have to be very careful with what you type here because if you make a mistake you have to reset your server so it's a very kind of finicky process I had a lot of trouble with this so follow very carefully so so first of all we want to activate the service or I'm not sure what the exact kind of terminology is but it's something along the lines if you want to bind the service to the socket to get everything up and running I guess I'm not really sure of the exact terminology but this is what you want to do so you want to do curl - - UNIX - socket and this is all outlined in the documentation by the way so if you're confused checking the jewnicorn socket file testing socket activation that's going to be this that's exactly what I'm doing right here so if you get confused you can check out the documentation and see what they're saying about it so suit curl UNIX socket I want to do slash run slash G unicorn sock so I'm referencing that G unicorn dot sock and I want to write localhost so make sure so this this is kind of the moment of truth if you're going to have a problem it's going to be right here so if I click enter let's see what happens if you don't see the HTML for your website printing out then you know you have a problem if you see anything except for the HTML what I suggest is going back and re-watching the socket set up stuff yeah going back and what rewatching the socket set up stuff and then also doing sudo shutdown - are now to reset your server and kind of reset things because check check your Yorgi unicorn socket filed check your G unicorn service file because if there's a if there's a problem it's gonna be a parent right here if there's no HTML that means you either set this file up wrong or you set this file up wrong or something is named incorrectly something is wrong with the way you set it up so you're gonna have to go back and figure out what it is and unfortunately there's no kind of easy way to be like ok if it's this then go fix this if it's this then go fix this you kind of got to check over everything because any error that you see is going to be very vague and it's gonna be very hard for you to figure out exactly what the problem is so all you can do is just go back and re-watch also if you change if you change this G unicorn service file if you and you can either shut down your server or you can try doing sudo systemctl daemon reload and that will reach that will kind of reset things and then after that you also want to write sudo systemctl restart to Unicorn socket and G unicorn dot service so try if you make any changes to the Geo Taccone service file or the G unicorn socket file you need to write those two commands and I just recommend restarting your server I think that would be best so that means that means doing sudo shutdown are now it'll take about 20 seconds for your server to restart and you can just reconnect and that'll kind of start you with a fresh slate so I would just restart your server I think that's that's easier so now the last step is we want to make sure that the service is running so if you see the HTML you know everything's good next you want to write sudo systemctl status to unicorn so you're checking the status of the G unicorn service if this is what you see and you see that it's active and running that's awesome you should be very happy because if there's a problem it would have already happened so you know that everything is working correctly so I'm pressing ctrl C because everything on my end seems to be working correctly like I said if you're getting any issues you're going to need to go back check your G Unicorn service file check your socket file make sure that all of these directories are correct make sure yeah basically just go you're gonna have to go and rewatch and make sure like I said name the directories exactly the way I did that made that way you won't have any issues so if you made it this far you're good and you're ready to move on to the next step which is going to be configuring nginx to proxy pass G unicorn which is the next step in the docs if you were to refer here so configure nginx to proxy pass to unicorn so just like G unicorn I don't really know a lot of what nginx I'm not like a UNIX admin guy I'm not super experienced with web development so if you want to know more about nginx I suggest just googling it that's gonna be it's gonna give you way more information than I could so I just it's just just know if you don't care just know that it's a web server and it's commonly associated or it's commonly used in conjunction with G unicorn to host websites and yeah like I said Google it if you want to know more so currently we don't have nginx installed so we that's the first step we need to install nginx so on write sudo apt install nginx and that will install nginx on my server I click why for yes make sure that you're you're on the root user session because again we're going to be editing some files so I'm going to etc' and instead of system D I want to go to nginx there should be an engine X folder now that I've installed nginx I want to go into here and I want to go into its sites available yeah sites available no sites yeah sites available and we need to create a file here this is gonna be an engine X configuration file so I'm Rick I'm clicking on here I'm gonna give this new file a name it's gonna be called Django project again I highly recommend naming things exactly the way that I'm naming them so you don't have any issues I'm editing this with the default text editor and I need to add some add some code in here so this code can be found in the docs just like when we made the other G unicorn settings files so if you're following along here you can just copy you want to copy this whole thing so it et Cie and genetics sites available my project in our case that's the Django project so I'm highlighting that copying it going back pasting that in and I need to change a few things here the first one is the server IP or the domain so later we're going to be adding a domain but right now we just have an IP address so I'm copying the IP address from digitalocean I'm going to paste that in there and the rest of this needs this needs to be changed to home Mitch this is going to be Django project directory and the rest of this is fine so all you need to change is the IP address and changing the root directory to home Mich Django project directory and we are going to need to come back and make some more changes to this file later on when we when we purchase a domain and also when we host our static files using digitalocean spaces and Amazon Web Services but for now this is what our configuration file is going to look like so ctrl s to save it or clicking the little Save button there and now I need to link this file to I basically need to link this file to the site's enabled folder so I'm going to write up command down here in the server so I want to write sudo ln- s et Cie nginx sites sites available Django project which is the name of the file that we just created and then I want to reference the sites available folder so etc' nginx and gen-x I think I spelled that right and then sites enabled so just double checking this to make sure I typed it sudo ln- s e-g C nginx sites available Django project and I'm linking that to sites enabled so sites available linked to sites enabled so what that did is it generated the file inside of sites enabled you can double check and you can see that the file is in there now now you can check the nginx setup by writing pseudo engine x- t and if it says that the test is successful you know that you set it up correctly so if there are no errors you can restart nginx if there is some errors then you need to go and figure out what those those are you need to go back and re-watch the steps that I just took so if there's no errors you want to do sudo systemctl restart nginx that will restart nginx and now I need to change the firewall port to allow nginx instead of port 8000 which is what we were doing for testing so sudo ufw I want to delete allow 8,000 so it's deleting the port 8000 from the firewall now I want to do sudo you fw allow nginx full so I think the spelling here matters so make sure you capitalize the N capitalize the F I think it does I'm not sure but to be safe I would just have it exactly how I have it click enter and then that will allow traffic through your file firewall through nginx so at this point we should be able to visit our website using the IP address if you've been doing everything correctly so I'm copying the IP address I'm opening a new tab I'm going to paste that in and there is our server up and running now so at this point you have your server hosted by your IP address you should have all everything working the way you had in our development environment but there's still a long a lot of things to do there's still a long way to go number one is we still need a service to host the static files so that means the blog images for our blog blogging application or our blogging website those files can't be hosted on your digital ocean server they need to be hosted somewhere else we're going to be using digital ocean spaces for that and which in conjunction with Amazon Web Services so we still need to do that number two is we still need to purchase a domain name and we need to associate that with our IP address so just like coding with coding with Mitch comm that isn't that's a domain name that domain name is pointed to the IP address right now all we have is an IP address so we need to set that up still and number three is we need to set up HTTPS so right now when I visit my IP address there's no like security technically right now all the information including passwords are being sent over the network in plaintext so that's obviously a very very bad way to do things if you use HTTPS just like coding with Mitch comm is everything is encrypted so there's no nothing is being passed through a text file which is obviously very insecure so so those are the three things we're going to do set up static files using AWS and digitalocean spaces purchase a domain name and associate it with the IP address and then number three is set up HTTPS you
Info
Channel: CodingWithMitch
Views: 26,607
Rating: undefined out of 5
Keywords: django, digital ocean, django digital ocean, digital ocean django, hosting django with digital ocean, digital ocean hosting, https django, digital ocean spaces, django aws, amazon web services django, digital ocean spaces django, aws setup django
Id: 0PJjquyGTOQ
Channel Id: undefined
Length: 65min 30sec (3930 seconds)
Published: Thu Jul 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.