Deploy Django + Channels + Redis + Heroku + Daphne

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to another tutorial so this is a simple deploy of django channels redis heroku and daphne so in the previous tutorial we built a simple chat app so we're going to utilize that to deploy to heroku so in this tutorial we're going to start off with a basic deploy assuming that you've not deployed to heroku before so you can skip that if you want and just head over to where we finished in the first tutorial but that's just going to take you through how to do a simple deploy then we're going to add the configuration for daphne channels and redis to our application and then deploy so remember that we're using http only so it won't work by default we need to change the url to http once we get our application started and this tutorial is just for reference so please don't deploy your application like this in a production server we're going to build upon this use other technologies to then further improve and just showcase some of the different technologies that we can use to deploy an application like this so the first task if you haven't already done so is go to the heroku website sign up i think you'll need some sort of bank card to do that once you've signed in it is a free service so you'll be able to utilize the free resource to actually deploy your application so you can see here that once you've signed in you'll be sent to the dashboard this is what you're looking at here these are all different apps i've created so first of all we'll go ahead and build a simple application and then we'll come back and we'll make a new project here so let's go into the code and start a new django project and then we'll deploy that basic project so that we know and understand how to deploy an application to a roku first and then we move on to try and deploy our channels application our chat app so like i just said i'm just going to create a new application i'm not going to take you through it by all means have a look at some other tutorials and creating a django application so i'm just going to build a django application and then we'll go through the initial steps that we need to take to prepare it to be utilized in heroku and then we go off to heroku create a a new project and then we deploy our default project to heroku so we want to just check to make sure and feel comfortable that we can deploy a basic project and then we can work from there work forward from there we then create our application i won't go through all the steps uh please check the previous tutorial to do that and then we go through the steps that you need to take to actually deploy your channels application so your chat application so we're going to deploy redis and a different type of server so that we can actually then utilize our chat application in heroku so i have my project created now so everything is running okay so that's obviously the first check now let's go over to heroku and click on the new and let's create a new app so i'm just going to call this app something crazy there we go oh no i'm not going to use that sorry something like that okay so this is our new app and i'm going to choose the create apologies that offends you i know some people like to use real names um let's do something random you can choose whatever you like of course so let's go to create app and choose your region obviously it doesn't really matter at this point so we create a new app which is great and now we've got some few options here so first of all we're going to head over to resources i'm just going to type in redis so we're going to need a the heroku redis so select heroku redis and then just select provision so i'm using the free hobby dev here so go ahead and do that so that will just take a couple of seconds we're not going to use it just utilize it just yet anyway so now you can go over to deploy so you can see here the deployment method heroku git and the github and obviously a container registry so we're going to be utilizing github here but you can just utilize download this your cli etc and deploy from the cli so the reason why i'm not doing that and i'm going to show you the graphical user interface for github a number of different reasons to help support beginners who are not familiar with the cli git cli and also i'm using a virtual machine here and i've never have ever since set up exactly how i want and it just takes a bit of time and i just want to show you how this works and i think it's always better to show beginners a gui interface of some sort because it's easier to follow the steps when you're using commands you can skip or miss commands you can forget about different settings that you might already have pre pre-done so it causes more problems and it's worth and potentially then you're asking me more questions or ultimately you don't get this project deployed now this is a tricky um situation tricky situation i mean this deployment is fairly tricky and what i want to make sure is that you get this done right because there aren't many guides online explicit guides that tell you step by step how to actually do this so i want to ensure that 100 everyone who follows this guide will be successful okay so next up then so assuming you've got a github account if you don't go ahead to github make a new account and so on and then we're going to go over to downloading the github desktop tool again i'm in windows like i said you can kind of skip this go to the next section if you're familiar with utilizing git and so on and just deploy it however you like so here i'm going to go to file a new repository you can just about see that so i'm going to create a new repository i'm going to call this chatty chat and then i'm just going to go to my local path where i actually have this project installed so here i'm going to go to the c drive i can't remember what it called it django i think heroku and it is just here yep there we go so select that folder create repository right there i'm going to initialize with a readme i'm going to ignore if it gives me the option it doesn't um so we'll do again ignore in a minute for the virtual machine because we don't want the virtual machine to be included so we'll add that in a second okay and now press create repository of course at this point i'm wishing i did it completely different but you can see now i have a chatty chat folder this is my repository i'm just going to drop my project into that apology so i'm going to leave the virtual machine there going to grab the core so i'm just going to put my django project in there i would need to close this up and i'm just going to put the everything inside of that again i apologize um so we're inside of our repository now so let's just go ahead and open our folder up to that project it was called heroku um and chatty chat so we'll select that folder there we go um so now we're inside so let's just um start the project up again okay so the repository started so just to confirm i've got the virtual machine on the outside here of course you could just get ignore that if you want to if you're not familiar with that i've created this new repository and i put everything inside of here my project you can see the files right there so that's all now in place and i've started the server and everything is running so that's our current project setup that's our baseline so now let's go ahead and think about deploying to heroku so obviously at this point what you can do the process is obviously we're going to make a a new commit and then we're just going to publish this repository um so we published that so obviously at this point once it's um published we can now connect that to our heroku account so go inside now i've already set this up with my github account so it's going to just appear you'll probably need to go through a few steps here so i just select github and it's obviously select the very academy github it's called chatty chat so we search for that once you're connected you can see now i can connect to that application and we're good to go now once it's connected uh you can see it's now connected um what we have is an option down here called deploy branch this is obviously the manual manual way of doing this i can press deploy it will go ahead and try and deploy it now by default this isn't going to work there's a few steps that we need to take here to get this working and this is why we're going through this process because i just want to make sure you understand the basic deploy because there's no point doing the other deploying unless you know that a your project will deploy because there's a few settings that we need to change for heroku so let's just do that first okay so let's go back into the project and change what we need okay so first step in your core now remember this is this is just a guide to get things working it's not necessarily the only method of working or deploying it's not the most efficient way possibly of deploying um it's just a way of deploying this so first of all we need to set up our static and root files otherwise we'll get an error when we actually deploy notice it says the os is not defined so we just import the os at the top here okay so that's that done so we can see the settings right there okay so next up we are going to need a few other additional required files so the first thing is we're going to need a requirements so we need to pip freeze and then just go to requirements uh we should do this in a second but i'll just show you now so we've got the requirements text so we're going to need that so this is a file that heroku is going to read and actually deploy all these packages so when we actually deploy on a roku it sets up the server from scratch it utilizes all these requirements sets them all up installs them and then the package starts to run your application will run on these settings so it's important to have that otherwise it won't know what package it needs to run your application so next up we're going to need a prop file so to create a proc file we just need to select new we're just going to type in proc file so there's no file extension here so if you use a file extension it won't work so we're going to need that so in here we're just defining what some additional parameters so the proc file is basically we're going to pass some additional parameters to heroku and tell it what we want to set up so here we're going to just define the wsgi server in actual fact so if you wanted to read a little bit more about the server that we're going to use this gunny corn server then please do it's just right here but we're just going to go through this i'm not going to explain at this point maybe we'll make a different tutorial explaining these different applications so this is going to help process our http requests essentially um so we're going to need to utilize this you can see here is a pure python http server for wsg applications and that's important at the moment because remember once we start using um channels we're not actually just using wsgi remember so we're going to run this first and just get this started so you can see here we're going to need to pip install this so let's just do that first so we pip install that obviously we're going to need to update our requirements text we can do that straight away there we go so now in the requirements text we now have the kenny horn right there so now we can go back and read the instructions you can see here this is what we need to set in the proc file so go back to the profile obviously get started is not the name of your project my project is called core so there we go so the main project this is where settings resides is going to be core so that's all i need to do there and press save so next up i include a runtime text file i can't remember if it's a requirement but um i seem to do it every time so runtime.txt and in here you can see i've just defined what python version i want to run so i'm just instructing the server what python version i want to run obviously you might have specific python versions that you want you'll need to check heroku because they don't have all the versions i don't believe but this is one that supports i think it's the latest version as recording this video so that's what i'm going to utilize for this application so now i'm in a situation where i've configured my static root files that's going to be important otherwise it won't load properly i've now got my proc file in place and that's going to tell the server the fact i'm using gunny on to serve http protocols and to actually allow the service to run correctly and obviously i've got requirements files so it's going to load up heroku is going to load up all of these applications so that's all good so what we can do is we can just simulate a deploy or apologies where one problem we seem to seem to get quite a lot is the static file creation on heroku the amount of times have been asked this question so what we can do is before we deploy we can test whether this is going to work correctly so let's just do that next okay so if we run the manage.pi collect static so we want to practice or simulate collect static we're going to go for a dry run so it's not actually going to perform the actual application but it's going to simulate it so let's go ahead and do that and you can see here that we have a problem so the system cannot find the path specified so if this doesn't work here it's not going to work on the server so we need to go ahead and create a new file or folder sorry new file new folder and we're going to call that static now like i said this is just a simulation here and you can change this to however you need so static right so go ahead and try that okay so it's um seemed to work okay obviously it hasn't copied anything across um but you can see it would copy everything across um so you can also see that potentially we're going to need a static files folder so we can go ahead and build that we might as well go ahead and build that so let's create a new folder again static files why not and there we go so that's gonna looks like it's gonna be okay now you can see that um you're probably screaming possibly at the screen now this is incorrect like i said i'm just trying to get this working um so one thing that to point out here is that when we actually utilize github it does no nothing in the folder then it's not going to be actually applied to the repository so what you're going to need to do and the amount of times um i've supported and help people with this single problem is that you just kind of need a file inside of it to actually get it onto github and then it gets deployed so just go ahead and create a new file just call that text.text for example we'll just uh again put it into static as well again i'm just doing this trying to avoid any questions and any potential problems so there needs to be a file here then it will be updated and utilized and deployed sorry on github so now we've got that in place let's actually now go ahead and go back into or deploy this so into my get up here you can see the changes that we've made so we make this new commit i'll push it to the github account online so push to origin so now that's been updated so now let's just go ahead and let's see if this works so let's go to the uh let's go to our dashboard so that's where we were before in chatty chat um sorry in our in our new app in deploy so it's connected etc so i just pressed a deploy branch and just wait excellent so if you've done that it says deploy to heroku and it was successful there is one step that we need to do so let's just go ahead and open the app and have a look at the problem and you can see here exactly what the problem is i say that i can see exactly what the problem is so the invalid http host header so this problem's easy to fix because in actual fact if we go into the settings what we need to do is just define the fact that we are going to use um this host so we need to define the allowed host in this case heroku app.com now if you apply this and try it in your local server it's not going to work so you also need to now add your local server right there so just remember to do that if you want to work on the local as well so i've used dot heroku um app so this dot obviously anything before that is going to be allowed and this is obviously just the host.herograph.com so now i do that i can now just go back into here you can see the changes that we've made create a new commit push that to the origin go back into heroku once it's done and then we just deploy again and i'll see what on the other side okay so now it says deploy to heroku was successful so let's just open our app again refresh and there we go so we now have our application deployed to heroku and now we can go to the next step so obviously the next step now is to follow the previous tutorial in this series and get your application to the point where we finish the chat application and then i'll see you back here where we're then going to change or make the settings so change the settings to make our application then work with channels and redis and django okay so welcome back hopefully now you've got a working version of your application that you want to now take forward and deploy on heroku so please use the previous tutorial like i said to go through and just build your application and you should now get have or you should now be at a point where you've built your chat room and if i open up a new tab for example here we should now be able to just say something here and then should go back into your original so it's definitely working now the current setup here is that i'm using the in-memory channel layer so i haven't got redis installed or anything on that in this project at the moment so that's how i've tested this project so i've just followed the previous tutorial um of using a similar code to the previous tutorial it is a little bit more slimmed down so now we're ready to deploy to heroku so as i've already alluded to and we've already deployed redis we need to actually connect to redis so we need to change the channel layer first to utilize redis so let's go ahead and do that so you can see here we've got two options we can remove this but we're going to access redis on our server through redis url os dot get so that's just going to connect to our redis local environment that we set up on heroku so that's the first setting we can go ahead also if we want to and we can set up the cache as well if you want to do that so that's the setting for that again this is just the local host you can remove that if you want to it's probably worth pointing out that this is 2020 i think we're in the month of september it's likely to change these type of settings will change quite regularly potentially so um if you do have a problem and it's a couple years after this tutorial then just leave a message and i'll try and update for you you can always ask and if i've got time i'll update it for you so if you're interested reading the deploying documentation in channels you can go ahead and do that there are some a few pointers here that help you out understand some of the um theory behind what we're doing here because i'm not going to explain every point so one thing it does mention we need to install the channel redis so we can go ahead and do that in our project so open up a new terminal just quit that and we just install that didn't work we'll just go back and install that i'll just grab that again pip i should have just typed this out pip sorry pip install so we'll go ahead and do that and that looks okay so obviously we've done that so we need to update our requirements text so go ahead and do that so pip freeze again so that just updates our requirements text for the server so now we need to get to the difficult part or the interesting part however you want to look at it so here we have wsgi now remember to summarize in order for our django application to work we need to pass the messages from the browser the users make the http request we need to pass those messages over to django now to do that we utilize a ws server say so we installed that previously if you remember this is why it worked on heroku because we were using gunnyhorn to do that so that was processing that passing those messages over to django through wsgi so if you remember back to the tutorial the channel's chat room tutorial we're now using asgi to process our different protocols because we're not just using http now remember we're also using web sockets so to process web sockets we're going to need the asgi so a different setting now this is where it gets slightly complicated because this is where you need to know a little bit about heroku and how it processes information and a little bit about behind the scenes and how things will work so this is where it gets complicated for beginners and for those who have not worked behind the scenes sorry behind the scenes or on server side configuration so with heroku it seems as though you can't run wsgi and asgi separately because of some of the settings on the free package we're a bit limited to do that so what we need to do is just run one server which will process and the wsgi and asgi so it'll process http and our websockets so there are a few packages that will enable us to do that and although i'm showing you how to actually deploy this it doesn't mean this is the only way of deploying nor does it mean it's the most efficient and effective way of deploying there are plenty of different crevits and issues with deploying this like i'm going to show you but it does take you to the first step of actually deploying this type of application to heroku or indeed any service so again if you recall from the previous tutorial i mentioned the fact in the slides that we utilize daphne to actually process our asgi requests so what we're going to do is use daphne and install it onto heroku and we're now just going to remove gunnyhorn so we're going to use daphne to process both our http and our websocket so if we look at the channel's documentation it's pretty friendly here and it tells us the new setup that we need to configure so this is something that's easy to overlook so let's just run with this so if i go back over to my was gi uh you can see that the configuration is slightly different if i just paste this in you can see here that we're using for example the django core asti but now we're using channels routing and we imported from get sti application but now it's get default application so everything else is pretty much um similar apart from importing django as well so we need to do that so let's just get rid of that remember this is pretty much the only setting that we need to change so we'll change that to core everything else is pretty much the same so i'll just mess that up let me just go back so let's just uh remove that there and we then go ahead and add core which is now there okay so that's our configuration that we're going to be utilizing in asgi so now if you remember we need to go into the proc file because this is where we told heroku that we wanted to use gunnyhorn to serve http protocol or pass the data over to django so obviously we don't need that anymore because we're not going to use that and of course you could remove that from the requirements if you wanted to so while we're talking about requirements in actual fact if we go into the requirement requirements the requirements when we install channels in actual fact it placed uh daphne also or installed daphne also and that's why you're going to see in the requirements file so um we don't need to add that in there like i said you can remove gunner horn that isn't a problem so let's go ahead and go into the profile and now configure this so there's two settings that we can make um first of all um we define the fact we're going to use daphne and then we can set up our chat worker so python manage pi run worker setting chat settings v2 so these type of settings you need to research your different providers in order to get the right settings for those individual providers so if we go back to the web here we can see that channels does offer us a basic kind of start and then we need to go to the heroku website and then do some research and see then how we can then better support our application so you can see here that we can bind to ports and ip addresses and so on so that's essentially what's happening here you can ask me questions about this and i'll happily um reply um but essentially these are settings that work on heroku currently and for this type of deployment which i'm trying to keep as relaxed and easy as possible so you can feel comfortable getting your application um online is um what's going to work at this point of course saying that i've already given you the answer and showing you where you can read about these binds the v here is just a verbosity so it's just going to give us different information when we start to troubleshoot so that's pretty much it there we go um so let's now go ahead i think we've done that so i think we're now ready to deploy so let's go ahead and go into our github here we're just going to make a a new commit uh push to origin so we'll let that do that and then we go ahead to heroku and then we go ahead and deploy my apologies it was one of those moments where i said something and i realized there was another step i wanted to show you so if you deploy now in actual fact there could be some problems now the question is then well how do i know what the problem is how do i access that information so if you're using the heroku git or sorry the hiroku cli that's going to provide you information so go ahead and download that i can't remember where we need to download that so go ahead and down oh i think it's just here an actual fact go ahead and download that for windows and yeah download that for windows um and that's then going to give us access or we can then hook into heroku and get some feedback from the console so in windows you can see that once you've downloaded heroku and stored the cli you can now access it from your command line interface your terminal your prompt here i'm going to type in heroku and then login and then i go through the login process once you're logged in you then need to select the app that you want information from so here's an example so i'm going to select heroku logs select the um the app and then a t switch is for additional information if i remember or just some advanced um information so a little bit verbosity verbosity again i think so go ahead and do that so my app is not called that so i need to change that but that's going to then give you um oh sorry the t apologies i've just it's just come to me so the t refers to the fact that i want to continually get updates so the t switch here is for that so once you hook into your application you're going to see this and this is redis so you can see that redis is running it seems to be okay uh active connections one um so that's red is in the background that's running in your cli and that's going to prompt or um come up now and then so that's now already so let's go ahead and deploy and let's uh see then what happens here so we've uploaded if you remember so we're now ready there so i'm just going to deploy the branch it starts a new server and the daphne server bam or i did too it's a little bit quick so and you can now see it says module not found no module named chat dot asgi so that seems to be the problem here um let me just move up here and we can see that happen again so so you can see here um utilized our settings it used port 7004 and then it started you can see it process exited with status one state change starting to crash so it crashed the server state changed from crash to starting and you can see that it tried to start again so this is kind of interesting it's going to help you troubleshoot so we had a build succeeded started the process again and then you can see it then exited save changed from starting to crashed so it crashed again and then so on and so on and so it says here no module named chat.asgi okay so there's two problems here now if you remember that my core application is called core so first of all the settings is not chat it's core so that's the first thing i'm going to change secondly um the in the core we have asti and not chat so we're going to need to change that to core so that's the second thing that we need to do um and let's just give that a go so i'm just gonna now upload that again to commit that to github again and then i'm just going to run heroku again okay so this time you can see here that it's got through daphne it started state changed from starting to up and the build succeeded so it looks like um we've succeeded in doing that so i'll just drop that down a bit um looks like we've deployed so let's go ahead now and check let's open our app let's um obviously we want to go to chat and then let's start a chat room say chat and then a chat room so our chat room is working let's see what's happening here so you can see we've got a get request so you can see what's happening behind the scenes here similar to what you'll see in visual studio code if you're utilizing that now i wondered um if we can just capture the websocket upgrade um see if we can see that doesn't look like it here okay so let's go ahead now and send a message and you can see that it's not working so the reason why it's not working is because you're currently using https and we've not actually configured it to run over https so what you're going to need to do to get it working initially is just make sure that you're just using plain or http colon and then we can then go in press and we're off so you can see here in the logs um for example we're sending web sockets packets to the client and so on and so on so there we have it so notice that i'm using a different application because there was much playing around with that uh different settings and so on i just couldn't figure out why it wouldn't work and it's early in the morning and then just hit me well i can see that we're using atps so obviously this is the first step to installing this um obviously there's a lot of different configurations that we can now apply and other technologies that potentially we could utilize instead of daphne which does have some cravettes kravitz some issues um utilizing heroku so definitely lots more to do but hopefully you can now get your application running following this guide if you do have any questions or any additions then please add them in the comments
Info
Channel: Very Academy
Views: 9,419
Rating: undefined out of 5
Keywords: django, django tutorial, djangotutorial, django tut, django 3, djangoproject, django examples, learn django, django beginners, beginners django, django framework, django 2020, django example, django heroku, deploy heroku, django heroku channels, heroku django, django redis, django daphne
Id: zizzeE4Obc0
Channel Id: undefined
Length: 35min 38sec (2138 seconds)
Published: Wed Sep 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.