- Step-01: Prepare Django Project
- Step-02: Logging Into the Cpanel
- Step-03: Create Subdomain
- Step-04: Setup Application
- Step-05: Setup MySQL Database
- Step-06: Upload Project Files
- Step-07: Final Setup
-
Make sure
DEBUG = False
insettings.py
-
Set
APPOWED_HOSTS
to your domain or used*
to accept any domainALLOWED_HOSTS = ["yourdomain.com"] # you can used multiple domain or subdomain # or ALLOWED_HOSTS = ["*"]
-
Integrate
whitenoise
inside theINSTALLED_APPS and MIDDLEWARE
insettings.py
:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'whitenoise.runserver_nostatic', # This must be added before 'django.contrib.staticfiles' 'django.contrib.staticfiles', 'portfolioapp', ]
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # This must be added here after SecurityMiddleware & SessionMiddleware 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
-
Set
Static & Media Files
root insettings.py
STATIC_URL = 'static/' MEDIA_URL = '/media/' STATIC_ROOT = BASE_DIR / 'staticfiles/' MEDIA_ROOT = BASE_DIR / 'media/' STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
-
Final Overview of the
settings.py
DEBUG = False ALLOWED_HOSTS = ["yourdomain.com"] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'whitenoise.runserver_nostatic', # This must be added before 'django.contrib.staticfiles' 'django.contrib.staticfiles', 'portfolioapp', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # This must be added here after SecurityMiddleware & SessionMiddleware 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] STATIC_URL = 'static/' MEDIA_URL = '/media/' STATIC_ROOT = BASE_DIR / 'staticfiles/' MEDIA_ROOT = BASE_DIR / 'media/' STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
-
Modify main project directory
urls.py
file.from django.contrib import admin from django.conf import settings from django.conf.urls.static import static from django.views.static import serve from django.urls import path, include, re_path urlpatterns = [ path('admin/', admin.site.urls), # Other paths... ] urlpatterns+=re_path(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}), urlpatterns+=re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
-
Add all required the packages and their version that you used in this project. Also must be add
gunicorn, whitenoise
packages for development. Otherwise some time you fetch lots of issue for static and media file after deployment.:Django==5.2.3 gunicorn==23.0.0 pillow==10.3.0 whitenoise==6.9.0
For auto create
requirements.txt
used below code:pip freeze > requirements.txt
-
From the
Domains
Page click to theCreate A New Domain
button. -
Then create and submit
#Ex: test.yourmaindomain.com
- Go to
Setup Python App
page - Click
Create Application
button Python Version:
Select Python VersionApplication root:
Enter project directory name. Where you upload your project files.Application URL:
Select theDomain or Subdomain
- Then hit the
Create
button.
-
Go to
Manage My Databases
-
On the Create New Database section set the Database name then hit the
Create Database
button -
Then scroll down and find
Add New User
section. Here set the databaseusername, password
-
Again scroll down and find
Add User To Database
section. Then selectUser and Database
-
After Add it redirect to the
Manage User Privileges
from here selectALL PRIVILEGES
then clickMake Changes
button.
**🚨 Alert:**
Must be copy the Database Name, Username and Password. Because need to configure database on the settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_database_username',
'PASSWORD': 'database_user_password',
'HOST': 'yourhostdomain.com',
'PORT': '3306',
}
}
-
Go to
File Manager
page -
Find the Application root directory.
-
Upload the project file as
zip
-
Edit the
passenger_wsgi.py
and add below line and save it.from yourProjectname.wsgi import application
**🚨 Note:**
Must change theyourProjectname
based on your project name
- Go to
Setup Python App
page - Select your app and click Edit Icon
-
From the application copy the
virtual environment
command -
Open the Terminal and Paste the command
-
Then run the below command:
-
Install requirements file:
pip install -r requirements.txt
-
Migration and Migrate the database:
python manage.py makemigrations
python manage.py migrate
-
Then Apply collect static command to collects all static files from apps and puts them into one central directory for easy serving in production:
python manage.py collectstatic
-
From the Application Setup Page apply the command manually
-
Write and Add
requirements.txt
into this section and clickRun Pip Install
. Note: Sometimes it send error message first try. If you fetch error then run again. -
Then into this section run other command and click
Run Script
button withoutpy or python
:After that click the
RESTART
button. Now you can visit your application.