首页 > 其他分享 >宝塔上部署FastAPI的步骤和一些注意点

宝塔上部署FastAPI的步骤和一些注意点

时间:2023-03-25 15:36:23浏览次数:37  
标签:宝塔 www FastAPI py helloworld File 步骤 line wwwpython

为了运维方便,选择直接用宝塔来管理python fastapi的项目,虽然直接部署可能性能更好更灵活,但是我选择了低层本,每个人的选择可能是不一样的,各有 考虑吧。

 

本文的大逻辑是先写一个helloworld的程序,然后再部署到服务器上

 

步骤一:先本地运行一个基于fastapi的helloWorld例子,方便后面在服务器上验证

1. 编写基于FastApi的Hello World文件main.py

import uvicorn
from fastapi import FastAPI

app = FastAPI()  

@app.get("/")
def sayHi():
    return {"message":"Hello world!"}

# 启动uvicorn服务,默认端口8000  uvicorn myapi:api  --reload
if __name__ == '__main__':
    uvicorn.run('main:app')

2. 显示本地运行跑通,本地可以使用vscode编译器,并在运行dos命令

pip install fastapi[all]
uvicorn main:app --reload

reload参数是为了修改代码后的热部署,运行没有报错后可以浏览器访问: http://127.0.0.1:8000

如果看到打印信息则说明OK

 

步骤二:在宝塔上部署python的环境

1. 商店安装插件python进程管理插件

    * Python项目管理器:管理应用实例

    * 进程守护管理器:实例进程的守护

2. 进入Python项目管理器,首先安装python版本,尽量与本地的一致,避免出现本地好的,服务器上出现问题,特别怕依赖包不一致的问题。本地查看版本的命令是

python --version

3.在本地生成requirements.txt,否则宝塔创建项目会报错。创建命令如下

pip freeze >requirements.txt
pip install -r requirements.txt

  将代码上传到宝塔的/www/wwwpython/helloworld

4. 在python进程管理插件创建项目,具体参数如下,记得选择gunicon

 

5. 在配置修改参数,重启。

默认为worker_class = 'geventwebsocket.gunicorn.workers.GeventWebSocketWorker'
修改为worker_class = 'uvicorn.workers.UvicornWorker'

7.这里有一个坑,启动后系统会自动暂停,日志报错如下的话,是2个问题,一是启动文件名为main.py,二是启动文件是app,即在main.py中启动命令是:

uvicorn.run('main:app') 具体报错信息如下:Worker failed to boot.
Traceback (most recent call last):
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/arbiter.py", line 586, in spawn_worker
    worker.init_process()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 203, in init_process
    super(GeventWorker, self).init_process()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 135, in init_process
    self.load_wsgi()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
    result = _import(*args, **kwargs)
  File "/www/wwwpython/helloworld/myapi.py", line 9
SyntaxError: Non-ASCII character '\xe5' in file /www/wwwpython/helloworld/myapi.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
[2023-03-25 09:26:44 +0000] [7373] [INFO] Worker exiting (pid: 7373)
[2023-03-25 09:26:44 +0000] [7368] [INFO] Shutting down: Master
[2023-03-25 09:26:44 +0000] [7368] [INFO] Reason: Worker failed to boot.
[2023-03-25 09:26:45 +0000] [7385] [INFO] Starting gunicorn 19.10.0
[2023-03-25 09:26:45 +0000] [7385] [INFO] Listening at: http://0.0.0.0:12345 (7385)
[2023-03-25 09:26:45 +0000] [7385] [INFO] Using worker: geventwebsocket.gunicorn.workers.GeventWebSocketWorker
[2023-03-25 09:26:45 +0000] [7394] [INFO] Booting worker with pid: 7394
[2023-03-25 09:26:45 +0000] [7394] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/arbiter.py", line 586, in spawn_worker
    worker.init_process()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 203, in init_process
    super(GeventWorker, self).init_process()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 135, in init_process
    self.load_wsgi()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/www/wwwpython/helloworld/helloworld_venv/lib/python2.7/site-packages/gevent/builtins.py", line 96, in __import__
    result = _import(*args, **kwargs)
  File "/www/wwwpython/helloworld/myapi.py", line 9
SyntaxError: Non-ASCII character '\xe5' in file /www/wwwpython/helloworld/myapi.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
[2023-03-25 09:26:45 +0000] [7394] [INFO] Worker exiting (pid: 7394)
[2023-03-25 09:26:45 +0000] [7385] [INFO] Shutting down: Master
[2023-03-25 09:26:45 +0000] [7385] [INFO] Reason: Worker failed to boot.

8. 设置映射域名,比如设置了 demo.xxx.com,即系统会自动创建这个域名的网站,并设置了反向代理

通过 http://demo.xxx.com 返回正常即配置完成。

 

各位,祝好运,顺利完成配置。

 

标签:宝塔,www,FastAPI,py,helloworld,File,步骤,line,wwwpython
From: https://www.cnblogs.com/jpeanut/p/17254756.html

相关文章

  • [FastAPI-22]响应模型-response_model
    importtypingfromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()'''响应模型s......
  • [FastAPI-21]响应体
    fromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()classUser(BaseModel):u......
  • [FastAPI-20]设置响应头
    fromfastapiimportFastAPI,Responsefromfastapi.responsesimportJSONResponsefrompydanticimportBaseModelapp=FastAPI()classUser(BaseModel):u......
  • CAD外部参照如何重新定位?CAD外部参照重定位步骤
    CAD外部参照如何重新定位?这个问题并不算是一个常见的问题,但偶尔也会遇到,今天小编就来给大家简单介绍一下浩辰CAD软件中CAD外部参照重定位的操作步骤,一起来看看吧!CAD外部参......
  • nfs实验步骤
    1.yuminstall-ynfs-utils查看是否安装nfs2.vim/etc/sysconfig/network-scripts/ifcfg-eno16777736编辑ip地址网关信息3.systemctlrestartnetwork重启网卡4.......
  • Centos7下安装Elasticsearch6步骤
    Centos7下安装Elasticsearch6步骤1、安装jdk2、安装Elasticsearch1、新建一个esGroup组和用户(出于安全考虑,Elasticsearch默认是不允许使用root账号运行的)2、更改Elasticsear......
  • [FastAPI-21]响应状态码
    fromfastapiimportFastAPI,statusfrompydanticimportBaseModelapp=FastAPI()'''响应状态代码status'''classUser(BaseModel):username:str......
  • [FastAPI-18]Filed请求体校验
    importrandomfromfastapiimportFastAPIfrompydanticimportField,BaseModelimporttypingapp=FastAPI()'''请求体的每一个字段需要单独校验name长度最......
  • [FastAPI-14]pydantic多个请求体
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''多个请求体{"user":{"username":"string","password":"string"......
  • [FastAPI-13]pydantic请求体接收数据
    fromfastapiimportFastAPIfrompydanticimportBaseModelapp=FastAPI()'''创建继承BaseModel的类,定义模型user路径函数中定义形参user,类型为User通过对象use......