Nginx+Gunicorn+Supervisor 部署 FastApi 项目

时间:2021-03-05 15:56:21   收藏:0   阅读:210

部署准备

  1. 有一台已经解析过域名的服务器,没有的话只能通过Ip访问项目。
  2. 安装了Gunicorn的虚拟环境,采用虚拟环境可以保障环境稳定性。
    采用conda创建虚拟环境:
    1.创建一个名为py3.6,版本为3.6的虚拟环境
        conda create --name py3.6 python=3.6
    2.进入虚拟环境:
        source activate py3.6
    3.安装gunicorn
        pip install gunicorn
    

3.确保你的项目在当前虚拟环境下可以正常运行。

使用gunicorn启动项目

1.在项目根目录,执行下面的命令启动服务,项目为FastAPI 项目:

```
gunicorn API_2_1_4:app -w 2 -k gthread --timeout 30 -b 0.0.0.0:8000
```

来解释一下各个参数的含义。

访问 ip:8000(ip 为你服务器的公网 ip),应用成功访问了,如果是flask或django项目,你可能要在此处收集静态文件,FastAPI 项目不需要。

Nginx 服务器

1.首先安装 Nginx:

yum install epel-release -y
yum install nginx -y

2.启动 Nginx 服务:

systemctl start nginx

3.配置nginx:

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;
    
    server
        {
            listen 888;
            server_name phpmyadmin;
            index index.html index.htm index.php;
            root  /www/server/phpmyadmin;
    
            #error_page   404   /404.html;
            include enable-php.conf;
    
            location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
            {
                expires      30d;
            }
    
            location ~ .*\.(js|css)?$
            {
                expires      12h;
            }
    
            location ~ /\.
            {
                deny all;
            }
    
            access_log  /www/wwwlogs/access.log;
        }
    # 导入自定义模块
    include /etc/nginx/conf.d/*.conf;
}
    server {
        charset utf-8;
        listen 80;
        server_name mytest.com 公网IP;

        location / {
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8000;
        }
    }

4.重启 nginx 使得配置生效:

systemctl restart nginx

5.域名访问

管理 Gunicorn 进程

  1. 现在 Gunicorn 是我们手工启动的,一旦我们退出 shell,服务器就关闭了,服务就无法访问。就算在后台启动 Gunicorn,万一哪天服务器崩溃重启了又得重新登录服务器去启动,非常麻烦。为此使用 Supervisor 来管理 Gunicorn 进程,这样当服务器重新启动或者 Gunicorn 进程意外崩溃后,Supervisor 会帮我们自动重启 Gunicorn。
  2. 安装 Supervisor
pip install supervisor
  1. supervisor 目录结构
(py3.6) [root@iZbp11gqesu0znu7pkmgp7Z etc]# tree
.
├── supervisor
│   ├── conf.d
│   │   └── fig1.ini
│   └── var
│       ├── log
│       │   ├── supervisord.log
│       │   ├── wenda-stderr.log
│       │   ├── wenda-stdout.log
│       │   └── wenda-stdout.log.1
│       ├── supervisord.pid
│       └── supervisor.sock
└── supervisord.conf

4 directories, 8 files
mkdir -p ~/etc/supervisor/conf.d
mkdir -p ~/etc/supervisor/var/log
cd ~/etc
echo_supervisord_conf > supervisord.conf

原文:https://www.cnblogs.com/gofun/p/14486216.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!