6

I am trying to run FastAPI as a windows service.Couldn't find any documentation or any article to run Uvicorn as a Window's service. I tried using NSSM as well but my windows service stops.

Ronin
  • 63
  • 1
  • 1
  • 3
  • 1
    Welcome to StackOverflow. Others may be able to help you better if you can include example code of what you are trying to accomplish and what you expect the result to be. See stackoverflow.com/help/how-to-ask for additional guidance. – zhangxaochen Jan 06 '21 at 08:03

2 Answers2

14

I managed to run FastAPI with uvicorn as a Windows Service using NSSM.

I had to deploy uvicorn programatically, basically run uvicorn directly from your Python script, then create and install a custom service with NSSM.

Here's a small example based on FastAPI's example, but instead of running it with uvicorn main:app --reload from the command line, you add uvicorn.run(app, **config) with your own config.

from fastapi import FastAPI
import uvicorn

app = FastAPI()


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

if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info")

Then you can install it with NSSM using the standard nssm install command

nssm.exe install "FastAPIWindowsService" "C:\Scripts\FastAPIWindowsService\venv\Scripts\python.exe" "C:\Scripts\FastAPIWindowsService\src\main.py"

Change your service name, the path of your python.exe, and the path of your script accordingly. Your service should appear in the Windows Services Manager when installed.

Hope this helps, and works for you!

perev
  • 172
  • 1
  • 3
  • 1
    On start of the service I got this error pup-up message: "Windows could not start the 'servicename' on Local Computer. For more info, review the System Event Log. In the System Event Log it says: "The service terminated unexpectedly. The system cannot find the path specified". However, I run nssm edit 'servicename' and see the path is there. – Igor Micev Mar 10 '22 at 19:30
  • this service is created, but never starts – Sand T May 16 '23 at 16:59
2

Expanding a bit on the other answer, there are a few different ways to run FastAPI as a Windows Service (which generalizes to being able to run any Python app as a Windows Service). The most common ways I have found are:

  1. Use NSSM
  2. Use one of the officially documented techniques here.

After trying a few of them out, I found NSSM to be by far the easiest and most effective method. Basic steps below:

  1. Add a __main__ entry point to your FastAPI app that will be called by the Windows Service. The FastAPI deployment guide has helpful info on the various parameters. You probably want to tweak your "workers" variable based on expected load. Example (assumes your main FastAPI file is named main.py):
if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, workers=4)
  1. Install Python on the Windows machine if it isn't already there. Also download nssm.exe

  2. Test out your app on the Windows box by running py main.py. If it starts up and runs then you are good to deploy as a windows service.

  3. Create the service using nssm:

nssm install <windows service name> <python.exe path> main.py  
nssm set <windows service name> AppDirectory <root directory of app>  
nssm set <windows service name> Description <app description>
nssm start <windows service name> 

If all is well then it should be up and running. A few other commenters above have issues with the app starting, which is likely because the AppDirectory was not set so the files could not be found.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Kevin
  • 21
  • 2