Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Join theFastAPI Cloud waiting list 🚀
Follow@fastapi onX (Twitter) to stay updated
FollowFastAPI onLinkedIn to stay updated
Subscribe to theFastAPI and friends newsletter 🎉
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor
sponsor

Run a Server Manually

Use thefastapi run Command

In short, usefastapi run to serve your FastAPI application:

$<fontcolor="#4E9A06">fastapi</font>run<ustyle="text-decoration-style:solid">main.py</u>  <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span>  Starting production server 🚀             Searching for package file structure from directories             with <font color="#3465A4">__init__.py</font> files             Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>   <span style="background-color:#007166"><font color="#D3D7CF"> module </font></span>  🐍 main.py     <span style="background-color:#007166"><font color="#D3D7CF"> code </font></span>  Importing the FastAPI app object from the module with             the following code:             <u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u>      <span style="background-color:#007166"><font color="#D3D7CF"> app </font></span>  Using import string: <font color="#3465A4">main:app</font>   <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span>  Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font>   <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span>  Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000/docs</u></font>             Logs:     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Started server process <b>[</b><font color="#34E2E2"><b>2306215</b></font><b>]</b>     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Waiting for application startup.     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Application startup complete.     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font> <b>(</b>Press CTRL+C             to quit<b>)</b>

That would work for most of the cases. 😎

You could use that command for example to start yourFastAPI app in a container, in a server, etc.

ASGI Servers

Let's go a little deeper into the details.

FastAPI uses a standard for building Python web frameworks and servers calledASGI. FastAPI is an ASGI web framework.

The main thing you need to run aFastAPI application (or any other ASGI application) in a remote server machine is an ASGI server program likeUvicorn, this is the one that comes by default in thefastapi command.

There are several alternatives, including:

  • Uvicorn: a high performance ASGI server.
  • Hypercorn: an ASGI server compatible with HTTP/2 and Trio among other features.
  • Daphne: the ASGI server built for Django Channels.
  • Granian: A Rust HTTP server for Python applications.
  • NGINX Unit: NGINX Unit is a lightweight and versatile web application runtime.

Server Machine and Server Program

There's a small detail about names to keep in mind. 💡

The word "server" is commonly used to refer to both the remote/cloud computer (the physical or virtual machine) and also the program that is running on that machine (e.g. Uvicorn).

Just keep in mind that when you read "server" in general, it could refer to one of those two things.

When referring to the remote machine, it's common to call itserver, but alsomachine,VM (virtual machine),node. Those all refer to some type of remote machine, normally running Linux, where you run programs.

Install the Server Program

When you install FastAPI, it comes with a production server, Uvicorn, and you can start it with thefastapi run command.

But you can also install an ASGI server manually.

Make sure you create avirtual environment, activate it, and then you can install the server application.

For example, to install Uvicorn:

$pipinstall"uvicorn[standard]"---> 100%

A similar process would apply to any other ASGI server program.

Tip

By adding thestandard, Uvicorn will install and use some recommended extra dependencies.

That includinguvloop, the high-performance drop-in replacement forasyncio, that provides the big concurrency performance boost.

When you install FastAPI with something likepip install "fastapi[standard]" you already getuvicorn[standard] as well.

Run the Server Program

If you installed an ASGI server manually, you would normally need to pass an import string in a special format for it to import your FastAPI application:

$uvicornmain:app--host0.0.0.0--port80<span style="color: green;">INFO</span>:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)

Note

The commanduvicorn main:app refers to:

  • main: the filemain.py (the Python "module").
  • app: the object created inside ofmain.py with the lineapp = FastAPI().

It is equivalent to:

frommainimportapp

Each alternative ASGI server program would have a similar command, you can read more in their respective documentation.

Warning

Uvicorn and other servers support a--reload option that is useful during development.

The--reload option consumes much more resources, is more unstable, etc.

It helps a lot duringdevelopment, but youshouldn't use it inproduction.

Deployment Concepts

These examples run the server program (e.g Uvicorn), startinga single process, listening on all the IPs (0.0.0.0) on a predefined port (e.g.80).

This is the basic idea. But you will probably want to take care of some additional things, like:

  • Security - HTTPS
  • Running on startup
  • Restarts
  • Replication (the number of processes running)
  • Memory
  • Previous steps before starting

I'll tell you more about each of these concepts, how to think about them, and some concrete examples with strategies to handle them in the next chapters. 🚀


[8]ページ先頭

©2009-2026 Movatter.jp