Django in production
Deploying a Django project with multiple settings files, such as base.py
, local.py
, and prod.py
, to a shared cPanel environment involves several steps. Below is a step-by-step guide to help you with this process:
-
Prepare Your Django Project for Deployment:
- Ensure your
prod.py
settings file is properly configured for a production environment. This includes settingDEBUG
toFalse
,configuring the allowed hosts, and setting up the database, static files, and other production-specific settings.
Tip
prod.pyfrom .base import * DEBUG = False ALLOWED_HOSTS = ['yourdomain.com'] # Database DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_db_name', 'USER': 'your_db_user', 'PASSWORD': 'your_db_password', 'HOST': 'localhost', 'PORT': '', } } # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static') # Media files MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- Ensure your
-
Prepare Your Environment:
- Ensure you have Python installed on your cPanel. You might need to use the Python Selector or Setup Python App feature if available.
-
Upload Your Django Project:
- Use the cPanel File Manager or an FTP client to upload your Django project files to your hosting account. Typically, you'll place these in the
public_html
directory or a subdirectory.
- Use the cPanel File Manager or an FTP client to upload your Django project files to your hosting account. Typically, you'll place these in the
-
Install Dependencies:
- Use SSH to access your cPanel account or use the cPanel terminal.
- Navigate to your project directory and create a virtual environment:
- Install your project dependencies:
-
Configure WSGI:
- Ensure your project has a wsgi.py file configured correctly. It should look something like this:
-
Setup the cPanel Application:
- In cPanel, go to the
Setup Python App
section. - Create a new application and point it to your project’s directory and virtual environment.
- Set the
Application startup file
to the location of yourwsgi.py
file and theApplication entry point
toapplication
. - Set the
Environment variables
to include DJANGO_SETTINGS_MODULE=myproject.settings.prod.
- In cPanel, go to the
-
Configure Static and Media Files:
- Ensure your
prod.py
settings handle static and media files correctly. - In cPanel, use the
File Manager
to create thestatic
andmedia
directories if they don't exist. - Collect static files by running:
- Ensure your
-
Configure Database:
- If you are using a PostgreSQL or MySQL database, ensure that it is created in cPanel.
- Update your
prod.py
settings with the correct database credentials.
-
Run Migrations:
- Apply the database migrations by running:
-
Security and Permissions:
- Ensure that your media and static files have the correct permissions.
- Secure your database credentials and secret key.
-
Testing:
- Access your domain to verify the deployment. Debug any issues that arise by checking the cPanel error logs and your application logs.
By following these steps, you should be able to successfully deploy your Django project on a shared cPanel hosting environment with the production settings from prod.py
.