You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署FastAPI+Selenium报H10错 本地正常线上异常求助

FastAPI+Selenium部署Heroku触发H10错误排查方案

问题背景

基于Python开发的FastAPI服务集成Selenium脚本部署至Heroku平台后出现H10错误,部署前已添加所需buildpacks、配置对应config variable,尝试常规排查方案后问题仍未解决:

  • 本地localhost环境下服务与脚本可完美运行
  • 部署至Heroku服务端后始终无法正常启动
  • 参考材料:

针对性修复方案

H10错误本质是Heroku平台检测到Web应用进程启动崩溃,结合FastAPI+Selenium的技术栈,按以下优先级排查修复:

  • 端口绑定配置错误(最高发)
    Heroku不支持硬编码端口,会通过动态PORT环境变量分配服务端口,同时必须绑定0.0.0.0地址才能接收平台路由转发的请求,本地写死127.0.0.1:8000的配置必然触发H10。

    1. 代码层面启动配置修正为:
    import os
    import uvicorn
    from fastapi import FastAPI
    
    app = FastAPI()
    
    if __name__ == "__main__":
        uvicorn.run(
            app,
            host="0.0.0.0",
            port=int(os.environ.get("PORT", 8000))
        )
    
    1. 项目根目录创建无后缀名的Procfile文件,写入正确启动命令,注意入口文件与FastAPI实例名要和自身代码匹配:
    web: uvicorn main:app --host=0.0.0.0 --port=${PORT}
    
  • Selenium运行配置不匹配Heroku环境
    本地可运行的Selenium配置缺少Heroku无头环境必需参数时,会直接导致进程崩溃触发H10:

    1. 确认buildpack按顺序添加,缺失或顺序错误都会导致Chrome/Chromedriver路径异常:
    • heroku/python
    • heroku/heroku-buildpack-google-chrome
    • heroku/heroku-buildpack-chromedriver
    1. Selenium初始化必须添加Heroku适配参数,不要硬编码二进制路径:
    import os
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    def init_driver():
        chrome_options = Options()
        chrome_options.add_argument("--headless=new")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-dev-shm-usage")
        chrome_options.add_argument("--disable-gpu")
        chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
        driver = webdriver.Chrome(
            executable_path=os.environ.get("CHROMEDRIVER_PATH"),
            options=chrome_options
        )
        return driver
    

    注意不要在全局作用域初始化Selenium驱动,否则驱动启动失败会直接拖垮整个FastAPI进程。

  • 依赖配置错误
    检查根目录requirements.txt:

    • 必须包含fastapi、uvicorn、selenium等核心依赖,漏装依赖会导致启动时模块找不到直接退出
    • 不要固定Chromedriver、webdriver-manager等和Chrome版本强绑定的依赖版本,避免和buildpack自动安装的Chrome版本冲突
  • 精准定位错误
    构建日志仅能反映slug打包阶段的问题,启动阶段的崩溃需要通过实时日志定位,在本地安装Heroku CLI后执行以下命令,可直接看到进程崩溃的具体堆栈信息,针对性修复即可:

    heroku logs --tail -a 你的Heroku应用名称
    

内容的提问来源于stack exchange,提问作者Sushil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 19:18:26