• nginx+uwsgi+bottle python服务器部署


    一、安装nginx(如果服务器上已经有nginx了,则无需重复安装)

    sudo apt-get install nginx

    二、nginx配置,例如:/etc/nginx/conf.d/digger.conf

    server{
        listen 9000;
        server_name 127.0.0.1;
        root  /data/webroot/python/digger;
    
        location / {
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:9090;
        }
    }

    注:端口随意,不冲突就好;server_name 可以为域名或ip;

    三、安装 uwsgi

    pip install uwsgi

    四、编写入口py文件,保存为 web.py文件(其他文件名也随意)

    import os
    from bottle import Bottle, request
    
    mybottle = Bottle()
    
    @mybottle.route('/')
    def index():
        return 'Hello World!'
    
    @mybottle.route('/<spider>/run')
    def runSpider(spider):
        url = request.query.url
        if url is None:
            return 'params error'
        cwd = os.getcwd().replace('\', '/')
        return os.system('python3 {cwd}/run.py {spider} {url}'.format(cwd=cwd, spider=spider, url=url))
    
    
    application = mybottle

    五、启动 uwsgi(端口要和上面nginx配置的一致)

    uwsgi --socket 127.0.0.1:9090 --wsgi-file web.py --master --processes 4 --threads 2 --daemonize /var/log/uwsgi/app/web.log

    默认情况下,修改web.py不会立即生效,需要重启uwsgi;如果调试阶段想自动重载,可以增加 --py-autoreload 1 参数

    uwsgi --socket 127.0.0.1:9090 --wsgi-file web.py --master --processes 4 --threads 2 --py-autoreload 1 --daemonize /var/log/uwsgi/app/web.log

    PS:使用 -- daemonize 参数时为后台守护进程运行,不使用时为当前会话窗口运行

    其他配置及参数看这里 -> https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

    六、重启 nginx 

    nginx -s reload

    七、访问 http://127.0.0.1:9000 看看效果

    八、使用 supervisor 来管理 uwsgi 进程,请看下文:

    《使用 supervisor 来管理 python 进程(以uwsgi为例)》


    完。

  • 相关阅读:
    sublime text 2安装及使用
    C陷阱与缺陷之语法陷阱
    上门洗车APP --- Androidclient开发 之 项目结构介绍
    编写语法分析程序
    TCP header
    boost事件处理
    TP-LINK无线路由器WR340G+ 54M支持WDS
    300M无线路由器 TL-WR842N
    python 2.7 支持dict comprehension
    100M 宽带办理
  • 原文地址:https://www.cnblogs.com/tujia/p/12084948.html
Copyright © 2020-2023  润新知