๐Ÿš€ Deployment Guide Panel

Complete guide from GitHub to production with Caddy & Cloudflare

1

Push Project to GitHub

๐Ÿ“Œ Goal: Initialize git, create main and dev branches, and push to GitHub

First, let's initialize your project with Git and create the necessary branches:

# Initialize git in your project folder
git init

# Add all your files
git add .

# Commit initial version
git commit -m "Initial commit"

# Create main branch
git branch -M main

# Add your GitHub repository (replace with your URL)
git remote add origin https://github.com/USERNAME/REPO.git

# Push main branch
git push -u origin main

# Create and push dev branch
git checkout -b dev
git push -u origin dev
โœ… You now have both main and dev branches on GitHub!

๐Ÿ’ก Git Cheatsheet

  • git branch List all branches
  • git checkout -b name Create and switch to new branch
  • git push origin branch Push branch to remote
  • git status Check current status
  • git log --oneline View commit history
2

Merge dev โ†’ main

๐Ÿ“Œ Goal: Merge your development branch into main for production

When your dev branch is ready for production, merge it into main:

# Switch to main branch
git checkout main

# Pull latest changes
git pull origin main

# Merge dev branch
git merge dev

# Push to GitHub
git push origin main
โš ๏ธ Best Practice: Always test thoroughly in dev before merging to main

๐Ÿ”„ Merge Strategies

  • git merge --no-ff dev Force create merge commit
  • git merge --squash dev Combine all commits into one
  • git cherry-pick commit-id Apply specific commit
3

Connect to Server via SSH

๐Ÿ“Œ Goal: Establish secure connection to your server

Connect to your server using SSH with the provided credentials:

# Basic SSH connection
ssh -p PORT USER@SERVER_IP

# Example with specific port
ssh -p 2222 [email protected]

# With SSH key (recommended)
ssh -i ~/.ssh/mykey.pem -p 22 [email protected]
๐Ÿ”’ Security Tip: Use SSH keys instead of passwords for better security

๐Ÿ–ฅ๏ธ SSH Commands

  • exit Disconnect from server
  • whoami Check current user
  • pwd Show current directory
  • htop Monitor system resources
  • df -h Check disk usage
4

Clone Project on Server

๐Ÿ“Œ Goal: Set up your project on the server with dependencies

Clone your repository and set up the Python environment:

# Update system and install git
sudo apt update && sudo apt install git python3-pip python3-venv -y

# Clone your project
git clone https://github.com/USERNAME/REPO.git
cd REPO

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Create .env file for secrets
nano .env
๐Ÿ“ In the .env file, add your environment variables:
DATABASE_URL=postgresql://user:pass@localhost/db
API_KEY=your-secret-key
DEBUG=False

๐Ÿ“ฆ Package Management

  • pip list Show installed packages
  • pip freeze > requirements.txt Save dependencies
  • deactivate Exit virtual environment
  • which python Check Python location
5

Test API with Uvicorn

๐Ÿ“Œ Goal: Run and test your FastAPI application

Start your FastAPI application with Uvicorn:

# Make sure venv is activated
source venv/bin/activate

# Run with auto-reload (development)
uvicorn main:app --reload --host 0.0.0.0 --port 8000

# Run in production mode
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4
๐Ÿงช Test your API: Open http://SERVER_IP:8000/docs in your browser

๐Ÿš€ Uvicorn Options

  • --reload Auto-restart on code changes
  • --workers 4 Run with 4 worker processes
  • --log-level info Set logging level
  • --access-log Enable access logging
6

Create Systemd Service

๐Ÿ“Œ Goal: Set up auto-start service for your API

Create a systemd service to keep your API running:

# Create service file
sudo nano /etc/systemd/system/myapi.service

Add this configuration (adjust paths and user as needed):

[Unit]
Description=FastAPI Application
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/REPO
Environment="PATH=/home/ubuntu/REPO/venv/bin"
ExecStart=/home/ubuntu/REPO/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable and start the service:

# Reload systemd
sudo systemctl daemon-reload

# Enable auto-start
sudo systemctl enable myapi

# Start the service
sudo systemctl start myapi

# Check status
sudo systemctl status myapi

๐Ÿ› ๏ธ Service Management

  • sudo systemctl restart myapi Restart service
  • sudo systemctl stop myapi Stop service
  • sudo journalctl -u myapi -f View live logs
  • sudo journalctl -u myapi -n 100 Last 100 log lines
7

Setup Domain with Caddy

๐Ÿ“Œ Goal: Configure HTTPS with Caddy reverse proxy

Configure your domain in Caddy panel (caddy.oanor.com):

  1. ๐Ÿ“ Add new site โ†’ ID: your-service-name
  2. ๐Ÿ”Œ Set Port: 8000 (your API port)
  3. ๐ŸŒ Enter your domain: api.example.com
  4. ๐Ÿ”’ Enable Cloudflare certificate
  5. ๐Ÿ’พ Save and deploy configuration
โš ๏ธ DNS Setup: Make sure your domain points to your server IP in Cloudflare

Alternative: Manual Caddyfile configuration:

api.example.com {
    reverse_proxy localhost:8000
    
    # Optional: Custom headers
    header {
        X-Content-Type-Options nosniff
        X-Frame-Options DENY
        X-XSS-Protection "1; mode=block"
    }
    
    # Enable compression
    encode gzip
}

๐ŸŒ DNS & SSL Tips

  • A Record Point domain to server IP
  • CNAME Alias to another domain
  • Enable Cloudflare proxy for DDoS protection
  • SSL/TLS mode: Full (strict) recommended
8

Final Testing

๐Ÿ“Œ Goal: Verify your deployment is working correctly

Test your live API with HTTPS:

# Test with curl
curl -I https://api.example.com

# Check API docs
curl https://api.example.com/docs

# Test specific endpoint
curl https://api.example.com/health

# Check SSL certificate
openssl s_client -connect api.example.com:443 -servername api.example.com
๐ŸŽ‰ Success! Your API should be live at:
  • ๐Ÿ“– Docs: https://api.example.com/docs
  • ๐Ÿ”ง Redoc: https://api.example.com/redoc
  • ๐Ÿ’ป API: https://api.example.com/

๐Ÿ” Troubleshooting

  • 502 Bad Gateway โ†’ Check if service is running
  • Connection refused โ†’ Check firewall/ports
  • SSL errors โ†’ Verify DNS and Cloudflare settings
  • Check logs: sudo journalctl -u myapi -f

๐Ÿš€ Deployment Complete!

Your application is now live and secured with HTTPS. Monitor logs and set up backups for production use.