๐ Deployment Workflows - Cheat Sheet
๐ Deployment Overview
1. Local Development
2. Version Control
3. Server Setup
4. Application Deployment
5. Service Configuration
6. Testing & Monitoring
๐ป Local Development Workflow
1. Project Setup
# Create project directory
mkdir my-app
cd my-app
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# Install dependencies
pip install flask psycopg2-binary python-dotenv gunicorn
# Create requirements.txt
pip freeze > requirements.txt
2. Environment Configuration
# Create .env file
touch .env
# Add configuration variables
DATABASE_URL=postgresql://user:password@localhost:5432/database
API_KEY=your_api_key_here
DEBUG=True
3. Create .gitignore
venv/
.env
__pycache__/
*.pyc
*.pyo
.DS_Store
*.log
node_modules/
๐ Version Control Workflow
Git Initialization
# Initialize git repository
git init
# Add files
git add .
# Initial commit
git commit -m "Initial commit"
# Add remote repository
git remote add origin https://github.com/username/repo.git
# Push to GitHub
git push -u origin main
Development Cycle
# Check status
git status
# Add changes
git add .
# Commit changes
git commit -m "Description of changes"
# Push to remote
git push origin main
# Pull latest changes
git pull origin main
๐ฅ๏ธ Server Setup Workflow
1. Initial Server Configuration
# Connect to server
ssh root@your-server-ip
# Update system
apt update && apt upgrade -y
# Install essential packages
apt install python3 python3-pip python3-venv git nginx postgresql postgresql-contrib supervisor -y
# Configure firewall
ufw allow ssh
ufw allow http
ufw allow https
ufw enable
2. Setup SSH Deploy Keys
# Generate SSH key for deployment
ssh-keygen -t ed25519 -C "deploy-key" -f ~/.ssh/deploy_key
# View public key
cat ~/.ssh/deploy_key.pub
# Add to GitHub: Settings > Deploy keys > Add deploy key
# Configure SSH
echo "Host github-deploy
HostName github.com
User git
IdentityFile ~/.ssh/deploy_key" >> ~/.ssh/config
๐ฆ Application Deployment Workflow
Complete Deployment Process
- Clone repository to server
- Set up virtual environment
- Install dependencies
- Configure environment variables
- Set up database
- Configure web server
- Set up process management
- Test deployment
1. Clone and Setup Application
# Navigate to web directory
cd /var/www
# Clone repository
git clone git@github-deploy:username/repository.git app
# Change to app directory
cd app
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
2. Configure Environment
# Create production .env file
nano .env
# Add production variables (no sensitive data shown here)
DATABASE_URL=postgresql://user:password@localhost:5432/database
DEBUG=False
SECRET_KEY=production_secret_key
3. Database Setup
# Switch to postgres user
sudo -u postgres psql
# Create database and user
CREATE USER app_user WITH PASSWORD 'secure_password';
CREATE DATABASE app_db OWNER app_user;
GRANT ALL PRIVILEGES ON DATABASE app_db TO app_user;
# Exit and test connection
\q
psql -h localhost -U app_user -d app_db
โ๏ธ Service Configuration
Systemd Service Setup
# Create service file
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
User=www-data
WorkingDirectory=/var/www/app
Environment=PATH=/var/www/app/venv/bin
ExecStart=/var/www/app/venv/bin/gunicorn --bind 127.0.0.1:8000 app:app
Restart=always
[Install]
WantedBy=multi-user.target
# Enable and start service
sudo systemctl daemon-reload
sudo systemctl enable myapp
sudo systemctl start myapp
sudo systemctl status myapp
Nginx Configuration
# Create Nginx site config
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# Enable site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
# Test and restart Nginx
sudo nginx -t
sudo systemctl restart nginx
๐ Update and Maintenance Workflow
Application Updates
# Navigate to app directory
cd /var/www/app
# Pull latest changes
git pull origin main
# Activate virtual environment
source venv/bin/activate
# Update dependencies if needed
pip install -r requirements.txt
# Restart application service
sudo systemctl restart myapp
# Check service status
sudo systemctl status myapp
# Check logs
sudo journalctl -u myapp -f
Database Migrations
# Backup database before changes
sudo -u postgres pg_dump app_db > backup_$(date +%Y%m%d).sql
# Run database migrations/updates
source venv/bin/activate
python manage.py migrate # Django example
# or run your migration scripts
# Test application functionality
curl http://localhost:8000/health
๐งช Testing and Monitoring
Health Checks
# Check all services
sudo systemctl status myapp nginx postgresql
# Check application response
curl http://localhost:8000
curl http://your-domain.com
# Check logs
sudo journalctl -u myapp -n 50
sudo tail -f /var/log/nginx/error.log
# Check resource usage
htop
df -h
free -h
Monitoring Setup
# Set up log rotation
sudo nano /etc/logrotate.d/myapp
# Basic monitoring script
#!/bin/bash
if ! systemctl is-active --quiet myapp; then
echo "App is down, restarting..."
sudo systemctl restart myapp
fi
# Add to crontab
crontab -e
# Add: */5 * * * * /path/to/monitor.sh
๐ Security Best Practices
Security Checklist
- Use strong passwords for database users
- Configure firewall properly
- Set up SSL certificates
- Regular security updates
- Backup data regularly
- Monitor access logs
- Use environment variables for secrets
- Limit database access to localhost
SSL Certificate Setup (Let's Encrypt)
# Install Certbot
sudo apt install certbot python3-certbot-nginx -y
# Get certificate
sudo certbot --nginx -d your-domain.com
# Test auto-renewal
sudo certbot renew --dry-run
# Set up auto-renewal
sudo crontab -e
# Add: 0 12 * * * /usr/bin/certbot renew --quiet
๐ Deployment Checklist
Pre-Deployment
- Code tested locally
- Dependencies updated
- Environment variables configured
- Database migrations ready
- Backup created
During Deployment
- Pull latest code
- Update dependencies
- Run database migrations
- Restart services
- Test functionality
Post-Deployment
- Check service status
- Monitor logs
- Test critical functionality
- Update documentation
- Notify team of deployment