Python Project Deploy in Cpanel Production

These are the simplest steps to change and deploy the python project in cpanel (host sharing server) before sending code to live server. These all the sample codes for production (live) server.
- Check your all the dependencies in pip list and export those dependencies modules in requirements.txt file.
- In settings.py check these lines:
-
BASE_DIR = Path(__file__).resolve().parent.parent
-
DEBUG = False
-
ALLOWED_HOSTS = ['your-domain-name' 'www.your-domain-name.com']
-
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'#for development path only for static file
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
].
-
To serve the media files in production sever (where cpanel is share hosting):
- MEDIA_URL = "/media/"
MEDIA_ROOT = "/home/your_username/public_html/media"
(Note: For media file the location must be inside public_html in share hosting cpanel)
To serve the static files in production sever we should install whitenoise in application:
- First install whitenoise
- pip install whitenoise
- Add these lines in middleware:
- MIDDLEWARE = [
# ...
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...
]
- MIDDLEWARE = [
- Change in wsgi.py with these lines:
-
import os
from django.core.wsgi import get_wsgi_application
# importing whitenoise
from whitenoise import WhiteNoiseos.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_application_name.settings")
application = get_wsgi_application()
# wrapping up existing wsgi application
application = WhiteNoise(application, root="static")
-
-
Don't forget to run the command "python manage.py collectstatic" which makes a new folder in your project folder as per your settings.py file
-
Then move your all application folder with zip format in cpanel.