Deploying Django to a server - Django Web Development with Python p.12
Key Takeaways
The video tutorial demonstrates deploying a Django application to a Linode server, covering steps from setting up a new Linode account and server to configuring Apache and handling static files in production. It utilizes various tools such as Linode, Django, Python, Apache, and SSH.
Full Transcript
what's going on everybody welcome to another Django tutorial video in this video we're gonna be working on is the actual deployment of a django application to the cloud so the first thing that we're gonna need is an actual host for our servers I'm going to be using Linode Linode is also a sponsor of the series I've been with Linode for almost a decade now and blue notice also who hosts Python programming dotnet so if you want to check out Linode you can go to lynda.com slash synth X for a $20 starting referral credit sort of thing so if you want to support me and check out Linode and follow along on Linum you can do that at lynda.com slash syntax you can use other hosts if you want there's tons of other hosters you know google AWS digitalocean tons of hosts there's also things like Heroku and Python anywhere the issue I have with them is it's really easy to get started but then and it can also be cheap or free to get started but then as you grow it suddenly becomes far more expensive to stay on those platforms so I would suggest you don't start on them except if you just simply cannot afford hosting but for 20 dollars you'll last quite a while it's gonna cost you about $5 a month on linens so anyway at some point you you're gonna need to learn how to use VPS might as well do it now so we're gonna use Linode also a cool thing with Linode is we can do stack scripts so I've actually shared a stack script which is kind of like a shell script if you don't if that is it's kind of like a script that is going to do all the things we're about to do and deploy like a really Sam a basic sample django application so you don't have to go through all the steps every single time it's good idea to learn how to do it at least once but then from then it's like super tedious you shouldn't do it every time so super cool that we can just use the stack scripts and I've shared it on the community so you can launch your own leonards with that stack script so pretty cool anyway what you're gonna do create an account once you create an account go ahead and there should be like a blue create you'll choose create Linode that's your no Linode is basically a VPS once you're there we will choose for now we're going to do everything our way by you know the hard way by typing it in so we're gonna choose older images and I want a boon to 1804 LTS for long-term support next pict region where do you want the server to be I always choose Dallas Texas nice center location in the United States then I'm gonna pick nano use the one gigabyte here $5.00 a month pick a label I'm gonna call this Jango video tutorial make a password and then this is a quick American nuke it at the end but seriously consider doing backups later you can write a program to do backups for you but until then enable backups I have done it every time and I have never regretted it I have needed backups on many occasions and it's always so nice to have them so until you write your own just just enable them ok I like create here and while that runs I am going to also create one from a stack script just to show you guys how convenient they are so I'll choose create from stack scripts now this is my script so it's here in my stack scripts but if you're watching this thing you want to do it go to community stack script search for syntax Gengo and then you click on this one mm-hmm now we'll come down here I'm not gonna fill this in we're gonna use the temporary Linode URL or yeah domain basically it gives you keep this if you want to change the project name have at it just you're gonna have to change other things as well well actually for this X script you actually won't have to do that but anyways if you want to change it go for it production or test this is basically for debug equals true select your image we'll go with the same one 18:04 select the region Dallas Texas we'll go with nano de ghin label I'm just going to call it stack scripts example root password there we go again same thing on backups um I want to think about doing it but for now hit create okay once you've created go to your Leonard's and then I'm gonna go back to the Django video tutorial one you don't have to spin up a stack script like I did I just did it to show you guys so for now I'm back in the one that is not a stack script this is something we're gonna write all ourselves so now we want to connect to the server there's a million ways you can do this on Mac and Linux you can literally open a terminal do ssh route at your IP address so if I go to networking I can see right here this is the one for me on Windows you could use PowerShell in SSH via PowerShell as long as Windows and PowerShell is updated also we could just click Launch console and connect this way I'm not gonna do that because I want to make the text really big and if I do that this gets all glitchy on me so I'm not gonna do that I mean I'm super tempted to but I've been bit too many times with that console if i zoom it in so I'm not going to do it instead I'm gonna use putty PU TTY putty is also nice because you can save profiles so you can save like a domain name and like a user name that way you don't have to keep track of them if you're someone like me I've got like I don't like 50 plus maybe even close to a hundred servers so it's super convenient to be able to save their profiles anyway I've already got putty downloaded so I'm just gonna type putty and then to connect I just need the IP here copy that paste that in there and then you'll get this message the first time you connect to any server it's just a security sort of thing if this is the first time you're connecting to the server we expected it if this is a server you connect to all the time like you're connecting by a domain name for example at a later date someone might point that domain name somewhere else that you don't intend and so this is just a quick warning to you that that is happening so this is a new server though so we're confident we're comfy we'll hit yes if you ever see that message and it's not a new server something is wrong but anyway we'll log in now with the credentials that we set boom we're in first thing I'm gonna do is make this a little bigger so everybody can see appearance will go with maybe even bigger font I don't know 20 to apply beautiful okay first thing we're gonna do pretty much every time you ever spin up a Linux anything have to get update apt is advanced package I don't know what T stands for but it's something like that it's your package package manager kind of like PIP with Python this is how you install packages so the first thing you're always going to do is an apt get update the next thing we're gonna do is apt-get install - why so we don't have to say yes this is set like later like if you don't do why it'll be like hey it's going to take up such and such amount of space do you want to do this you could say no I don't know why you would so we're just going to use the yes flag and we're gonna install Python 3 pip we already have python 3 on the system but we don't have PIP so that's why we've got to do this also a quick note we probably have Python 3.6 not 3.7 just to save time I'm not going to update 23.7 if you point if you want to do that update 237 on your own time I'm sure you could google it for doing that but we're gonna skip that it's going to take a bunch of time so once we have pip let's make sure pip is fully updated so python 3m pip install - - upgrade pip cool I think from here on - let me just say you can go to the text-based version of this tutorial scroll on down to where we start having these commands boom you can just copy and paste these in again also in the future you can just run the Stax script super quick and easy so just do that now so once we've done that the next thing we want to install is Django and that Django app that we use so I'm just going to take this copy right click to paste usually depends on what you're doing so a lot of times right click work sometimes you've got to right-click and paste and so on anyway so okay we've installed Django and that app that we're using so now we're gonna M cater which is just make tur em cater any anytime you lead with a slash so it's like that's the root directory the very first the base directory make Durbar www now we're gonna see d VAR w w okay now what we want to do is basically we're gonna make that we're gonna start that project so Django admin start project and we're gonna call that my site okay so once we've done that the next thing that we want to do is we'll CD actually let's just nano my site and then basically we want to Nano we're get in the setting so should be my site yeah my site my site settings you can also use tab to autocomplete so you start typing some hit tab and it autocompletes so the first thing we want to do is debug shall be false next what we want to do is come over to networking and we're looking for this reverse DNS this is for our temporary domain name so I'm gonna copy that come over to allow hosts paste that in cool again to keep this short I'm not gonna talk about domain names beyond here's how you do it make a domain name you know register it with a registrar on that registrar point that domain name to the hosts name servers so in this case would be Linode name servers then on Linode you go to like networking or dns or somewhere on there then you'll point the domain name to a specific Linode and then that Linode will point based on the domain name that you use cuz like you can have like multiple web sites hosted off of the same server in the cloud okay but for the sake of brevity this is the way we're gonna do it so allow it host that's fine later we're gonna have to add it a couple more things but for now control X yes hit enter cool alright so also uh Nano is an editor so it's just a basic editor lots of people like vim as well but I prefer Nano so you Nano and then the thing that you want to edit okay so the next thing that we're gonna do is start a new app so we're going to Python 3 my site slash managed up PI start app and then we're gonna call this it works cool now we're gonna do is I think I'm gonna start copying and pasting again so let me come over here I don't want to have to like write all this stuff out this is just for like a really quick example anyway so let me scroll down to where we are it works ok so first let's CD into mice site and then we're going to nano my site URLs depay because we have to point that to our new app so I'm going to take this copy if you hold ctrl + K and Nano it deletes lines at a time so delete it all the lines paste in the code control X yes enter done now we're going to come over to the URLs for the app and again that's blank so we're just gonna copy and paste that in X yes or ctrl X yes you're writing what happened hold on my site LS Oh it almost looks like we did we didn't get I wonder if I can't do this sort of pathing I think this is what screwed us up let me go back one yeah ooh that's gross so let me RM - our remove recursively it works hopefully I didn't screw us up any further anyway yes CD my site LS for listed out managed at PI let's create that one more time Python 3 managed up hi start app it underscore works okay anyway let's try that one more time nano it works URLs now PI and then copy paste that stuff in yes by the way for sure by now our stack script is done just take note anyway now let's do the views so Nana okay so take this paste those in control X yes save okay cool all right that's all the Django stuff okay so now you basically we've got our server in the cloud we've got Django set up now we need to set up our web server and then for a web server to talk to Django we need an intermediary and that's where WSGI comes in WSG is stands for web server gateway interface and it just kind of communicates between your your web server software and whatever framework we are using so to get that that's these commands here so Afghan install again yes Apache 2 which is our web server and then WSGI yes someone will ask why not ngx or nginx fix why not nginx because we had to pick one okay so we picked one Apache done question answered okay so then what we're going to do is fill in the virtual host information so again I'm just be doing some copy and paste in here my side comp and then yeah I'm just gonna copy and paste that virtual host stuff and this is a good example of basically at this point your web server can actually serve many websites so our server here runs a web server and then that web server comes to this virtual host file and then looks at the hostname and if it matches the hostname does things based on that match so so first we have to fill in this hostname so again that hostname if we go here networking it's this this is our hostname so it basically would be your domain name if you pointed a domain name here but for now we're just gonna use that so delete hostname paste that in also for some reason my copy and paste puts this on a new line make sure that doesn't happen yeah that's gonna hit a nasty error so I'm actually going to delete here so that's up in line with this variable here okay so hopefully it doesn't screw us up anyway control X yes enter okay so that's your my site kampf and I we are probably done we might have to do one more thing let me check let's first do a 2 n site my site and then it just says hey to activate the new configuration you can run this I believe you can also just run the service Apache to you reload but we'll do this ok now let's go and visit this URL hopefully it works ok for now we're getting the default Apache page I'm trying to think of all the reasons why that would be but one is probably our default settings are not quite what we were hoping so let me go let's nano and in fact we'll run these two commands first we want to delete that default config so we're gonna just RM for remove and we're going to remove this is 0 0 0 - default conf because we want that to agree so then we're gonna Nino that default configuration file and then we're gonna copy and paste in some really basic information here but basically this way this doesn't have any preference on hostname save yes now let's reload Apache 2 again ok great so now it works so before we at probably had a whole bunch of other setting information in there that we didn't need for the default settings because we already set those but somewhere in there it was probably specifying a hostname that we didn't care for so everything works now and we've served a basic Django app now think about all the things that we just did to get to this point ok we put in a decent amount of work now let's look at our snack script website so head over to your Linode or if you did it anyway let me pull up my stack script okay so here stack scripts stacks you know what I'm trying to say ok so this is our domain so to speak copy that come over here paste that one ok and that one's already done so that one really it just takes as long as it takes to like do like the apt-get stuff and the updates and stuff like that it you know it's like 2 to 3 minutes or something so you can just spin that up go get a drink of water and you're probably all set so speaking of drinking so ok so up to that point that's a really basic django app but probably what you really want to do is deploy the project that you've been working on all this time so how do we do that well first we have to get the project files on our server there's a bunch of ways one you could have started working on the server but two you probably worked locally and then you want to put it up on the server there's a million ways to do this probably the biggest one would be like SCP if you're on Mac or Linux you just have an SCP command in your terminal on Windows you also have SCP in Rochelle and you just SCP from a location to a location and there's lots of tutorials on that also like on Windows there's win SCP which is like a GUI version of SCP also you can do SFTP to like move files to a server securely using something like FileZilla also pretty popular anyway there's a tons of wait there's so many ways to copy those files to the server what I'm gonna do is since I've already hosted the website we're just gonna pull it from because I've hosted it okay so if we go to the back to the text measure of the tutorial blah blah blah blah blah here yeah so we're just gonna W get that part eleven Django so I'm just going to copy that so now what we're gonna do CD back a directory so dot dot is backwards and now we're gonna RM - our my site because we don't actually want that that's a stupid little app so so we got rid of all that now we're gonna do is W get which grabs like content offline and we're gonna grab that Django part 11 zip file boom done now we're gonna apt-get install unzip we could also use I think like tar or something like that but tar is like really just a pain in the butt to use unzip is really simple unzip Django 11 like anytime I want to use tar I have to like google the commands and it's annoying anyway so we've unzipped it now we could do LS again there it is my site is there okay now we have to do all the things we had to do before basically so first we need to edit the settings so Nano my sites my site settings dot pi come down into here and we're gonna set debug so that's a false then what we want to do is grab this again set that as our host okay great the other thing I want to point out before I forget Django deployments go to the Asheville jingo docks cool and click on deployment checklist definitely go through this this has a lot more things so again I can't cover all the security things like for example we're using root user probably in practice you would make your own pseudo or users and then probably disable the root user account also you're probably not gonna log in with passwords you're gonna use an SSH key there are so many like little things when it comes to like using web servers again that I can't cover all in one video it would have to be like an entire tutorial series ok so anyway hmm here's another thing you should just check out go to the deployment checklist one of the things probably the first things for a secret key probably save your secret key in some sort of file somewhere outside of your Django project so in this case they're hiding it in in you know basically the leading /root door et Cie and then the secret key put it somewhere outside of the project files that way for example if you have a permission issue or later like if you decide to share this with people on github you don't share your secret key by mistake on github you can literally search for strings and stuff and you can write a program that would just go through everything looking for people's secret keys you probably want it you don't want that to be you so anyway go through the checklist there's lots of stuff here anyway back to what we were doing ok so we sent aloud hosts great I think that should be like everything we have to do at this point before we could test it we're is gonna be more things that we have to do for sure but anyways now let's service Apache to reload and let us already forget which one was it 9:14 is the one we want to check so I'm gonna close the other one so I don't screw up refresh ok those are tutorials aren't they beautiful so what's going on ok the issue is our static is not loading so in production we have to handle static a little differently so I'm gonna just come back to our tutorial here and we need to edit that settings not pi again we're gonna scroll down to the very bottom just hold that down arrow okay underneath static URL we want to add these three new things cool now from here what we want to do is like our static files are in many locations if you remember each app has its own static files what we need in production is them actually to all be in one location so Jango has a little script for us that's already doing this so control X Y s to save enter what we want to run is let's CD into my site yeah Python 3 managed PI and it should be collect static great so all our files have been moved to the same location on our actual web server bar ww-why site static now if we come back to our website refresh boom everything's working again so now we could do things like click on data analysis hopefully and then click on like panda is okay everything is working it looks like our style styles are here blah blah blah blah alright now there's a couple of things we need to talk about yeah that the two things we need to talk about are one as a few people up the deployment tutorial is well it's not yet over sorry we got to talk about ownership so actually let's do the ownership thing first okay so so if we attempt to login oh login doesn't course crush okay we're not going to do that first we're gonna do the URLs first so what happened to our URLs darn it okay so if wait yeah so if we nano main URLs dot I our issue is these darn slashes so at least in flats like if you hat like in a path if you have a slash in a path it will correspond to things that have slashes or don't have so you someone if you have that path and register and someone does register but not a slash it will still direct them to that that location if you don't have the slash but someone adds the slash it's gonna fail so I always like to add to the trailing slash but in this case it's causing trouble so what I'm just gonna do for now short quick solution is delete all the trailing slashes ctrl X yes great now refresh this page maybe we have to redo - maybe I have to reload Apache let's try again ok now it works so now we could actually like login and register and all that so let's try let's try sent X what happens okay we hit a server error 500 so this is super useless to us at the moment later you'd probably want to have some sort of error handling that again can either email you or save to a file like you're using logging and stuff like that but for now we just get this ugly error luckily I already know what the problem is the problem is we can't act we can only we can read from the database but we don't actually even have permissions fully to do that because we're not able to actually confirm this thing anyway what we need to do is give the user that's attempting to read that database permission to do so so back over here we can see the two commands basically what we want to do is CD back a directory and then we want to do channel owned or CH owned for is it I think it's w w data let me make sure yeah so that's the username for your Apache user basically so we want to give that user access I want to make sure I get this right but it should just be yeah the my site directory and then we want to give them access to the SQLite database so my site and then we'll give them WWD data my site slash I think yeah DB sqlite3 cool and then let me just reload Apache cool now we'll just go back to login let's try to login one more time cool we've logged in awesome ok so the last thing what was the other thing oh we actually had to do it in order we had to do the URLs in the ownership so actually I think we're done I think we're done I'm trying to think if I've missed anything I don't think so so that was deployment in Django ok so again if you guys want to use Linode to kind of support the channel but also get $20 in credit that's just lynda.com slash sin tax if you guys got questions comments concerns suggestions whatever feel free to leave them below if I forgot something let me know also if you've got requests for future coverage let me know that as well I've seen a lot of requests for the Django rest API stuff the only thing it just doesn't quite fit this example but maybe like a delayed later project like it looks like the main use case there would be like single page app unless you're like making some other sort of restful api type thing but anyway so I might cover that but it doesn't really fit this example so unless I'm totally done with the Python program yet stuff let me know what you guys would like to see in the future otherwise that's it for now thanks for the support the subscriptions the channel membership the donations all that stuff and I will see you guys in another video
Original Description
In this tutorial, we're going to focus on the deployment of a Django website.
Get started with $20 credit: https://linode.com/sentdex
Text-based tutorial and sample code: https://pythonprogramming.net/deploying-to-server-django-tutorial/
Channel membership: https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ/join
Discord: https://discord.gg/sentdex
Support the content: https://pythonprogramming.net/support-donate/
Twitter: https://twitter.com/sentdex
Facebook: https://www.facebook.com/pythonprogramming.net/
Twitch: https://www.twitch.tv/sentdex
G+: https://plus.google.com/+sentdex
#django #web-development #python #deployment
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI