Nginx+supervisord 部署python应用

安装配置Nginx

安装:sudo apt install nginx

配置:

upstream aiohttp {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response

    # Unix domain servers
    server unix:/tmp/example_1.sock fail_timeout=0;
    server unix:/tmp/example_2.sock fail_timeout=0;
    server unix:/tmp/example_3.sock fail_timeout=0;
    server unix:/tmp/example_4.sock fail_timeout=0;

    # Unix domain sockets are used in this example due to their high performance,
    # but TCP/IP sockets could be used instead:
    # server 127.0.0.1:8081 fail_timeout=0;
    # server 127.0.0.1:8082 fail_timeout=0;
    # server 127.0.0.1:8083 fail_timeout=0;
    # server 127.0.0.1:8084 fail_timeout=0;
  }

server {
listen 80;
client_max_body_size 4G;

server_name example.com;

location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://aiohttp;
}

location /static {
# path for static files
root /path/to/app/static;
}

}

安装配置supervisord

安装: pip install git+https://github.com/Supervisor/supervisor

配置:

配置文件默认从以下路径查找

  1. $CWD/supervisord.conf
  2. $CWD/etc/supervisord.conf
  3. /etc/supervisord.conf
  4. /etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
  5. ../etc/supervisord.conf (Relative to the executable)
  6. ../supervisord.conf (Relative to the executable)

生成supervisord.conf文件,没有权限的话设置一下权限:

mkdir /etc/supervisor

echo_supervisord_conf > /etc/supervisor/supervisord.conf

supervisord.conf 修改最下面两行:

;[include] 
;files = relative/directory/*.ini    ; 
# 把前面的注释号去掉
[include] 
files = conf.d/*.ini    ; 

创建自己的配置文件:

mkdir /etc/supervisor/conf.d

vim /etc/supervisor/conf.d/example.ini

[program:aiohttp]
numprocs = 4
numprocs_start = 1
process_name = example_%(process_num)s

; Unix socket paths are specified by command line.
command=/path/to/aiohttp_example.py --path=/tmp/example_%(process_num)s.sock

; We can just as easily pass TCP port numbers:
; command=/path/to/aiohttp_example.py --port=808%(process_num)s

user=nobody
autostart=true
autorestart=true

启动:

开启服务:Supervisord

重新加载配置:supervisorctl reload

查看状态: supervisorctl status

在配置和启动过程中如果遇到没有权限的地方需要配置权限。

python 服务器示例:

#!/usr/bin/env python3
from aiohttp import web
import argparse

parser = argparse.ArgumentParser(description="aiohttp server example")
parser.add_argument('--path')
parser.add_argument('--port')


class MyView(web.View):

    async def get(self):
        return web.Response(text="Hello, world。")



app = web.Application()

app.add_routes([web.view('/', MyView)])

args = parser.parse_args()
web.run_app(app, host="127.0.0.1", port=args.port, path=args.path)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值