dylanjcastillo / blog_comments

dylancastillo.co comments
1 stars 0 forks source link

posts/fastapi-nginx-gunicorn/ #12

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

How to Securely Deploy a FastAPI app with NGINX and Gunicorn

In this tutorial, you'll learn how to use NGINX, and Gunicorn+Uvicorn to deploy a FastAPI app, and generate a free SSL certificate for it.

https://dylancastillo.co/fastapi-nginx-gunicorn/

landolabrum commented 1 year ago

Very good documentation, I appreciate it! Just in case anyone was wondering,   $ sudo supervisorctl restart fastapi-app  will RESTART the app after you edit code.

airanirenuka commented 1 year ago

how to reload when using gunicorn with Uvicorn process

dylanjcastillo commented 1 year ago

@airanirenuka, you can use sudo supervisorctl restart fastapi-app.

brealisty commented 1 year ago

can gunicorn load balancing with multiple gpus? any advices for delopying a python server(load several deep models) with multiple gpus?

dylanjcastillo commented 1 year ago

Sorry, but I haven't done that myself. For more complex apps, I separate the app and inference logic into different services, and use something like torchserve

Muntasir2001 commented 1 year ago

Hi

I was wondering whether this would work with APIKeys (access_tokens in header)?

dylanjcastillo commented 1 year ago

@Muntasir2001 I don't see why not!

alrexo commented 12 months ago

There is one important thing missing: you should disable the default nginx site, otherwise it won't work (at least not with a domain name in my case):

sudo rm /etc/nginx/sites-enabled/default
sudo systemctl reload nginx
henxing commented 11 months ago

Nice guide! Before getting all the way to the bottom of it and noticing that you have this all in a repo, I used it to create a project that includes an install script for ease of use. It assumes you've installed the prerequisites on your own, but then the rest of the install is free from interaction. Feel free to fork it if you find it useful, I linked to an archived copy of this page in the install script to give you credit. Please let me know if you would like me to change the attribution in any way.

stephen-bidchat commented 11 months ago

How to attach a custom SSL certificate from Namecheap? please advice.

rinigarg15 commented 11 months ago

$sudo supervisorctl restart fastapi-app is not picking up my code changes. can anyone help?

vittxr commented 10 months ago

is possible use gunicorn from system, instead from venv? I want to use uvicorn and the other deps from venv, but gunicorn i would use for other apis and the docs recommend installing it globally, because would be easier to config multiples applications, so i wanna try to do something like:

NAME=installers_tickets_backend
DIR=/home/backends/installers_tickets
USER=root
GROUP=root
WORKERS=1
WORKER_CLASS=uvicorn.workers.UvicornWorker
VENV=$DIR/venv/bin/activate
BIND=unix:$DIR/run/gunicorn.sock
LOG_LEVEL=error

cd $DIR
source $VENV

/usr/bin/gunicorn main:app \ # -> '/usr/bin/gunicorn' is the path for system gunicorn
  --name $NAME \
  --workers $WORKERS \
  --worker-class $WORKER_CLASS \
  --user=$USER \
  --group=$GROUP \
  --bind=$BIND \
  --log-level=$LOG_LEVEL \
  --log-file=-
dylanjcastillo commented 10 months ago

@vittxr yes that should work

vittxr commented 10 months ago

@dylancastilo but doesn't work, because i was getting "modules not found" - somehow, when i try to use absolute path, supervisor ignores venv dependencies and try to find the modules globally instead (idk)... i give up and install gunicorn inside venv xD (after wasting some hours on it). I am using debian 12 by the way

MarkusvonStaden commented 9 months ago

I'm trying to get websockets to work, but I just don't get it. Anyone knows, how to do that?

sagardspeed2 commented 6 months ago

Hey, thanks for this awesome documentation. I spent almost 4-5 hours while solving the issue of deployment of FastAPI with nginx and I was fetching the error. but this docs save my time.

Thanks again

aatish-shinde commented 4 months ago

Hi, Can someone help? I don't see any output. no errors in log file either. Something went wrong after step5(nginx). It was working before that.

aatish-shinde commented 4 months ago

so the curl command - curl --unix-socket /home/fastapi-user/fastapi-nginx-gunicorn/run/gunicorn.sock localhost

only runs in venv. If I open a new terminal and run the above command it does not work. Any idea?

aatish-shinde commented 4 months ago

sorry 1 more update, I can see the output using - curl http://localhost, but not the IP in the browser?

tarunsk1998 commented 4 months ago

@dylanjcastillo How can I configure this setup in such a manor that the supervisor runs my gunicorn workers only if my FastAPI receives traffic. And remove the workers from memory if the FastAPI is idle for say like 120 seconds. I need this for reusing my GPU memory. The workers capture my GPU and are never clearing the memory, causing trouble for my other processes as they are unable to allocate GPU memory.

So, I want the gunicorn to be running only when the request for my fastAPI is hit and if not the fastapi should not be in memory.