Publishing to a Web Server – Django Web Development with Python 11



what's going on everybody welcome to another Jenga web development tutorial with Python in this tutorial we're going to be talking about taking our website and pushing it live to production at this point we have a beautiful website with just really high quality content that would just make the world a better place if it could just access our content so that's what we're going to be talking about here and so there are a lot of ways to push a website into production and let the public access it you've got things from shared hosting all the way up to just having your own actual you know server or something running out of your house so what we're going to be using here is digitalocean there are many other options you don't have to use digitalocean we are going to utilize digitalocean mainly because digitalocean offers a one-click install app for django specifically there are a lot of little things that we're still gonna have to do after that but it will significantly make the startup process a lot easier to use digitalocean plus it's fairly cheap it's $5 a month for the smallest server but also you can pay hourly and that's point seven pennies an hour so really less than a penny to run through this tutorial and then you could close it if you wanted or or whatever so we're going to using digitalocean if you do not have a digitalocean account there is a link in the description that's a referral link it's if you want to support me in the channel and all that and you were already going to use the joy lotion anyways you can use that link if you want to stick it to me then I suppose you you could not use the link so anyway coming over here but why are you watching anyway come over here and once you sign into digitalocean that'll be like a big green button in the top right click that button and you'll be brought here this is just create droplets now normally what a droplet is your virtual machine it's your VPS okay so here are operating systems and you can choose versions and stuff like that but they also have these one-click apps that I was talking about we're interested in Jango on fourteen point oh four so take that that's fourteen point oh four is your operating systems going to be Ubuntu version 14 point oh four now after you're all said and done here you you can upgrade to fifteen point oh four or 1510 which is a now you can do what you want but starting at this point it's just going to be really easy you're not gonna have to configure a bunch of junk so choose that then we've come down here and we're going to go with five dollars a month or a point zero zero seven dollars an hour and then you've got a region I'm just going to go with New York it really doesn't matter but you kind of want to think about like who your main users are going to be and you want your server located close to them if that's what you're doing also if this is going to be your actual if you plan to continue using this I highly suggest you check backups it adds 20% to your monthly cost but it's worth it it's worth it to have it there things do go wrong things get corrupted you accidentally move something one time accidentally copied like clicked and dragged my root directory into some other directory I don't even know what happened and I could not fix that situation so I just reverted to a backup is super easy I'm not going to do it this is just a tutorials website don't do it if you're I feel like if you're just planning the nuke this server later on but it's definitely useful I've never regretted having them I've only regretted not having them finally the host name this is the name of your server you can call whatever you want I'm just going to call it Django Tut's good enough so go ahead and hit create there and I'm going to move this aside while that's creating and we can look at our beautiful website yeah I'm actually gonna move this aside well that's creating what you're going to want to do is first of all it depends on if you're on Windows Mac or Linux Mac and Linux are basically the same as far as SSH is concerned so if you're on Mac or Linux you would you would su just type in your terminal SSH and then the username which for now would just be root and then at and then the IP address so let's say it's like one ninety two five five twenty two thirty four twenty two okay that's all you need to do if you're on Mac or Linux if you're on Windows that doesn't work you're going to need something like putty so if you're on Windows get putty okay and that'll that's a that basically goes to here you put in your IP address and you're good to go so then so that's putty if for transferring files like just like so you click and drag file you're going to want to be able to do either SFTP or SCP you can also do SCP via the command line I just really don't like it it's actually easier for me to use the GUI form so I prefer to use winscp so if you're on Windows you can use winscp FileZilla also works on Windows and back and stuff like that so get those two things and pause the video while you're getting those by now your website should be where your server should be ready to go so I'm going to move this stuff aside and let's see if I got my email yet so when you sign up for a digitalocean droplet server you should be emailed and the email will contain the new droplet name the IP address the user name which will always be root and then a password the first time you log in it's going to make you change that password so let's go ahead and log in so I'm just going to copy and paste just copy that IP paste it in here hit enter wait for it my ticket maybe the server maybe I was too quick the server might not totally be ready if the server was like literally just setup you kind of want to wait like a minute or two let me go see if it's up on digital ocean pause this okay we're back so that probably took let's see the the droplet was created I'll just give you guys an exact time that that took three minutes from being created to when I could actually connect okay so here we have this is just a message basically any time you connect to a server for the first time it's going to send you the RSA these for now RSA to fingerprint so if it's the first time you've ever connected to the server that's totally normal that's fine you'll hit yes and continue on if it is not the first time if you've been developing on the server and then you go to connect to it and suddenly the fingerprint changed someone's something hinky has happened and you need to figure out what is going on it's probably not good so keep that in mind it's a new server don't worry about it hey yes so then you're going to log in here in our case go back to your email your username should be root and then they emailed you the password if you're using putty you should be able to copy just copy and then you come over to the terminal and just right click that pastes it in you just can't see it but right click that's good not sure if that works on Mac or Linux someone can comment below it should work on Linux I'm not sure about Mac anyway hit enter you should be fine there and then that then you're logged in ok then again it's going to force you to change that password because that was just a temporary password so again I'm just right-clicking hitting Enter and now I make a new password so my password is cookies with an exclamation mark protip don't tell anybody else your server password ok so once you're connected you're actually your website should be live so if you go to your IP address you can do this just copy this and go paste that into a browser you should see this and that should be a pretty familiar message from you from like one of the first tutorials when we set up Jango so you should have that and you should be happy to be seeing that message now basically this is here because we just have a really simple app right now so what we're ready to do is to begin moving our files over if you've been following along you should have all the files if you haven't been following along for like whatever reason you should you can go to Python programming net come down to web development or come over and then django and then right now i don't have all of them up yet so releasing them but you can come down to each tutorial and at the bottom there's the you can download the code so you can go to part ten and just download the zip file if you don't have the code or something else is wrong so let me move that aside and see so once you have that code it should you can extract it and you should basically have all this information okay so we've got blog my site personal the database and manage pi so with that infrared e to go what you're going to want to do is pop over to SCP go ahead new site and the hostname is your IP address if you're on FileZilla same thing it will ask for a hostname it'll be the IP address for some reason that has changed my protocol I don't want that I want to be on SCP or SFTP on FileZilla I don't actually think you choose that I'm pretty sure the only thing you pick is the port number make sure has 22 that's a secured port now user name what for now is root and then my password is cookies exclamation mark wait for it again this is the first time we've connected with winscp so yes wait for it again and you are in so now what we can do is well I'll leave that alone we're now you're in the root directory so with this one click install Jango is installed back here so if you're on winscp you can just kind of click back to this little error or the forward slash but you can also always go back so like for example going back to where oops that's the wrong one go back to where we were like here you can always just click these dots to go back to so now we're in the root directory or the main directory and django is hosted at should be i think it's in home so home yeah home django and then Django project and then we'll come in to actually say here so once you're on this page coming over here this is our like website data we're going to take blog my site personal actually we're going to leave my side out of this and we're actually just going to take blog personal and the database so take those and either you're on FileZilla or winscp and just click and drag them into the home django Django project so click and drag those in and while waiting on that that should be pretty quick it's just the initial stuff will take a sec but while we're waiting on that what's going to happen is basically what have we done well we've added new apps right so what's the first thing we always have to do well we've got to install the apps so clicking on Django project here just double click into that that's basically our site was you know my site slash my site this one click install makes it Django project Django project so everywhere we have my site we're just replacing with Django project we could kind of flip that we could change the core directories that lead to or the core files that lead to Django project either ones going to be a lot of effort so this one's I think the easier way to go plus this is what you're more likely to do you're more like if you're going to use someone else's application you need to be familiar with moving those over what what's required to implement so once you're in here we're going to go into the settings pie and that's this file here another pro tip I've set up before but I'll say it again don't share the secret key with anybody it's just in this tutorial simply because it has to be so making this big the first thing we have to do is we have to install the app so we come down here and if you remember we had two apps we have a blog and then personal ok save that the other thing we have to do is if you come down here we'll see that they're using a Postgres database instead I'm going to replace that we're using a SQLite so if you go into your these are the files this is the tutorial files leading up to this go into my site and then the settings dot pi let's go ahead and open that one up and I'm going to move this aside we're looking for the database this ok this is your databases so we'll take that copy/paste and we'll save that we are going to come back in and how we have to have one more thing but we'll come back to that so go ahead and exit that and I'm going to access this before I get confused and then we're going to open up URLs dot PI and then again going back over to our original website just go ahead and open up that one we'll just copy and paste them over so we need the URLs that point to the personal dot URLs and the URLs that point to the blog URL so let's move this over coming over here paste and then fix this okay save that close out of that and let me think here the next thing that we want to do this one click install is first of all it's on Django 14 point oh four for example so you could do upgrade and release and upgrade your actual operating system that's not as important but one thing to note is most likely like type Python and then import Django and then what you're going to want to do is do Django dot versions are just make sure y'all can see that Django version and you'll see we're using one point six point one we don't want to be using that I mean it should be fine but we're going to go ahead and update that so quit and what we're going to do is pip pip install – – upgrade Jiang go ahead hit enter there and now we've got one point nine point one we did see that we got this syntax error but it looks like it's still installed but we will check just to make sure so python import django and then Jiang Django dot all caps version okay so now we're pushing one point nine point one great so we'll go ahead and quit not all caps just regular okay so the next thing that we need to do is we actually should be good at this point let's go to our web site let's try refresh nope nothing yet I'm trying to think if there's something I've miss or if we need to just restart let's see so go go and do service G unicorn or actually the CEO is this is the right IP yeah jewnicorn restart we try to refresh now if that did it yeah okay so that's kind of like we're starting Apache right so anytime you make a change you might need to do that but I'm pretty sure you only need to do that if you've updated your like your settings in like your maybe the URLs PI and stuff like that you have to play around with that but a lot actually but it seems like Jango automatically updates a lot more than flask from what I found anyway once you've done that you can refresh and boom you've got a lot of things but you're actually you're still missing a few things right our images are gone and it looks like we're not pushing any bootstrap right there's no CSS here so moving this over we can go over here and we're like hey what's going on basically if you recall our our CSS is supposed to be impersonal and then static and then personal and then we've got the CSS fonts image JavaScript all that so normally it should work just already but what we're gonna have to do is two major things first what you're going to want to do is you want to nano slash etc' and then nginx and then sites available and then django and what that's going to do is is going to open up your configuration for your django application and coming down what you're looking for is the Django project static files directory in our case it's alias to /home django Django project Django project static take that in just ctrl C to copy it you're just trying to copy it and then go ahead we'll leave that up for now coming over to you SCP go into the main Django project Django project settings PI edit and come down to here we go static URL equals above that we're going to add one thing that's going to be a static underscore root equals full quote paste home django Django project Django project static make sure that's in there we're going to save that I'm going to exit out of that we're going to come over here control X do not if you added something don't save it and then we're right now we're in the root directory so you can do PWD to figure out where you are we're in slash root or the root user directory where we want to be is home django Django project Anka project right we're actually home django django project so change directory slash home django django project if you're not familiar you can start to types on the hit tab and it finishes it hit enter and now what we want to do is we want to run Python managed PI collect static hit enter and then basically it's just telling you you're going to overwrite some existing files are you sure you want to do that yes says 73 static files have been copied we'll come over here refresh and now we have HTML CSS well we already had HTML but we've got CSS we've got our part our styling is here we can bebop around we can open up stuff sort database is connected so that's good the final thing I want to show you guys is right now right now by the time you see this this will note not be hosted but if you just happen to know I had it hosted you could go to this IP address and visit my web site the problem is that's kind of lame like nobody actually has an IP address as a website they've got a domain name so I'm going to show you guys how you can connect a domain name to digital oceans so the first thing you're going to want to do is wherever you know you might have GoDaddy or I don't know the other there's a billion domain registrar's but go to whichever you use I'll show you domains Google comm so I've got a domain on on there well that's taken forever to load and then we can click on one of the domains and what you're looking for is to configure the domain name server so just looking at Google domains you can come here and like this is your configured DNS so that's your name servers you can click on that and I'm just making sure nothing that shouldn't come up should come on okay so here's your name service mine are already in but if you're on like GoDaddy or whatever the other hosts they're all the same you configure name servers and eventually you would say I want to use a custom name server in my case since we're hosting on digitalocean these are the three name servers so if you're on digitalocean use the exact same thing it's NS 1 dot digitalocean comm and s 2 and n is 3 same things put those in and then that can take I don't know how long the cycle is on Google but usually that can take up to 15 minutes to take effect if you're changing name servers like if you had name servers before and you're changing them that can take like hours and you may want to flush DNS on your whatever machine you're using but that's not we're talking about we're just going to keep it simple for now that's the first time it should take up to 15 minutes and you should be good once you've done that basically what you're doing is you're pointing this this is you know cyber io we're pointing that domain to the name servers for digitalocean that's one step the next step is on digitalocean saying what we want to do with that domain name so coming over to digitalocean you can click on the networking tab at the very very top and then click on domains and the one I'm interested in is well actually probably what you would do is you would add a domain let me pull this over so you would go to you've come digitalocean here and you go networking come down to domains and you'd add a domain so in my case I already have one added but you would basically just say here you would say for my domain at cyber I oh it already exists and then you can click here and you can either put in the IP address or when you pick click that you'll get a bunch of options of droplets that already exist when you're done you'll hit create record and then you should have something that looks like let me pull up the one that is for cyber basically you'll create a record and you'll see this okay and make sure that's actually the correct IP for what we're doing where's the websites 162 243 0 oh no it's not the right IP so let me change the IP let me copy this page this one might not this might not happen eventually but anyway so for me I'm actually changing the IP address for this record because I already had one before so you would put in here you shouldn't have to do this though like this should already be done for you if you followed the other step at all anyway I'll go ahead and save that and it says it was updated let me see here let's go to Cyberdyne items ok cool so it actually already worked so I've tied in that so really the thing that takes the most time is point is actually taking a domain from the domain registrar and pointing it to specific host name servers that takes a lot of time especially if you're changing the name service so if you changed hosts or something like that but the once you've done that part once that process is done and if it's the first time it's usually pretty quick but anyway once that's done you go to your whoever your host is in our case of a digital ocean and then you set up the rest there so anyway that's how you can tie like a domain name to the actual website and everything else should be done so anyways that's it for pushing your website to production if you have questions comments concerns whatever feel free to leave them below otherwise as always thanks for watching thanks for all the support and subscriptions and until next time

48 thoughts on “Publishing to a Web Server – Django Web Development with Python 11

  1. Restarting my Python / Django journey after spending some time in NodeJS and Laravel. Thank you for your tutorials man, you have no idea how much they not only help but INSPIRE others! Will definitely be supporting you via your website

  2. Do you have a video of how to setup nginx to run local Django server? and with SSL also, would be so helpful!

  3. 1st of all, Great Job thx for the tut,
    2nd. for some reason all of my posts, contents, blog navigation bar… wrap together.
    they don't go to the next line. they just appear as a continuous string, can anybody help me>

  4. I have deployed my website thanks to this tutorial. However, it does not work when I put "www" i front of domain. Eg: www.mydomain.com will not work but mydomain.com will work. Can anyone help ?

  5. I would really appreciate a new up to date tutorial about deploying a django 2.1.X project to Digital Ocean capable of serving STATIC and MEDIA files. The One-Click Django Application for the time being is for Django 1:1.11.11 so it's a little bit of a problem for example with 'path()' and 'url()'.

  6. As always , brilliant video. I configured my server (Linux, Django 1.11, Apache2, MySQL) for deployment and it worked fine. Sharing all the steps in this article :
    https://letssolveeverything.blogspot.com/2018/12/production-server-launch-using-django.html

  7. So simple, easy to follow, 10/10 would recommend. If anyone has any issues feel free to comment below and ask me for help 🙂

  8. Explain please how to use digitalocean with python 3, because it actually uses 2.7 by default. Despite changing default python version to 3.6.6 via "alias python=python3" it still doesn't react.

  9. Hi,
    I've set up django website with nginx and gunicorn on a default apache2 ubuntu server. But when I runserver I can't access my_ip:8000 even after binding gunicorn with project.wsgi in 0.0.0.0:8000

  10. Hey Everyone! If you are watching this and are receiving the 502 ERROR, one way to fix it is to not update django. I skipped that step and receive no errors. Also, if you are watching in 2018, and are working in django 2.0, you probably want to use an earlier version in your code.

  11. Finally completed the Django series. But I have done it through pythonanywhere for hosting. Thank you sendex.
    My website: http://ishritam.pythonanywhere.com

  12. thank you for this django series..my question is,….where is the actual blog setup like in the admin where users can add comments,….. ??..its only in the admin section…how will users post comments??..ok thank you

  13. Nowadays it's easier to take free ubuntu server and set django on it. Cause i wanted to use Python 3 and Django 2

  14. Windows 10 does support SSH now. You have to enable Developer Mode first, then install the OpenSSH client through the 'Manage Optional Features' tab.

  15. I got the website to work on my ip address but not on the domain. I get 502 error on domain name only. I configured the dns on goddaddy.

  16. You rock Harrison!

    To SCP source to Digital Ocean remote from Mac or Linux:

    a. cd to mysite directory
    b. scp -r blog personal db.sqlite3 [email protected]<your webpage IPv4 address>:/home/django/django_project

  17. I am getting a 502 Bad Gateway, and " nginx/1.10.3 (Ubuntu) " writen below. Any ideas how I can sort this out please? I've tried all the suggestions in the comments so far, and was unable to sort it out.

    The pip upgrade of django was successful, and yet still receiving the error.

    Any ideas how I can fix this sentdex? Thank you in advance!

  18. The first time I try this YOUR WAY, I banged my head on the wall for two days. For some reason I could not display images nor login to the database. Whatever solution I got from google still not working. So I decided to start from a clean Ubuntu server and then install Django and IT WORKED! the first time. So a little advise for someone DON'T DO ONE CLICK APP.

  19. I drove myself crazy troubleshooting this step. If you are getting a 502 after restarting gunicorn like I did don't pull your hair out and read https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image-for-ubuntu-16-04 which is documentation that isn't outdated. Really frustrating when one part of the puzzle decides to update! Anyway fantastic tutorial series and my issue wasn't the author's fault at all.

  20. have a 502 gateway error downloaded django 2.0 and now have a url import error this shit is really starting to get on my nerv

  21. Best tutorial ever!! But what if I don't want to use Digital Ocean (I have my a computer that I run as a server)?
    Tkanks

  22. Very helpful video, just one question: You said that you want to switch off debug mode before pushing a site to production. Did I miss that part or didn't you switch off debug mode? In any case: How is it done?

  23. Thanks sentdex. It worked for all web pages. However, when I try to login to the admin, it said "OperationalError at /admin/login/ unable to open database file" Appreciate if you could give some advise.

Leave a Reply

Your email address will not be published. Required fields are marked *