Running fastapi app using uvicorn on ubuntu server
Asked Answered
A

3

13

I am dealing with the project deposition made on FastAPI to a remote ubuntu server. I'll try to run the project from terminal (using SSH connection) by the command

gunicorn -k uvicorn.workers.UvicornWorker main:app

The output is

gunicorn -k uvicorn.workers.UvicornWorker main:app
[2020-07-14 15:24:28 +0000] [23102] [INFO] Starting gunicorn 20.0.4
[2020-07-14 15:24:28 +0000] [23102] [INFO] Listening at: http://127.0.0.1:8000 (23102)
[2020-07-14 15:24:28 +0000] [23102] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2020-07-14 15:24:28 +0000] [23104] [INFO] Booting worker with pid: 23104
[2020-07-14 15:24:28 +0000] [23104] [INFO] Started server process [23104]
[2020-07-14 15:24:28 +0000] [23104] [INFO] Waiting for application startup.
[2020-07-14 15:24:28 +0000] [23104] [INFO] Application startup complete.

But I need the project to be available at the IP address of the server. If I try smth like

uvicorn main:app --host 66.226.247.55 --port 8000 

I get

INFO:     Started server process [23308]
INFO:     Waiting for application startup.
INFO:     Connected to database postgresql://recognition:********@localhost:5432/reco
INFO:     Application startup complete.
ERROR:    [Errno 99] error while attempting to bind on address ('66.226.247.55', 8000): cannot assign requested address
INFO:     Waiting for application shutdown.
INFO:     Disconnected from database postgresql://recognition:********@localhost:5432/reco
INFO:     Application shutdown complete.

Where 66.226.247.55 - external IP adress from google cloud platform instances How do I start a project so that it can be accessed via IP?

Algonkian answered 14/7, 2020 at 15:44 Comment(1)
This problem should apply on virtual server such as Digital Ocean, Vultr and Linode. The proposed solution works well.Mcduffie
P
31

The --host should be the local address of your GCP server.

uvicorn main:app --host 0.0.0.0 --port 8000

and now access the application by http://66.226.247.55:8000

Note: You should open your 8000 port of GCP server.

Pfeiffer answered 14/7, 2020 at 17:31 Comment(6)
There's no mistake now. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) But I still can't get a response when I contact the IP address. Is there any other nuance?Algonkian
Probably you didn't open your port 8000 of your GCP yet.Pfeiffer
Allowed port 8000 using ufw sudo ufw allow 8000Algonkian
I am not a pro in GCP or DevOps, But, you can try running something else in your GCP (say python HTTP server) and can check whether it is publically available or not.Pfeiffer
Thank you, I'll try to do the same on digitalocean. Maybe there's a problem with GCP.Algonkian
Thanks.. This works well on Vultr VPS as expected.Mcduffie
C
1

If you're using nginx server

create a file in /etc/nginx/sites-enabled/ create file touch fastapi_nginx copy code into file and adjust accordingly

server{
    listen 80;
    server_name "your public ip";
    location / {
        proxy_pass http://127.0.0.1:8000; #localhost
    }

}

This should reroute to your public ip

Coontie answered 18/5, 2022 at 12:5 Comment(0)
E
-2

You cannot launch your fast api app on your local to your remote server in GCP.

You must deploy your app to GCP. In other words you need to run that command on a remote server not your localhost.

Eohippus answered 14/7, 2020 at 15:59 Comment(1)
Of course, I don't run the command from my local machine. My project is cloned to GCP and I run the command from the GC console.Algonkian

© 2022 - 2024 — McMap. All rights reserved.