Deploy Django with Nginx , Gunicorn , PostgreSQL on Linux Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey developers so today i'm gonna show you how you can deploy your django website on ubuntu or linux based server or virtual private server with the help of nginx and we are also going to integrate our postgres database so without wasting time let's jump into it so currently in my vs code i'm i have opened my django project which is my portfolio website and it is currently running on the local host so let me open this and this is my website this is how it looks and here are the projects work experience i also like vlogs here will be the vlogs so i deleted the media folder so that's why it is not showing the image but that's not a problem but so here is the contact form and photo so that's all okay let's close this so i will so i am assuming that you have purchased the virtual private server or the linux server from anywhere so it doesn't matter from where you purchase the server so once you have that you can access to the server i think from putty or from powershell powershell so what is putty so it is this if you you can just download this you can google this and download this piece of software and you can put the ip address of your server and post port 22 and then you can log in here login using root and i will type in my password and yep that's connected so i will close this and i will open my powershell from which i can access this so this is currently so this is a nice ui so that's why i use it ssh root and the rate i will put my ip address here so it is showing that verification failed so what i will do is i will go to my computer and i will head to my c where is my window installed my username and i will delete the ssh folder so if this error shows to you then you can do this so i will run this command again and i will type yes so it is added to my trusted host so i will type in the password so this error only show up when you are trying to connect again if you have done that before and you're trying again to connect it so this error might show up so you can do this like what i did like just delete that dot ssh folder from user and user pro user folder and then that folder might be hidden so you can unhide that and delete that so this is my fresh ubuntu server so whenever you get linux fresh server so what you have to do is sudo apt get update so firstly you have to update your linux server this might take some time i i think i have already updated that so this won't take too much long so i am just doing against so this might take some time on your server so next what we are going to do is sudo app get uh install these okay so we are installing python 3 pip and python 3 dev and lib pq dev and postgres and postgres contrib and nginx so that's what we are we all need so it didn't take too much time because i have already installed them and this might take some time on your server so now what i will do is i will go to my postgres terminal i think i forget to add uh sorry i think i will just copy some command sorry we are already in our database so you post chris terminal so i will hit this so firstly i have already created that so i will drop the database drop database my project group database and i have already created created a user with the password so you can use this command and you can type your password whatever you want so for security purpose i have done that before so i won't lick my password here so you can just type in here random commands and you can create your user so what you will first do is firstly you have you will create your database which will be this and then you will create your user like i show i showed you before with this command and i'm not going to do it because i have already created that and next we are going to give it some permissions and some character encoding so i'll roll and next we are going to give it some more permissions and then we are going to set the time zone for this user with the help of this command let me just copy it so paste it anyway so with the help of this command you can set the time zone okay and at the last for this postcard setup i will grant all of the permissions or all of the privileges on the database my project to my project user so this is the username which you can create which you will create okay so that's created now i will quit from here and now i will install pip i will install the virtual env which is our package to create our virtual environments so i think i have already installed that so requirement already satisfied that was already installed so if you are if you haven't done that so it might take some time to install and then what you can do is make a directory which will be my project okay then cd into my project and then you you will have to create your virtual environment sorry i will okay i will move that so i will create my environment again which is so you can give this name my project env so our environment has been active uh created and now we will activate it by source env slash pins slash activate so this is how you activate your virtual name and sorry i think i misspelled spell something so this is our my project so your my project env so put the name in here and yep our environment is working as you can see it here so i will just clear the terminal and now i will install django g unicorn and psycho g2 so django you already know what it is and unicorn is our one of the dependencies that we need to install so that we can configure it with our nginx so that we can deploy our website very easily and cyclop g2 is the connector for django and postgres so with the help of cycop g2 we can connect our postcase database to our django project okay so these are installed now so let's create a project with the help of django admin command so i will copy this command and we will start our project we will start our project with the django admin start project and give it a name and we will give it a path okay our my project has been created and now we will go to our so let's navigate to our project file uh project folder and then let's list what's it in here so yeah we have our settings.py so i will use nano editor and i will put the file name there and here i will so here you can put your hosts so for now i am just going to allow every host and remember this is not a good option or this is not a good practice to do but just for testing or just for this video i'm gonna do this so remember you don't need to do that and now i will do some database configurations so let's move up and go to our database okay let's leave it here we are gonna connect our postgres database to our portfolio website so this is just a sample project which we are gonna set up and in here we can define our static route and here we will have to do path so if you are using django version 3 or above than that you have to do this you have to do this okay instead of os dot which was done before okay i think i saved open anyway yes i think i added e and let's move again to the bottom of this file and let's see if there's something wrong no so we will save this file with control s and i will exit the file with control x okay control of uh sorry save the file with ctrl s and exit the file with control x so now we are going to migrate our database so it will be in sqlite3 so let's do this migrate and boom our fight database is created db.sqlite3 which is by default provided by chango and now we will go to our manage.py and create a super user create super user i will name it admin i will leave the email address blank admin addit123 administrate one two three is my password for the user super user and now i'm gonna use the manage.pui for collect static so it will collect our files so remember this address okay we will use in the our nginx configurations okay so let's list our so directory and we have our static folder so let's print out our working directory hole so we are working in slash root and slash my project so remember this address okay so you remember this route and also this one we will need later in the nginx configurations so for now we are gonna allow our nginx or our web server to allow some traffic at port 8000 okay so now i will run my server django built-in server at the local host sorry at the current pc ip address at the port 8000 and if i go to my ip address and here to the port 8000 yep our website is working cool okay let's stop this server and we have installed g unicorn so let's test again it with g unicorn bind 0.0.0.0 and 8000 which will be my project dot whiskey or wsgi i think misspell something my p r object wsg is so let's again go to our browser and let's type in the ipaddress of my server and let's yep so our g unicorn is also working so next we are gonna do some unicorn stuff so let's deactivate our environment so environment has been deactivated so and but now we are gonna enter this command sorry not this this command so we are going to over in etc folder directory and in that we are going to the system md directory and in that we are going to the system directory and in there we are going to make a file with name g unicorn with extension dot service okay let's enter that so i'll copy some code and i will paste here so so if you remember i told you to remember the path and our user is root so where was the my project my project was in the root folder so remember to give it the full path of your whole server sorry whole project where it is stored so root my project my project in which unicorn that's fine and instead of home semi oops so let's go again there so let's open it anyway root my project and next file we were editing this one and let's back for backspace this root my project my project and then we are going to define my project.whiskey.app this key colon application and boom we have saved that so let's enable our g unicorn okay so with the help of this command why are you not running oops yeah it's working so i'm gonna run this command sudo system ctl enable g unicorn so this will enable our g unicorn it will create a sim link and now we can start our g unicorn server with the help of this command i'm gonna copy and paste sudo system ctl start unicorn and yeah it started and let's check what's the status of that so i'm gonna paste sudo ctl status g unicorn and it will stay tell us this yo active running cool so we will exit this clear awesome and last list so our my project dot shock file has been or this is the socket file this has been created and now we can run over so now i'm gonna paste this command sudo journal ctl dash ug unicorn and i don't know what this thing do okay let me just read so this will so this file helps you in debugging i think so you can just cut out from here so let's clear the console and now we are going to demon reload our system ctl and we are gonna restart our g unicorn and now we need to make our nginx configurations okay so with the help of nano editor with super user command we are going to etc folder and then nginx and then sites available and there we are going to create a my project file okay so let's hit enter and now i'm gonna copy some code and paste in here so you can drag copy you can pause the video or you can just write or i will provide the code in the description so you can copy it from there so what will my server name so my server name will be i think this so you don't need to put the http or backslash or forward slash whatever it is and after giving a space you can write your domain name where it will be hosted okay and you can also delete these the this line not these okay so for static we are going to our root directory root my project and for proxy we are going to root my project my project dot socket file and we are going to save with ctrl s and we are going to exit our file with control x okay cool so now what we are going to do so now we are going to do this command we are going to execute this command sudo ln dash as slash etc engine x sites available and in my project so it will copy this file and it will paste this file in this directory okay so this will pull out the website configuration from site available to sites enabled so this is a very crucial step to do and now we are going to check if our nginx syntax was correct or not or if there is a error or not sorry forget the engine x okay configuration file engine x syntax is okay for nginx.com text is test is successful okay so if you know that so if you have been following along so you know we allowed our server to host something at port 8000 so now we are gonna delete that rule and now we are going to allow nginx to fully access our server okay in that case we can allow any url to the server and for error locks you can use this command to see what are the nginx logs so you can use this command okay so we are going to hit enter so currently we haven't run anything so this is this was blank and now what you can do is i'm sorry i'm gonna express this and name i i'm gonna use this gnome and root and in my project and there will be my project dot sock and boom so if you can see that in here we have our these permissions these permissions and these but for root for root directory we don't have these permissions so we are gonna set that so how we can set that pseudo ch so i think with chmod i think we can set 6 6 4 which will be for root i think we can provide it absolute path so let's run this command again okay our permission are not exact as we wanted so we will do 6x6 and we are going to run this command again okay forget about that so we will run 777 which is not a good practice so but anyway for the sake of this video i'm gonna do this so let's clear our terminals so now what we are going to do and now we need to check our postgres status which we can check using sudo systemctl status postgres which i will hit enter so now it is activated and now i will start my postgres service with the help of not this with the help of this command and our postgres services has been started and i will do sudo systemctl and i will copy some command okay i double pasted them so i will enable my services our postgres services has been enabled now and i will restart the jion g unicorn not this but i will okay forget i will just type sudo system ctl restart g unicorn our g unicorn services has been is re restarted and now with the help of diamond reload we are gonna take reload we are going to reload our unicorn and we can start our g unicorn here so again at the end we have this command sudo nginx minus t double land and sudo system ctl restart yeah miss boundaries taught engine x cool or testing successful and so let's go to our ipads let's check yeah boom it is running so now what we will do is we will install of name filezilla you can download it download it from google and i will open it here and i will copy my ip address so you can log in here with the help of how you can log in you can type in here sftp colon double slash then your ip address and then you are going to put your username and then you're gonna put your password and then port 22 and then click enter so now you will connect to the server why it is not connecting i think my password was wrong let's check again using yep we are connected so our files has been listed here we will go to our so let's go again to the root folder and in our my project open this so this is our project we are gonna just delete it and with the help of here okay let's list out what is here so we can sudo our minus rf this will remove our static folder and let's enter now our emp folder left and we can also delete tp sorry sudo r minus rf tv that's rm [Music] okay click enter so let's list so we have our manage.py file we can also delete that okay cool so we are not going to exit it so here is my portfolio website i have open i have already opened it here and now i can copy these two folder so this might take some time and i will also copy this here so i will fast forward this this might take some time so if this is taking some time so let's configure or modify our junecon dot service file so i'm gonna find the path where we created so yeah found it so at this address you can type this command and you can go here my project and instead of my project dot whiskey you can type praveen dot whiskey and ctrl s to save and ctrl x to exit okay let's see how it is running so our 52 file has been queue in the queue and let's wait for that this might take some time so don't get frustrated so i will go to my so what i will do is i will click here and list out again what we have here and now i will go to my project and i will copy this file here is it is it pasted nope but let's go to our project and my projects i think this is in templates portfolio and let's copy these two and let's put into our putty and we'll copy it here all files transferred and now we will open our terminal and let's list all files are stored now and now we can run sorry firstly we have to activate our environment source my projector env then bin then activate our environment has been activated now as you can see that now we are gonna install all of our dependencies from requirement.tx as you can see in here these are our all of the dependencies white noises for settings sorry is for serving static files and pillow is for image processing and this is the seek editor and this is a dependency for seek editor this is a django and i don't know why i use this but anyway let's install that so cool or all of the dependencies has been installed and let's go to our project and let's go to our nanosettings.ui and in here our debug is equal to true so first of all we are going to set this okay let's keep it this way and for our database we can uncomment here and we can delete this one which is configuration for dv dot sqlite tree so databases and postcycle g2 and whatever was our project sorry what was our database name so wait a second i will just put it over here so that was our my project the name was my project and the username was this and i think this was the password so don't you even try i will change that after this video uploading on the server so let's try running our so let's migrate our data to our postgres database so with the help of python manage.ui we will we can migrate um okay our password is wrong sudo we will go to our project and let's see what was our password so let's save that and let's run our migration again okay cool hour so this password was correct and now reload our g unicorn how we can reload that we can reload with this command where it is yeah there it is okay so you can restart your g what system okay we have to damn and reload demon reload whatever you can pronounce this whatever you however you can pronounce it and we have done that so let's check again our nginx configurations okay that's successful and we are gonna restart our nginx engine x started and now we are going to check status of our postgres database and we are gonna type these i'm gonna just paste this command systemctl start postgres and enable postgres so yep that has done so we are going to restart our gun gun with the help of this command paste it anyway and now with the help of this command i will check for nginx configuration and restart nginx so now let's go to our ip address okay cool so website is showing up but it's not how we expect it so let's run python manage dot py collect static so files has been collected and now we are going to restart our unicorn and again we are going to demon reload and restart unicorn these two commands and we are gonna check for nginx configurations again and restart it so cool that has been restarted so let's go again to our ip address so yep this is connected awesome our website is now working so how now how we can configure our ssl certificate so as you can see here our website is not secure so we are gonna so i'm gonna show you how you can set up and free ssl certificate so for that you have to run this command i will provide this into description so this will take some time let's wait for that okay cool so engine uh sorry our search boat is now installed and now we you can type this command sudo search board dash dash engine x and it will look into your so firstly it will ask for email address so i'm gonna paste my business email install my 0.36 gmail.com and it will look into your engine x configuration and it will take uh take domain name from that and it will suggest you if you want to create an ssl certificate for that domain so it will give you some options so you have to agree for that yep agree oh no yes not so as you can see here it is picking prayer private military dot me and select option one so i will select one so it is requesting for a certificate requesting certificate for my website where it is on this so congratulations you have enabled https okay so if we go to this link i will open my browser and boom we have successfully configured our ssl certificate and now we have working website awesome so how did i connected my domain to that server so if i go to my products so if i check it here i will go to the manage dns and and in the area code i filled my ip address and it automatic so my domain name which is this so this automatically point to that ip address and shows the website okay so that's cool so now we can what you can do is you can create your super user here so i will do python manage dot py create super user i will give it my name and with my email address and for password i'm gonna show you okay so my admin pass admin user is now created and now i can restart the g unicorn with the help of this command and now i can download reload and restart unicorn again and now i can restart my nginx with the help of this command so let's okay we have already the website running here i think okay so let's go to the link and go to our admin panel our admin study files are working cool and also you know working fine as expected so this is my password i hope you don't see that i will save it and here here is our admin panel so what we did so we firstly installed our all of our packages all of our packages and we created our database and we created our g unicorn configuration and then we configure our jion cone with the nginx and we enabled our postgres database and we created a free ssl certificate so that's it for this video see you in the next
Info
Channel: CodingVampire Praveen
Views: 1,696
Rating: undefined out of 5
Keywords: django, python, dennis ivy, python programming, web, web development, django website, django website upload, django website deploy, django with nginx, coding for entrepreneurs, django website with nginx, nginx with django, django ssl, django ssl setup, django wuith gunicorn, django gunicorn nginx, django postgres, postgres django, postgresql
Id: ynTGME4UKt4
Channel Id: undefined
Length: 44min 37sec (2677 seconds)
Published: Tue Jul 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.