FastAPI

A modern, high-performance Python web framework for building APIs with type hints.

1. Connect to Your Server

Use SSH to connect to your server:

ssh root@<server-ip>

2. Access Login Credentials

A dedicated user named fastapi has been created to manage your application.

You can find the login credentials in this file: /root/.fastapi-creds

To view them, run:

cat /root/.fastapi-creds

If you wish to change the password, use:

passwd fastapi

3. Switch to the App User

Switch to the fastapi user account to manage your app:

su - flask

This account contains your FastAPI project and environment.

4. Project Structure

Here’s the default directory layout:

/home/fastapi/fastapi_app/
├── main.py          → Your main FastAPI application file
├── venv/            → Virtual environment
└── __pycache__/     → Compiled Python files (auto-generated)

5. Managing Your Application

Navigate to the application directory:

cd ~/fastapi_app

Activate the virtual environment:

source venv/bin/activate

Edit the main app file:

nano main.py

Restart the FastAPI service to apply changes:

sudo systemctl restart uvicorn-fastapi

6. Viewing Logs

You can check logs for Uvicorn and Nginx as follows:

  • Uvicorn logs: /var/log/uvicorn/
  • Nginx error log: /var/log/nginx/error.log
  • Nginx access log: /var/log/nginx/access.log

To view them:

sudo tail -f /var/log/uvicorn/*

7. Accessing Your API

Once the service is running, your FastAPI app is available in your browser at: http://<server-ip>

Nginx automatically forwards HTTP requests to the Uvicorn socket running your FastAPI app.

8. Changing the Domain or Port

If you want to update the server name, port, or domain:

  1. Edit Nginx site config: sudo nano /etc/nginx/sites-available/uvicorn-fastapi
  2. Test configuration: sudo nginx -t
  3. Reload Nginx: sudo systemctl restart nginx

9. Uvicorn Service

The FastAPI app runs via a systemd service named uvicorn-fastapi.

It uses /home/fastapi/fastapi_app/main:app as the application entry point and automatically restarts on failure.

To manage the service:

systemctl status uvicorn-fastapi
systemctl restart uvicorn-fastapi

To edit the service:

sudo vim /etc/systemd/system/uvicorn-fastapi.service
sudo systemctl daemon-reload
sudo systemctl restart uvicorn-fastapi
sudo systemctl status uvicorn-fastapi

10. Enable HTTPS (Optional)

Use Certbot to enable HTTPS.

Application Details