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:
-
A Django project ready for deployment
-
cPanel hosting with SSH access
-
Git installed on both local and server
-
Python virtual environment on server
-
Passenger or similar WSGI server configured
The Deployment Script
Here's the complete automation script we'll be using:
#!/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.txtwith current dependencies -
Starts development services (like Tailwind) that need to run during deployment
-
Commits and pushes all changes to Git
2. Remote Deployment Phase
-
Connects to your cPanel server via SSH
-
Activates the Python virtual environment
-
Pulls the latest code from Git
-
Installs/updates Python packages
-
Runs database migrations
-
Collects static files
-
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
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
./deploy.sh
Advanced Customizations
Adding Pre-Deployment Checks
# 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:
echo "💾 Backing up database" python manage.py dumpdata --natural-foreign --indent=2 > db_backup_$(date +'%Y-%m-%d').json
Email Notifications
# 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.txtis 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
-
Never store credentials in the script - Use environment variables or CI/CD secrets
-
Use SSH keys instead of passwords
-
Restrict file permissions:
chmod 700 deploy.sh
-
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 (0)