首页 > 系统相关 >Ubuntu部署FastApi项目

Ubuntu部署FastApi项目

时间:2023-04-20 13:35:29浏览次数:40  
标签:gunicorn 部署 FastApi py 本例 nginx conf Ubuntu

环境介绍

系统: Ubuntu 22.04
Pyhton版本:3.8.10
Fastapi版本:0.95.0
Gunicorn版本:20.1.0

准备工作

1. ssh 连接工具(本例使用基于Windows的Linux子系统中的ssh工具)
2. 配置nginx代理服务器
3. 配置Gunicorn WSGI HTTP服务器

一、SSH连接Ubuntu服务器

ssh username@host

username 是Ubuntu的登录密码

host 是Ubuntu的公网IP地址

image

二、nginx安装和配置

nginx安装

    apt install nginx

具体实施步骤:

  1. apt install nginx
  2. 进入目录/etc/nginx 找到 nginx.conf 文件
  3. 修改nginx.conf文件,添加以下配置

        server {
                listen 80;
                server_name domain_name;# 这里的domain_name是域名地址。
                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;
                }
        }

三、Gunicorn的安装和配置

安装

pip install Gunicorn

创建文件 gunicorn_conf.py (可以使用Vim)

sudo vim gunicon_conf.py

按 “i” 键进入编辑模式,将下面的代码Copy到编辑器

    from multiprocessing import cpu_count

    # 是否守护
    daemon = True

    # 绑定
    bind = '0.0.0.0:8000'

    # pid 文件地址
    pidfile = 'gunicorn.pid'

    # 项目地址
    chdir = '.'

    # Worker Options
    workers = cpu_count() + 1
    worker_class="uvicorn.workers.UvicornWorker"
    threads = 2

    # Logging Options
    loglevel="debug"
    accesslog = 'access_log.log'
    errorlog =  'error_log.log'

按 “ESC” 输入 “:wq!”保存。

将该文件Copy到项目目录(本例中项目目录在~/testapi/tracker)

cp gunicorn_conf.py ~/testapi/tracker

启动gunicorn

gunicorn main:app -c gunicorn_conf.py

最后的结果应该是可以看到 接口文档了
image

写在最后

  1. 一般本地服务器需要换源之后才能快速安装,本例使用的是线上服务器,已经换过源了,若是自己的服务器,建议先换apt源再换pip源。

  2. nginx配置没提交完整配置文件,新版本的nginx需要注释掉第一行

        user www-data;
  1. 如果需要停止服务,可以直接使用
        # 结束进程
        killall gunicorn 

再使用

    gunicorn main:app -c gunicorn_conf.py

开启服务。

  1. Linux基本操作 和 数据库的部署不在本例的范围内

标签:gunicorn,部署,FastApi,py,本例,nginx,conf,Ubuntu
From: https://www.cnblogs.com/LearningC/p/17336452.html

相关文章

  • obd自动化部署单副本集群
    1.环境介绍centos7.6可用内存不少于8G根目录可用空间不少于30G 2.介绍obd针对不同部署场景提供不同配置文件谷歌浏览器:https://github.com/oceanbase/obdeploy/tree/master/examplemini-single-example.yaml:observer单节点配置文件(最小资源配置)mini-single-with-ob......
  • k8s deployment资源部署java以及skywalking agent示例
    catdeploy.ymlapiVersion:apps/v1kind:Deploymentmetadata:name:app-namenamespace:your-namespaceannotations:kubernetes.io/change-cause:2.11.0-SNAPSHOT-20230420-46#版本说明-用于回滚等labels:app:app-namespec:replicas:1sel......
  • apisix网关安装部署文档
    apisix网关安装部署文档1. apisix网关简介及官方文档apisix简介:Apache APISIX 是 Apache 软件基金会下的云原生 API 网关,它兼具动态、实时、高性能等特点,提供了负载均衡、动态上游、灰度发布(金丝雀发布)、服务熔断、身份认证、可观测性等丰富的流量管理功能。我们可以使......
  • ubuntu 22.04安装postgresql
    安装sudoaptinstallpostgresql修改/etc/postgresql/14/main/postgresql.conf把listen_addresses='127.0.0.1'修改为listen_addresses='*'/etc/postgresql/14/main/pg_hba.conf添加hostallall0.0.0.0/0m......
  • 部署kubernetes-dashboard顺便搞懂kubernetes中的ServiceAccount和RBAC
    "种草"kubernetes-dashboard安装部署dashboard创建用于登录面板的ServiceAccount权限控制"种草"kubernetes-dashboardKubernetesDashboard是通用的用于管理Kubernetes集群的WebUI面板kubernetes-dashboard代码库readme中对自己的介绍:KubernetesDashboardi......
  • vue项目部署后 nginx配置重定向 大文件报错问题
      删除Nginx缓存文件试试?#rm-rf/usr/local/nginx/proxy_temp  注意proxy_busy_buffers_size是proxy_buffers的两倍,proxy_temp_file_write_size也要比proxy_buffers大。  ......
  • Ubuntu20.04下tacacs服务器安装
    1、下载文件,最新的版本可以在这里看到(ftp://ftp.shrubbery.net/pub/tac_plus)百度网盘https://pan.baidu.com/s/1z8pEHDrJW0kyuyYF6U0Jag,密码(r1r1)sudowgetftp://ftp.shrubbery.net/pub/tac_plus/tacacs-F4.0.4.28.tar.gz2、使用apt-get软件包管理器安装了libwrap0-dev、fle......
  • Ubuntu搭建SFTP服务器笔记
    说明:server01为sftp服务端;client01为客户端【在server01上操作】1、强力卸载openssh-serverapt-getremove--purgeopenssh-server2、安装openssh-server1)更新apt-get源apt-getupdate2)安装openssh-serverapt-getinstallopenssh-server【在client01上操作】3、使用......
  • Kubuesphere部署Ruoyi(二):部署kubesphere
    先决条件:更换DNS更换apt的镜像源Ubuntu下永久性修改DNSvi/etc/systemd/resolved.confDNS字段取消注释,并修改DNS为223.5.5.5223.5.5.5是一个IP地址,是AlibabaCloud提供的免费DNS服务器的IP地址。修改后保存。systemctlrestartsystemd-resolved清华镜像源https://m......
  • ubuntu18.04 ORB-SLAM3编译运行
    如何编译?1、代码下载;gitclonehttps://github.com/UZ-SLAMLab/ORB_SLAM3或者gitclonehttps://github.com/electech6/ORB_SLAM3_detailed_comments2、按照里面的README.md进行环境安装和编译;安装和编译过程中可能会出现的问题:1)提示找不到GLEW解决方法:sudoaptinstallli......