Giter Club home page Giter Club logo

practice's Introduction

Deploying FastAPI with Nginx, Uvicorn, Gunicorn, and PM2 on Ubuntu

Table of Contents

  • Prerequisites: #prerequisites
  • Installation: #installation
  • Setting Up FastAPI Application: #setting-up-fastapi-application
  • Running FastAPI with Gunicorn: #running-fastapi-with-gunicorn
  • Managing with PM2: #managing-with-pm2
  • Configuring Nginx as a Reverse Proxy: #configuring-nginx-as-a-reverse-proxy
  • Testing the Deployment: #testing-the-deployment
  • Additional Notes: #additional-notes

Prerequisites

  • An Ubuntu server with SSH access
  • Python 3.6 or newer installed
  • A non-root user with sudo privileges

Installation

  1. Update Package Lists:

    sudo apt update
  2. Install Python Dependencies:

    sudo apt install python3-pip python3-venv nginx
  3. Create a Virtual Environment (Recommended):

    python3 -m venv venv
    source venv/bin/activate
  4. Install FastAPI, Uvicorn, and Gunicorn:

    pip install fastapi uvicorn[standard] gunicorn
  5. Install PM2 (Process Manager):

    sudo npm install -g pm2

Setting Up FastAPI Application

  1. Create a Project Directory:

    mkdir my_fastapi_app
    cd my_fastapi_app
  2. Create a Main File (main.py):

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    def root():
        return {"message": "Hello World!"}

Running FastAPI with Gunicorn

  1. Start the Application:

    gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

Managing with PM2

  1. Start with PM2:

    pm2 start "gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app" --name my_fastapi_app
  2. Manage PM2 Processes:

    • List processes: pm2 list
    • Stop a process: pm2 stop my_fastapi_app
    • Restart a process: pm2 restart my_fastapi_app

Configuring Nginx as a Reverse Proxy

  1. Create Nginx Configuration File:

    sudo nano /etc/nginx/sites-available/my_fastapi_app.conf
  2. Paste the Following Configuration:

    server {
        listen 80;
        server_name your_domain_or_IP;
    
        location / {
            proxy_pass http://localhost:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
    }
  3. Enable the Configuration:

    sudo ln -s /etc/nginx/sites-available/my_fastapi_app.conf /etc/nginx/sites-enabled/
  4. Test Nginx Configuration:

    sudo nginx -t
  5. Restart Nginx:

    sudo service nginx restart

Testing the Deployment

  1. Access your server's IP address or domain name in a web browser. You should see the "Hello World!" message from your FastAPI app.

**##

practice's People

Contributors

ibrahim7787 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.