Push Project 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
๐ก Git Cheatsheet
git branchList all branchesgit checkout -b nameCreate and switch to new branchgit push origin branchPush branch to remotegit statusCheck current statusgit log --onelineView commit history
Merge dev โ main
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
๐ Merge Strategies
git merge --no-ff devForce create merge commitgit merge --squash devCombine all commits into onegit cherry-pick commit-idApply specific commit
Connect to Server via SSH
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]
๐ฅ๏ธ SSH Commands
exitDisconnect from serverwhoamiCheck current userpwdShow current directoryhtopMonitor system resourcesdf -hCheck disk usage
Clone Project on Server
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
DATABASE_URL=postgresql://user:pass@localhost/db API_KEY=your-secret-key DEBUG=False
๐ฆ Package Management
pip listShow installed packagespip freeze > requirements.txtSave dependenciesdeactivateExit virtual environmentwhich pythonCheck Python location
Test API with Uvicorn
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
http://SERVER_IP:8000/docs in your browser
๐ Uvicorn Options
--reloadAuto-restart on code changes--workers 4Run with 4 worker processes--log-level infoSet logging level--access-logEnable access logging
Create Systemd Service
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 myapiRestart servicesudo systemctl stop myapiStop servicesudo journalctl -u myapi -fView live logssudo journalctl -u myapi -n 100Last 100 log lines
Setup Domain with Caddy
Configure your domain in Caddy panel (caddy.oanor.com):
- ๐ Add new site โ ID: your-service-name
- ๐ Set Port: 8000 (your API port)
- ๐ Enter your domain:
api.example.com - ๐ Enable Cloudflare certificate
- ๐พ Save and deploy configuration
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 RecordPoint domain to server IPCNAMEAlias to another domain- Enable Cloudflare proxy for DDoS protection
- SSL/TLS mode: Full (strict) recommended
Final Testing
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
- ๐ 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.