FastAPI 生产部署

开发部署

安装asgi插件

pipenv install uvicorn

# main.py

from fastapi import FastApi

app=FastApi()


@app.get('/hello')
async def hello():
    return {'message':'hello World'}


nvicorn main:app --reload 开发模式下运行 热加载

生产环境部署 CentOS8.x +Nginx

安装插件 pipenv install gunicorn

shell中执行gunicorn -v有版本输出表示安装成功

# 编写配置文件 gunicorn.py

daemon=True
bind='0.0.0.0:8000'
pidfile='/var/run/gunicorn.pid'
chdir='/opt/web/fastapi' # 工作目录
worker_class='uvicorn.workers.UvicornWorker'
workers=1  #multiprocessing.cpu_count()+1
threads=2
loglevel='debug' # 日志级别
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
accesslog = "/opt/web/fastapi/gunicorn_access.log" 
errorlog = "/opt/web/fastapi/gunicorn_error.log" 

gunicorn main:app -c gunicorn.py

在gunicorn_error.log  文件中看到日志输出表示启动成功

配置 gunicorn.service服务开机自启动

cat >/usr/lib/systemd/system/gunicorn.service << EOF
[Unit]
Description=Gunicorn fast
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking

PIDFile=/var/run/gunicorn.pid
ExecStart=/root/.local/share/virtualenvs/fastapi-Xq8atoqR/bin/gunicorn  -c 
/opt/web/fastapi/gunicorn.py main:app
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

EOF

systemctl daemon-reload

systemctl enable gunicorn

systemctl start gunicorn

查看服务状态

 systemctl status gunicorn

配置nginx代理访问

server {
        listen 80;
        server_name api.rainbow.cn;
        access_log  /var/log/nginx/access.log;
     
        location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

nginx -s reload

 完成FastApi的生产部署

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 可以使用 Docker 部署 FastAPI 应用程序,也可以使用 uWSGI 或 Gunicorn 部署。在 Ubuntu 上,可以使用 Nginx 作为反向代理服务器来处理请求。具体的部署步骤可以参考 FastAPI 的官方文档。 ### 回答2: 在Ubuntu上部署FastAPI可以按照以下步骤进行: 1. 首先,确保已经安装了Python和pip。 ``` $ sudo apt update $ sudo apt install python3 $ sudo apt install python3-pip ``` 2. 创建一个新的虚拟环境(可选但推荐)。 ``` $ python3 -m venv myenv $ source myenv/bin/activate ``` 3. 安装FastAPIuvicorn。 ``` $ pip3 install fastapi $ pip3 install uvicorn ``` 4. 编写一个FastAPI应用程序。 ```python from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} ``` 5. 使用uvicorn运行应用程序。 ``` $ uvicorn main:app --host 0.0.0.0 --port 8000 ``` 这将在本地主机的8000端口上运行应用程序。 6. 若要在生产环境中使用FastAPI,您可以使用Gunicorn作为反向代理服务器。 ``` $ pip3 install gunicorn $ gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app ``` 这将在8000端口上运行FastAPI应用程序,并使用4个工作进程进行请求处理。 通过按照以上步骤,在您的Ubuntu服务器上部署FastAPI应用程序应该是比较简单的。您可以根据您的需求进行进一步配置和调整。 ### 回答3: 要将FastAPI部署在Ubuntu上,可以按照以下步骤进行操作: 1. 确保Ubuntu系统已正确安装和配置。确保系统处于最新更新状态,可以通过运行`sudo apt update && sudo apt upgrade`命令来更新系统。 2. 安装Python和相关依赖。FastAPI是用Python编写的,因此需要在Ubuntu上安装Python及其相关依赖。在终端中运行以下命令安装Python: ``` sudo apt install python3-dev python3-pip ``` 3. 创建并激活虚拟环境(可选)。为了避免与系统中的其他Python软件包发生冲突,可以创建一个虚拟环境来安装和运行FastAPI。在终端中运行以下命令创建虚拟环境: ``` python3 -m venv myenv source myenv/bin/activate ``` 4. 安装FastAPI和其它软件包。在虚拟环境中运行以下命令来安装FastAPI和相应的依赖: ``` pip install fastapi[all] ``` 这将安装FastAPI及其所有附带的依赖,包括uvicorn作为默认的Web服务器。 5. 编写FastAPI应用程序。创建一个Python文件,例如`main.py`,使用FastAPI编写你的应用程序逻辑。例如,你可以创建一个简单的接口,显示一个Hello World消息。示例代码如下: ```python from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} ``` 6. 启动FastAPI应用程序。在终端中运行以下命令,启动FastAPI应用程序: ``` uvicorn main:app --host 0.0.0.0 --port 8000 ``` 这将使FastAPI应用程序在本地主机的8000端口上运行。 7. 在浏览器中测试。使用浏览器或任何HTTP客户端工具,访问`http://localhost:8000`,你将看到FastAPI应用程序返回的Hello World消息。 通过按照以上步骤,在Ubuntu上成功部署和运行FastAPI应用程序。这使你能够构建高性能、现代化的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恒云客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值