FastAPI Deployment Guide for Internal LAN - Debian 12
Created: 2024-11-12 23:09:38 | Last updated: 2024-11-12 23:09:38 | Status: Public
This guide walks through deploying a Python 3.10.0 FastAPI application with SQLite on a Debian 12 server for internal LAN access.
Table of Contents
- Prerequisites
- System Setup
- Project Configuration
- Server Configuration
- Security
- Maintenance
- Troubleshooting
Prerequisites
Ensure you have:
- Debian 12 server with sudo access
- Python 3.10.0 FastAPI application
- SQLite database
- Internal LAN network
System Setup
First, update your system and install required packages:
# Update system packages
sudo apt update && sudo apt upgrade -y
# Install required packages
sudo apt install -y python3-pip python3-venv nginx supervisor sqlite3
Project Configuration
1. Set Up Project Directory
# Create project directory
sudo mkdir -p /opt/fastapi_app
sudo chown $(whoami):$(whoami) /opt/fastapi_app
# Create and activate virtual environment
cd /opt/fastapi_app
python3 -m venv venv
source venv/bin/activate
2. Install Dependencies
# Install Python packages
pip install fastapi uvicorn sqlalchemy databases[sqlite] python-multipart
# Install any additional project dependencies
# pip install -r requirements.txt
Server Configuration
1. Create SystemD Service
Create /etc/systemd/system/fastapi.service
:
[Unit]
Description=FastAPI application
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/fastapi_app
Environment="PATH=/opt/fastapi_app/venv/bin"
ExecStart=/opt/fastapi_app/venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000
[Install]
WantedBy=multi-user.target
2. Configure Nginx
Create /etc/nginx/sites-available/fastapi
:
server {
listen 80;
server_name _; # Matches any server name
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;
}
}
3. Enable Configuration
# Set up Nginx
sudo ln -s /etc/nginx/sites-available/fastapi /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl restart nginx
# Enable and start FastAPI service
sudo systemctl enable fastapi
sudo systemctl start fastapi
Security
1. File Permissions
# Set ownership and permissions
sudo chown -R www-data:www-data /opt/fastapi_app
sudo chmod 600 /opt/fastapi_app/your_database.db
2. Firewall Configuration
# Install and configure UFW
sudo apt install -y ufw
sudo ufw allow from 192.168.0.0/16 to any port 80 # Adjust IP range
sudo ufw enable
Maintenance
Application Updates
cd /opt/fastapi_app
source venv/bin/activate
# Update application files
# git pull # If using git
# Update dependencies if needed
pip install -r requirements.txt
# Restart service
sudo systemctl restart fastapi
Monitoring
# Check service status
sudo systemctl status fastapi
# View logs
sudo journalctl -u fastapi -f
Troubleshooting
Common Issues
- Service Won’t Start
# Check logs for errors
sudo journalctl -u fastapi -n 50
- Permission Issues
# Verify ownership
ls -la /opt/fastapi_app
# Fix permissions if needed
sudo chown -R www-data:www-data /opt/fastapi_app
- Database Access Problems
# Check database permissions
ls -la /opt/fastapi_app/your_database.db
# Fix if needed
sudo chown www-data:www-data /opt/fastapi_app/your_database.db
sudo chmod 600 /opt/fastapi_app/your_database.db
Need Help?
If you encounter issues:
1. Check the application logs
2. Verify all services are running
3. Confirm network/firewall settings
4. Ensure all file permissions are correct
Note: This guide assumes a basic Debian 12 installation and may need adjustment based on your specific environment and security requirements.