FastAPI Deployment Guide for Internal LAN - Ubuntu 2024

Created: 2024-11-12 23:19:51 | Last updated: 2024-11-13 02:24:15 | Status: Public

This guide walks through deploying a Python 3.10.0 FastAPI application with SQLite on an Ubuntu server for internal LAN access.

Table of Contents

Prerequisites

Ensure you have:
- Ubuntu 22.04 LTS (or newer) 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 Python and required packages
sudo apt install -y python3.10 python3.10-venv python3-pip nginx supervisor sqlite3

# Install development tools (if needed)
sudo apt install -y build-essential python3.10-dev

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.10 -m venv venv
source venv/bin/activate

2. Install Dependencies

# Upgrade pip
pip install --upgrade pip

# 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;
        proxy_buffering off;
    }

    # Optional: Add SSL configuration here if needed
}

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 daemon-reload
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

# Ensure logs directory exists and has correct permissions
sudo mkdir -p /var/log/fastapi
sudo chown www-data:www-data /var/log/fastapi

2. Firewall Configuration

Ubuntu uses UFW (Uncomplicated Firewall) by default:

# Enable UFW if not already enabled
sudo ufw enable

# Allow SSH (important to prevent lockout)
sudo ufw allow ssh

# Allow internal LAN access only
sudo ufw allow from 192.168.0.0/16 to any port 80  # Adjust IP range
sudo ufw allow from 192.168.0.0/16 to any port 443  # If using HTTPS

# Enable firewall
sudo ufw reload

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

# Monitor nginx access logs
sudo tail -f /var/log/nginx/access.log

# Monitor nginx error logs
sudo tail -f /var/log/nginx/error.log

Troubleshooting

Common Issues

  1. Service Won’t Start
   # Check logs for errors
   sudo journalctl -u fastapi -n 50

   # Verify Python path
   which python3.10
  1. Permission Issues
   # Verify ownership
   ls -la /opt/fastapi_app

   # Fix permissions if needed
   sudo chown -R www-data:www-data /opt/fastapi_app
  1. 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
  1. Nginx Issues
   # Test nginx configuration
   sudo nginx -t

   # Check nginx status
   sudo systemctl status nginx

System Resources

# Check system resources
htop  # Install with: sudo apt install htop

# Check disk space
df -h

# Check memory usage
free -m

Need Help?

If you encounter issues:
1. Check the application logs (journalctl)
2. Verify all services are running (systemctl status)
3. Confirm network/firewall settings (ufw status)
4. Ensure all file permissions are correct
5. Check system resources for bottlenecks


Note: This guide assumes a basic Ubuntu installation and may need adjustment based on your specific environment and security requirements. For production deployments, consider adding SSL/TLS encryption, rate limiting, and additional security measures.