Skip to content

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:

  1. Prepare Your Django Project for Deployment:

    • Ensure your prod.py settings file is properly configured for a production environment. This includes setting DEBUG to False,configuring the allowed hosts, and setting up the database, static files, and other production-specific settings.

    Tip

    prod.py
    from .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')
    
  2. 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.
  3. 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.
  4. Install Dependencies:

    • Use SSH to access your cPanel account or use the cPanel terminal.
    • Navigate to your project directory and create a virtual environment:

    Tip

    bash
    python3 -m venv myenv
    source myenv/bin/activate
    
    • Install your project dependencies:

    Tip

    bash
    pip install -r requirements.txt
    
  5. Configure WSGI:

    • Ensure your project has a wsgi.py file configured correctly. It should look something like this:

    Tip

    Python
    import os
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.prod')
    
    application = get_wsgi_application()
    
  6. 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 your wsgi.py file and the Application entry point to application.
    • Set the Environment variables to include DJANGO_SETTINGS_MODULE=myproject.settings.prod.
  7. Configure Static and Media Files:

    • Ensure your prod.py settings handle static and media files correctly.
    • In cPanel, use the File Manager to create the static and media directories if they don't exist.
    • Collect static files by running:

    Tip

    bash
    python manage.py collectstatic
    
  8. 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.
  9. Run Migrations:

    • Apply the database migrations by running:

    Tip

    bash
    python manage.py migrate
    
  10. Security and Permissions:

    • Ensure that your media and static files have the correct permissions.
    • Secure your database credentials and secret key.
  11. 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.


Reference