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为例)》


    完。

  • 相关阅读:
    验证SMTP工作过程
    FileZilla FTP服务器的安装和配置
    最后一块石头的重量
    不用加号的加法
    同构字符串
    最长公共子序列
    Telnet 验证HTTP工作过程
    矩阵的最小路径和
    子数组的最大累加和问题
    海思开发板——YOLOv3模型移植(4)
  • 原文地址:https://www.cnblogs.com/tujia/p/12084948.html
Copyright © 2011-2022 走看看