Giter Club home page Giter Club logo

fastapideployapach2's Introduction

Deploying FastAPI on Unix Server with Apache2

This guide outlines the steps required to deploy a FastAPI application on a Unix server using Apache2 as the reverse proxy. FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+.

Prerequisites

  • Unix server (Ubuntu, CentOS, etc.)
  • Apache2 installed and configured
  • Python 3.6+ installed
  • Let's Encrypt SSL certificate (optional but recommended)

Step 1: Creating a Systemd Service for FastAPI

Create a systemd service to manage the FastAPI application. This allows the application to start automatically on system boot and restart in case of failure.

[Unit]
Description=Uvicorn instance to serve FastAPI app
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/application
EnvironmentFile=/path/to/venv/bin/activate
ExecStart=/path/to/venv/bin/uvicorn run_server:app --host 0.0.0.0 --port 8000 --proxy-headers
Restart=always

[Install]
WantedBy=multi-user.target

Make sure to replace /path/to/application with the directory containing your FastAPI application and adjust other paths accordingly. To enable and start the service, run the following commands:

sudo systemctl enable your-service-name.service
sudo systemctl start your-service-name.service

Step 2: Configuring Apache2

HTTP Configuration

Create a virtual host configuration file for HTTP (port 80) to redirect traffic to HTTPS.

<VirtualHost *:80>
    ServerName api.test.com

    # Redirect HTTP to HTTPS
    Redirect permanent / https://api.test.com/
</VirtualHost>

HTTPS Configuration

Create a virtual host configuration file for HTTPS (port 443) to serve the FastAPI application.

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName api.test.com

    SSLCertificateFile /etc/letsencrypt/live/api.test.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/api.test.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:8000/
    ProxyPassReverse / http://127.0.0.1:8000/
    
    # Headers for reverse proxy
    RequestHeader set X-Forwarded-Proto "https"

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
</IfModule>

Ensure to replace api.test.com with your domain name and update the SSL certificate paths accordingly. To enable the Apache virtual host configurations, run:

sudo a2ensite your-http-config.conf
sudo a2ensite your-https-config.conf
sudo systemctl reload apache2

Step 3: Directory Permissions

Grant Apache2 access to the directory containing your FastAPI application.

sudo chown -R www-data:www-data /path/to/application

Conclusion

You have successfully deployed your FastAPI application on a Unix server with Apache2 as the reverse proxy. Ensure to restart Apache2 for the changes to take effect.

For any issues or further customization, refer to the official documentation of FastAPI and Apache2.

fastapideployapach2's People

Contributors

ceo-py avatar

Stargazers

 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.