【FastAPI】线上部署
-
1.编写代码并且上传到线上仓库
-
2.根目录下,data文件夹下克隆项目
-
3.创建虚拟环境并且下载第三方模块
- 3.1:创建虚拟环境
- 3.2:下载第三方模块
- 3.3: 安装pip install gunicorn
- 3.1:创建虚拟环境
virtualenv /envs/fastapi_demo --python=python3.9
source /envs/fastapi_demo/bin/activate
pip install -r requirements.txt
- 4.nginx的安装与配置
- 4.1:安装参考上一篇
- 4.2.配置,内容如下
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
listen [::]:80;
server_name localhost;
location /static {
alias /data/fastapi_demo/static;
}
location / {
proxy_pass http://127.0.0.1:9002;
}
}
}
- 4.3:启动关闭
# 启动
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
# 开机启动
systemctl enable nginx
-
5.启动gunicorn
-
6.访问