Estimated read time: ... min
Anonymous Author

 |  73 views  |  0 likes

Login to Like

Automate Django Deployments to cPanel: Complete CI/CD Guide

Introduction

Deploying Django applications to cPanel manually is time-consuming and error-prone. In this guide, I'll show you how to automate your Django deployments using a simple deploy.sh script that handles everything from committing code to running migrations and restarting your server.


Why Automate Django Deployments?

  • 🕒 Saves hours of manual deployment work

  • 🔒 Reduces human errors in the deployment process

  • ♻️ Consistent deployments every time

  • 🚀 Enables continuous delivery practices

Prerequisites

Before we begin, ensure you have:

  1. A Django project ready for deployment

  2. cPanel hosting with SSH access

  3. Git installed on both local and server

  4. Python virtual environment on server

  5. Passenger or similar WSGI server configured

The Deployment Script

Here's the complete automation script we'll be using:

bash
Copy
Download
#!/bin/bash
set -e  # Exit immediately on error

# ======================
# CONFIGURATION
# ======================
PROJECT_NAME="yourdomain.com"
REMOTE_USER="cpanel_username"
REMOTE_HOST="your-server.com"
REMOTE_PORT="22"  # Default SSH port
VENV_PATH="/home/${REMOTE_USER}/virtualenv/public_html/${PROJECT_NAME}/3.11/bin/activate"
PROJECT_PATH="/home/${REMOTE_USER}/public_html/${PROJECT_NAME}"
GIT_BRANCH="main"

# ======================
# DEPLOYMENT PROCESS
# ======================

# 1. Package Requirements
echo "📦 Updating requirements.txt"
pip freeze > requirements.txt

# 2. Start Development Services (like Tailwind)
echo "🎨 Starting Tailwind CSS builder"
python manage.py tailwind start &
TAILWIND_PID=$!
trap "kill $TAILWIND_PID 2>/dev/null" EXIT

# 3. Commit Changes
echo "💾 Committing code changes"
git add .
git commit -m "Auto-deploy commit $(date +'%Y-%m-%d %H:%M')" || true
git push origin $GIT_BRANCH

# 4. Remote Deployment via SSH
echo "🚀 Deploying to production server"
ssh -p $REMOTE_PORT $REMOTE_USER@$REMOTE_HOST <<EOF
  set -e
  source $VENV_PATH
  cd $PROJECT_PATH
  
  echo "🔄 Pulling latest code"
  git fetch origin
  git checkout $GIT_BRANCH
  git pull origin $GIT_BRANCH

  echo "📥 Installing dependencies"
  pip install -r requirements.txt

  echo "🛠️ Running migrations"
  python manage.py makemigrations --noinput
  python manage.py migrate --noinput

  echo "📦 Collecting static files"
  python manage.py collectstatic --noinput

  echo "♻️ Restarting application"
  touch tmp/restart.txt
  
  echo "✅ Deployment successful!"
EOF

echo "🎉 Production deployment complete!"

How the Script Works

1. Local Preparation Phase

  • Updates requirements.txt with current dependencies

  • Starts development services (like Tailwind) that need to run during deployment

  • Commits and pushes all changes to Git

2. Remote Deployment Phase

  1. Connects to your cPanel server via SSH

  2. Activates the Python virtual environment

  3. Pulls the latest code from Git

  4. Installs/updates Python packages

  5. Runs database migrations

  6. Collects static files

  7. Restarts the application (via Passenger)

Setting Up the Script

1. Save the Script

Create a file named deploy.sh in your project root and paste the script.

2. Make it Executable

bash
Copy
Download
chmod +x deploy.sh

3. Configure for Your Project

Edit the configuration section at the top:

  • Set your domain name

  • Update SSH credentials

  • Verify paths match your server setup

4. Test Run

bash
Copy
Download
./deploy.sh

Advanced Customizations

Adding Pre-Deployment Checks

bash
Copy
Download
# Add before the deployment section
echo "🔍 Running tests"
python manage.py test || { echo "❌ Tests failed"; exit 1; }

Database Backups

Add this to the remote section:

bash
Copy
Download
echo "💾 Backing up database"
python manage.py dumpdata --natural-foreign --indent=2 > db_backup_$(date +'%Y-%m-%d').json

Email Notifications

bash
Copy
Download
# Add at the end of the script
echo "📧 Sending notification"
curl -s --form-string "token=YOUR_TOKEN" \
     --form-string "user=YOUR_USER" \
     --form-string "message=Deployment completed successfully" \
     https://api.pushover.net/1/messages.json

Troubleshooting Common Issues

🔴 SSH Connection Failed

  • Verify your cPanel username and server IP

  • Check that SSH is enabled in cPanel

  • Ensure your SSH key is properly configured

🔴 Python Package Installation Errors

  • Make sure your virtual environment is properly set up

  • Check Python version compatibility

  • Verify requirements.txt is up to date

🔴 Database Migration Problems

  • Check database credentials in settings.py

  • Ensure the database user has proper permissions

  • Look for merge conflicts in migration files

Security Considerations

  1. Never store credentials in the script - Use environment variables or CI/CD secrets

  2. Use SSH keys instead of passwords

  3. Restrict file permissions:

    bash
    Copy
    Download
    chmod 700 deploy.sh
  4. Regularly rotate your deployment keys

Conclusion

This automated deployment solution offers:

✅ One-command deployments
✅ Consistent deployment process
✅ Built-in error checking
✅ Easy customization for your specific needs

By implementing this script, you'll save countless hours and reduce deployment-related stress. The script can be further enhanced to include testing, notifications, and rollback capabilities as your project grows.

Pro Tip: For team projects, integrate this with GitHub Actions or GitLab CI for fully automated deployments on every git push!



Discussion (20)