安装配置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
配置:
配置文件默认从以下路径查找
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf
(since Supervisor 3.3.0)../etc/supervisord.conf
(Relative to the executable)../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)