如何在IIS 10上使用FastAPI运行Python?

sbtkgmzw  于 5个月前  发布在  Python
关注(0)|答案(2)|浏览(97)

配置:

  • Windows Server 2019
  • https://www.iis.net/downloads/microsoft/httpplatformhandler安装了httpPlatformServer x64
  • 已为所有用户安装Python 3.12.1 32位C:\python312-32\
  • 已安装FastAPI库,使用
  • 创建了新的应用程序池(Pool-A),使用自定义帐户(User-A)作为身份,无托管代码,集成管道
  • 已创建C:\python-logs\文件夹用于日志
  • 创建了C:\python-app\,文件为main.py
  • 设置文件夹中User-A的完全控制:python312-32,python-logs,python-app
  • 添加应用程序到路由/python-app与Pool-A

C:\python-app\main.py:

from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
    return {"message": "Sample app"}

字符串
C:\python-app\web.config(基于https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="c:\python312-32\python.exe"
                  arguments="c:\python-app\main.py --port %HTTP_PLATFORM_PORT%"
                  stdoutLogEnabled="true"
                  stdoutLogFile="c:\python-app\python.log"
                  startupTimeLimit="60"
                  processesPerApplication="16">
      <environmentVariables>
        <environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>


我总是得到:

  • HTTP错误500.19 -内部服务器错误
  • 错误代码0x 80070003
  • 无法读取配置文件

怎么了?
我没有使用FastCGI,因为项目没有维护。

iszxjhcz

iszxjhcz1#

此错误是由于缺少权限或物理路径与虚拟目录的路径不匹配而导致的。例如,Web应用程序物理根路径下不存在Web.config。
请验证Web.config路径是否存在并且权限设置是否正确。请收集进程监视器日志以获取有关错误的详细信息。
更多信息,请参考此链接:HRESULT代码0x80070003。

b1zrtrql

b1zrtrql2#

我已经包括了下面的指令使用FastAPI(Python库)在IIS与HTTP平台的Java。
1.为所有用户安装Python 3.12 64位(installer
1.安装Microsoft HTTP Platform SDK 1.2(installer
1.以管理员身份安装Python包pip install fastapi uvicorn
1.创建文件夹C:\python-app
1.创建文件夹C:\python-app\logs
1.创建C:\python-app\main.py

from fastapi import FastAPI
app = FastAPI()
@app.get("/python") #route must contain alias set in IIS
async def get_root():
    return {"message": "Hello World"}

字符串
1.创建“C:\python-app\web.config”

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
    <handlers>
        <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <httpPlatform processPath="C:\Program Files\Python312\Scripts\uvicorn.exe" arguments="main:app --host 0.0.0.0 --port %HTTP_PLATFORM_PORT% --log-level info" stdoutLogEnabled="true" stdoutLogFile=".\logs\uvicorn" startupTimeLimit="60" processesPerApplication="16">
    <environmentVariables>
        <environmentVariable name="PYTHONPATH" value="C:\python-app\" />
        <environmentVariable name="HTTP_PLATFORM_PORT" value="%HTTP_PLATFORM_PORT%" />
    </environmentVariables>
    </httpPlatform>
</system.webServer>
</configuration>

  • 创建PythonAppPool
  • 无托管代码
  • 综合管线
  • “ApplicationPoolIdentity”作为应用程序池标识
  • 向文件夹添加权限
icacls "C:\Program Files\Python312" /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls "C:\Program Files\Python312" /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"
icacls "C:\python-app" /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls "C:\python-app" /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"

  • 将python应用程序添加到主机根目录上的IIS
  • 别名:python
  • 应用程序池:PythonAppPool
  • 物理路径:C:\python-app
  • 在浏览器中打开http:\\localhost\python
  • 如果一切正常,则可以在web.config文件中将--log-level更改为error

相关问题