How can I fix FastAPI application error on Apache WSGI?
Asked Answered
B

1

6
I am trying to run FastAPI application on Apache running server. I have configured Apache virtual host file accordingly
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName fastapi.example.com
        ServerAlias fastapi.example.com
        DocumentRoot /var/www/fastapi
        ErrorLog ${APACHE_LOG_DIR}/fastapi_error.log
        CustomLog ${APACHE_LOG_DIR}/fastapi_access.log combined
        WSGIScriptAlias / /var/www/fastapi/main.wsgi
        <Directory "/var/www/fastapi">
                AllowOverride All
        </Directory>
</VirtualHost>

and created main.wsgi and main.py files.

main.wsgi

#! /usr/bin/python3.7

import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/var/www/fastapi/')
from main import app as application
application.secret_key = 'alibaba'

main.py

from typing import Optional
from fastapi import FastAPI
app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

When i am trying to access the web, I got the 500 Internal server Error with the following log in fastapi_access.log

mod_wsgi (pid=24946): Exception occurred processing WSGI script '/var/www/fastapi/main.wsgi'.
TypeError: __call__() missing 1 required positional argument: 'send'

Could you please advice, how can I fix this problem and what am I doing wrong?

Thanks in advance.
Bemean answered 26/7, 2020 at 12:0 Comment(0)
G
7

WSGI servers is not compatible with FastAPI, FastAPI only runs in ASGI server, gunicorn and all the other WSGI servers just implements PEP Standards with ASGI workers in depth they still work as ASGI with the workers.

Gauleiter answered 26/7, 2020 at 12:43 Comment(2)
So, is there any solution to run uvicorn with virtualhosts?Bemean
Does this helps? Routing for Hosts and SubdomainsGauleiter

© 2022 - 2024 — McMap. All rights reserved.