FastAPI is a Web framework for developing RESTful APIs in Python. FastAPI is based on Pydantic and type hints to validate, serialize, and deserialize data, and automatically auto-generate OpenAPI documents. It fully supports asynchronous programming and can run Uvicorn and Gunicorn.
In this topic, we will cover how to publish the FastAPI application with on windows server 2019.
Let’s assume you already have a FastAPI project and python environment associated with it and you successfully uploaded it on the server.
First, we will test it locally by following steps.
Activate your environment by enve_fold_path/Scripts/activate.bat command in cmd.
Go to your project directory using cmd ex: cd path/to/project/folder
Now, if you are in your project folder on the same cmd window then run python -m uvicorn main:app — reload command. here uvicorn is the asgi server, the main is the python file and the app is the FastAPI constructor function.
Command for running fastapi on local
4. If everything has done well so far then you can see your FastAPI is running on 127.0.0.1:8000 or localhost:8000.
Now we will host it on the IIS server, there are two ways of hosting on IIS
by Proxy server
by making the service and running it
Here we will follow the second that is By making the service and running it on the server.
We will add the command arguments inside our main.py file like given below
Download the NSSM for creating the service from given below https://nssm.cc/download
Unzip the nssm folder and go to your OS specific folder…. and open the command window here(cmd or Powershell)
Once you hit the enter button, you will see the confirmation message.
Woohoo!!! now service is created successfully and you can check it in services that one service will be there with the name “FastAPIWindowsService”.Next, you need to start the service.
After service successfully starts, now you can browse the API URL and will see that your API is running successfully.
Many clients I meet struggle to implement AI within their corporate infrastructure. They often become frustrated because their companies are mostly on-premises and use Windows, while the AI industry largely operates in the cloud. Although they can call APIs like ChatGPT and Azure OpenAI, they face problems when attempting to integrate more advanced AI features into solutions primarily written in Java and .NET.
This is where I see Python playing a crucial role. The AI industry predominantly uses it to develop solutions, making it easier to get up to speed with any AI example. The real question is whether you can integrate Python into your existing tech stack if it’s already based on Windows.
This article will show you how to setup an API written in Python using an amazing framework called FastAPI. This article is an introduction on how to use the framework, I blog later on more advanced use cases.
Setting up the app example in IIS
Follow the steps below to start with
NOTE: you’ll need to have Python and PIP installed for this, follow these steps if you haven’t installed it already
Create the website in IIS as shown in the image, note I’m using the port 8080
Copy the code example below into a main.py code (the example was taken from the Fast API website)
Install Fast API using the pip command pip install fastapi
Configuring the HttpPlatformHandler
The next step is to get the HttpPlatformHandler working in IIS, this is a handler that passes socket connections directly to the Python process (more info here).
Create a web.config file inside the website folder with the following content, make sure you change the folder to point at the location where python is installed in your local machine
Navigate to the http://localhost:8080/ which should load the API already
Fast API also comes with swagger already installed, you can simply navigate to http://localhost:8080/docs
Other considerations
You might find some recommendations to use hypercorn rather than uvicorn, I tried that initially but I couldn’t resolve an issue with socket permission, I raised it as a bug in their GitHub repo.
You might also find issues where the API loads and hangs forever. In that case, you’ll most likely need to grant IIS_IUSRS read access to the folder where the app and Python are located, as mentioned in here.
If you just want to run a program at system startup, you can use the Windows task scheduler or a tool like NSSM that runs command line programs as a service.