Deploy or Host Django Project in CpanelThis blog will walk you through the process of deploying and setting up a Python app or Django project in Cpanel. Lets Starts …
Let’s Make the Django Project Ready for Production with a few pre steps
Creating Requirements A Text File which contains all the necessary modules used in the Django Project
- Have a virtual environment activated
- Open up terminal in same project dir.
- The Run command
pip freeze > requirements.txtthis will place all of the module names and versions which you have installed in your environment.
Lets start by deploying the Django project in cpanel.
- Upload your project to github
- Copy repo url
- Open terminal from Cpanel Main page
- Terminal opens in home dir, clone project in home dir.
git clone repo_url
file manager and upload your project. Note: You need to zip the file before uploading it, then after you need to unzip it again. overwhelming huh? Go with the GitHub one.
Creating a Python App in Cpanel
Creating Python Apps (Django App)Go to the main page of Cpanel, search for
setup python app , open it.
Setup Python appNow here you need to fill in the requested info. Application Root-name of your project folder which you have uploaded in your home dir before Application Url-Url/domain url of your app. I kept the same And to create your app, click on the blue create button there. After that, this will look like this: Setup Passensger_wsgi.py
Setting up the passenger_wsgi.py fileThis file is created in same project folder by cpanel while setting up python app locate the file, it is present in home dir > yourprojectfolder Click once on that file. Once on, the Edit button will appear in the top bar. Edit it. put from .wsgi import application Done… Note: yourprojectfolder i mean the folder which contains settings.py ,wsgi.py, asgi.py etc..
Installing modules or requirements in virtual environmentclick the virtual environment path it will copy it, which you will see in the top after creating a Python app. Open the terminal and go to the project folder. command goes cd hit enter, and then paste the environment path you just copied and hit enter. The run command pip install-r requirements.txt will install all the listed modules in files.
The most important steps to finalize the setupEdit your settings.py file, search for ALLOWED_HOSTS =  Make it such ALLOWED_HOSTS = [“*”] or you can replace * with your websiteurl for example.www.itsnp.org
Setting up static and media files for production of the Django projectthe is the most overwhelming steps.. This is how it looks before in most cases. Now we are going to change this for production. STATIC_URL = “/static/” STATICFILES_DIRS = [BASE_DIR / “static”] STATIC_ROOT = BASE_DIR / “staticfiles” # need to change this MEDIA_URL = “/media/” MEDIA_ROOT = BASE_DIR / “media” # need to change this
STATICFILES_DIRS: This isThe following is a list of all the directories where folders containing static files are present.
STATIC_ROOT: This is So serve as the folder where all of the static files are production foldercollected later on in production.
MEDIA_ROOT: Same goes for media root also.. all the uploaded files are saved in media root.
And inside the petpup folder of a subdomain or domain, all the root folders like STATIC ROOT, MEDIA ROOT are created and served in production.
So now this is how it will look after replacing BASE_DIR with the absolute path of the applicationfolder inside of a subdomain or domain.
Now Django will search for all of the static and store media files in
STATIC_URL = "/petpup/static/"
STATICFILES_DIRS = [BASE_DIR/"static"]
STATIC_ROOT = "/home1/itsnporg/sachit.itsnp.org/petpup/staticfiles"
MEDIA_ROOT = "/home1/itsnporg/sachit.itsnp.org/petpup/media"
This might be so overwhelming to get it as a beginner.