This repository contains a FastAPI backend that serves a static frontend and provides the foundation for deploying the application on a Google Cloud VM using Gunicorn and NGINX for production.
back/python/serve.py # FastAPI application
back/python/venv/ # Python virtual environment
front/ # Frontend (HTML, CSS, JS)
- FastAPI backend
- Serves
index.html
and static files (/static
,/resources
,/src
) - Production-ready deployment with Gunicorn and NGINX
- Systemd service for automatic startup on boot
- Ready for HTTPS with Let's Encrypt (SSL encryption)
cd back/python
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn
uvicorn serve:app --reload --host 0.0.0.0 --port 8000
http://localhost:8000/
sudo apt update
sudo apt install python3 python3-venv python3-pip nginx certbot python3-certbot-nginx
cd /path/to/back/python
python3 -m venv venv
source venv/bin/activate
pip install fastapi gunicorn uvicorn
/etc/systemd/system/fastapi.service
[Unit]
Description=FastAPI Gunicorn Service
After=network.target
[Service]
User=fastapiuser
WorkingDirectory=/path/to/back/python
ExecStart=/path/to/back/python/venv/bin/gunicorn -w 4 -k uvicorn.workers.UvicornWorker serve:app --bind 127.0.0.1:8000
Restart=always
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl start fastapi.service
sudo systemctl enable fastapi.service
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;
}
}
Reload NGINX:
sudo systemctl reload nginx
sudo certbot --nginx -d your-domain.com
- Certbot will automatically configure SSL in NGINX
- You can test auto-renewal with:
sudo certbot renew --dry-run
https://your-domain.com
- Use
fastapiuser
with limited permissions - Protect port
8000
with GCP firewall rules - Enable HTTPS with Let's Encrypt
- Validate API inputs with Pydantic
- Keep system and Python dependencies updated
- Add more API endpoints
- Add frontend framework support (React/Vue)
The code provided here is shared in the spirit of free education. Please do not use this code directly for your coursework or professional projects. Instead, let these implementations serve as inspiration to develop your own solutions.
If you have any questions, suggestions, or feedback, feel free to contact me.
© 2025 by Amir Nourinia