Skip to content

Instantly share code, notes, and snippets.

@marobo
Last active September 13, 2025 04:12
Show Gist options
  • Save marobo/e2f7789e3e83a4bcb23c10590bc393ef to your computer and use it in GitHub Desktop.
Save marobo/e2f7789e3e83a4bcb23c10590bc393ef to your computer and use it in GitHub Desktop.
deploy_pythonanywhere.sh
#!/bin/bash
# Steps:
# 1. Go to https://www.pythonanywhere.com/registration/register/beginner/ and sign up for a free account
# 2. Go to your email and verify your email address
# 3. Login to your PythonAnywhere account
# 4. Go to the "Files" tab and upload this file (deploy_pythonanywhere.sh)
# 5. Go to the "Consoles" tab and create a new console by clicking on the "Bash" button
# 6. Open the "deploy_pythonanywhere.sh" file and edit the variables with your own values
# 7. Make the "deploy_pythonanywhere.sh" file executable by running "chmod +x deploy_pythonanywhere.sh"
# 8. Execute the "deploy_pythonanywhere.sh" file by running "source ~/deploy_pythonanywhere.sh" in the console
# === CONFIGURATION ===
GIT_REPO="[git repository URL]" # [email protected]:username/repository.git
GIT_BRANCH="[git branch name]" # master, main, etc.
GIT_USERNAME="[git username]" # username
PROJECT_FOLDER="[project folder name]" # Folder name after cloning
PROJECT_NAME="[project name]" # Django project folder containing settings.py
PYTHON_VERSION="[python version]" # 3.10, 3.11, etc.
PA_USERNAME="[pythonanywhere username]" # PythonAnywhere username
PA_EMAIL="[pythonanywhere email]" # PythonAnywhere email
DOMAIN="[PA_USERNAME.pythonanywhere.com]" # username.pythonanywhere.com
# === DEPLOY SCRIPT ===
echo "================================================"
echo "πŸš€ Starting deployment..."
echo "================================================"
echo "This script will:"
echo " - Navigating to home directory"
echo " - Generate SSH key if not present"
echo " - Cloning the Git repo"
echo " - Checking out to the branch specify in the script"
echo " - Pulling latest changes from Git"
echo " - Creating virtual environment and then activating it"
echo " - Installing requirements"
echo " - Running migrations"
echo " - Collecting static files"
echo " - Creating superuser account"
echo " - Configure WSGI file"
echo " - Reload web app"
echo "================================================"
echo "πŸ“ Navigating to home directory..."
cd ~ || exit
# === Generate SSH key if not present ===
if [ ! -f ~/.ssh/id_rsa.pub ]; then
ssh-keygen -t rsa -b 4096 -C "$PA_USERNAME@$DOMAIN" -N "" -f ~/.ssh/id_rsa
echo "πŸ”‘ Public key generated:"
echo "================================================"
cat ~/.ssh/id_rsa.pub
echo "================================================"
echo "πŸ”‘ Copy and add public key above to your GitHub account at https://github.com/${GIT_USERNAME}/${PROJECT_NAME}/settings/keys/new"
read -p "After adding the public key to GitHub, press ENTER to continue..."
else
echo "πŸ”‘ SSH has been generated."
echo "================================================"
cat ~/.ssh/id_rsa.pub
echo "================================================"
echo "πŸ”‘ Copy and add public key above to your GitHub account at https://github.com/${GIT_USERNAME}/${PROJECT_NAME}/settings/keys/new"
echo "πŸ”‘ If it has been added to your GitHub repository, you can skip the next step."
read -p "Press ENTER to continue..."
fi
if [ ! -d "$PROJECT_FOLDER" ]; then
echo "πŸ” Cloning the Git repo..."
git clone "$GIT_REPO"
cd "$PROJECT_FOLDER" || exit
echo "πŸ“₯ Checking out branch $GIT_BRANCH..."
git checkout $GIT_BRANCH
else
cd "$PROJECT_FOLDER" || exit
echo "πŸ“₯ Checking out branch $GIT_BRANCH..."
git checkout $GIT_BRANCH
echo "πŸ“₯ Pulling latest changes from Git..."
git pull origin $GIT_BRANCH
fi
# === Create and activate virtual environment outside of project folder ===
echo "🐍 Navigating out of the project folder..."
cd ..
if [ ! -d "venv" ]; then
echo "🐍 Creating virtual environment..."
python$PYTHON_VERSION -m venv venv
fi
echo "βœ… Activating virtual environment..."
source ~/venv/bin/activate
# === Install requirements and run migrations ===
cd "$PROJECT_FOLDER" || exit
if [ -f "requirements.txt" ]; then
echo "πŸ“¦ Installing requirements..."
pip install --upgrade pip
pip install -r requirements.txt
fi
echo "🧱 Running migrations..."
python manage.py migrate
echo "πŸ–ΌοΈ Collecting static files..."
python manage.py collectstatic --noinput
# === Create superuser ===
echo "πŸ‘€ Creating superuser account..."
echo "You'll be prompted to create a superuser account. You can skip this by pressing Ctrl+C."
python manage.py createsuperuser --username=$PA_USERNAME --email=$PA_EMAIL || echo "Skipped superuser creation"
echo "βœ… Superuser created!"
# === Configure WSGI file ===
WSGI_FILE="/var/www/${PA_USERNAME}_pythonanywhere_com_wsgi.py"
echo "βš™οΈ Updating WSGI file at $WSGI_FILE..."
cat > "$WSGI_FILE" <<EOL
import sys
import os
path = '/home/$PA_USERNAME/$PROJECT_FOLDER'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = '$PROJECT_NAME.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
EOL
# === Reload web app ===
echo "πŸ”„ Reloading web app..."
touch /var/www/${PA_USERNAME}_pythonanywhere_com_wsgi.py
echo "βœ… Deployment complete!"
echo "================================================"
echo "πŸ”— NEXT STEPS:"
echo "1. Go to https://www.pythonanywhere.com/user/$PA_USERNAME/webapps/#tab_id_new_webapp_tab"
echo "2. Tap on the 'Add a new web app' button and select 'Manual configuration'"
echo "3. Select Python $PYTHON_VERSION"
echo "4. Set the source code directory to: /home/$PA_USERNAME/$PROJECT_FOLDER"
echo "5. Set the working directory to: /home/$PA_USERNAME/$PROJECT_FOLDER"
echo "6. Set the virtual environment to: /home/$PA_USERNAME/venv"
echo "7. Set the static files mapping to: URL: /static/ Directory: /home/$PA_USERNAME/$PROJECT_FOLDER/staticfiles"
echo "8. Set the media files mapping to: URL: /media/ Directory: /home/$PA_USERNAME/$PROJECT_FOLDER/media"
echo "9. Reload your web app by clicking the green 'Reload' button"
echo "================================================"
echo "πŸŽ‰ Your Django app should now be live at: https://$DOMAIN"
@marobo
Copy link
Author

marobo commented Sep 12, 2025

Updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment