supervisor+nginx+gunicorn 部署

本文介绍如何在Linux环境下安装并配置Gunicorn与Nginx,实现Python Web应用的高效运行。通过设置Supervisor自动启动Gunicorn,利用Nginx进行负载均衡,确保应用稳定性和性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装gunicorn && 启动

(env) apt install gunicorn
gunicorn --workers=4 --bind=0.0.0.0:8002 wsgi:app
上面的命令等价于
gunicorn -w 4 -b 0.0.0.0:8002 wsgi:app
wsgi 为项目根目录下的wsgi.py app是该文件中实例对象
我们后面将使用supervisor来启动gunicorn

nginx 安装 && 配置

apt install nginx

cat /etc/nginx/nginx.conf
......
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
....
我们只需要在nginx/conf.d 目录下创建.conf后缀的配置文件即可

nginx 常用配置指令
events:事件设置
http : http设置 包括 server 和upstream
server:主机设置(域名) ,含location
location:URL设置
upstream:负载均衡设置

在/etc/nginx/conf.d目录下新建 sayhello.conf,内容如下
proxy_pass 为gunicorn 启动监听的服务,即你的应用程序

server{
        listen 8003;
        server_name 39.106.217.14;
        access_log /root/log/sayhello_nginx.log;
        error_log /root/log/sayhello_nginx_error.log;
        location / {
                proxy_pass http://127.0.0.1:8002;
                
        }

}

**重启即可 service nginx restart **

supervisor 配置 && install
  1. apt install supervisor
  2. supervisor 的配置文件放置在 /etc/supervisor 下的 supervisor.conf 中,
  3. 在supervisor.conf中
......
......
[include]
files = /etc/supervisor/conf.d/*.conf -----> 我们只需要在这个目录先建立.conf后缀的配置文件即可

在 /etc/supervisor/conf.d/ 中建立 sayhello.conf 文件 来启动gunicorn

[program:sayhello]
command=/root/.virtualenvs/sayhello/bin/gunicorn -w 4 -b 0.0.0.0:8002 wsgi:app
directory=/root/code/sayhello
user=root
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stdout_logfile=/root/log/sayhello.log

** service supervisor restart** 重启即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值