Deploying a website online always become a cramp-breaking task, as we need to prepare web-server code for production environment, if we talk about a Python Django project we need to create a list of libraries that we have used in our Python Django project, need to edit configuration files and in some cases there is need for changing urls also. In the post we will see step by step guide for hosting Django project on Google cloud console i.e Google App Engine, whole process will take at max 30 minutes to set up. So, let’s start:
Django Project Overview :
Setup Django Project for Hosting:
First step is make a list of libraries, installed while creating your Django Project. Here is bonus tip, every-time starting your Python Django Project create & activate virtual environment this will isolate installed libraries.
- pip freeze >req.txt
Above command create file named req.txt which includes the list of libraries used in our project. Now just paste this file where in Project directory and create a compressed tar file .
- tar -cvzf cpanel.tar.gz cpanel
Setup Google App Engine Vm instance:
- Allow HTTP traffic
- Allow HTTPS traffic
Now install pip3 in vm instance typing following command in ssh terminal –
- sudo apt-get install python3-pip
Setup SCP ssh key :
- ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C [USERNAME]
- Example: ssh-keygen -t rsa -f ~/.ssh/key -C Linux
- cat ~/.ssh/key.pub
above commands will create and show a public ssh key that you have to add in Google cloud console.
Go on Google Cloud dashboard and navigate to compute engine -> metadata -> ssh keys -> edit
here add ssh key you got in previous step .
Using scp command for transferring files :
- scp -i ~/.ssh/my-ssh-key [LOCAL_FILE_PATH] [USERNAME]@[IP_ADDRESS]:~
Setup Django Project on Vm instance :
- tar -xvzf cpanel.tar.gz
- cd cpanel
- sudo pip install -r req.txt
So, here comes our final step for which we are waiting let’s run our Django project use below command and access your live website on vm instance public I.P. address.
- sudo python3 manage.py runserver 0.0.0.0:80
There are too many ways for running django in background, i personally prefer Nohup command for running project in background.
- sudo nohup python3 manage.py runserver 0.0.0.0:80
there might be conditions you need to kill nohup process for server upgrade use kill command.
- ps -ef | grep python3 manage.py runserver 0.0.0.0:80
- killl process id.