zoukankan      html  css  js  c++  java
  • 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为例)》


    完。

  • 相关阅读:
    Servlet 06: jar包引入和web配置文件创建
    Servlet 05: Servlet生命周期方法
    Servlet 04: 什么是重定向
    Servlet 03: 通过xml配置文件配置servlet访问路径
    080 面向过程编程
    079 内置函数
    078 匿名函数
    077 递归
    076 生成器
    075 字典生成式
  • 原文地址:https://www.cnblogs.com/tujia/p/12084948.html
Copyright © 2011-2022 走看看