Jekson
Jekson

Reputation: 3252

Running fastapi app using uvicorn on ubuntu server

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?

Upvotes: 13

Views: 38915

Answers (3)

Nizor
Nizor

Reputation: 11

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

Upvotes: 1

JPG
JPG

Reputation: 88429

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.

Upvotes: 33

Bhargav Tarpara
Bhargav Tarpara

Reputation: 27

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.

Upvotes: -2

Related Questions