【问题标题】:Setup uWSGI as webserver with pyramid (no NGINX)将 uWSGI 设置为带有金字塔的网络服务器(无 NGINX)
【发布时间】:2013-04-27 10:09:19
【问题描述】:

大多数可用的教程都展示了如何使用上游 HTTP 服务器(如 NGINX)设置 uWSGI。但是 uWSGI 单独可以很好地充当路由器/代理/负载平衡器 - 请参阅 this 对于我的项目,我现在不想设置 NGINX,所以我开始探索通过 uWSGI 提供网页的选项。此处的答案显示了如何使用 Pyramid 进行设置。

【问题讨论】:

    标签: pyramid uwsgi


    【解决方案1】:

    我正在使用 pyramid_mongodb 脚手架,我已经对其进行了修改以使其在 python3 上工作。有关详细信息,请参阅here。 假设我们有一个 Pyramid 项目(使用pcreate -s pyramid_mongodb MyProject 创建)。 以下是 development/production.ini 中需要的 uWSGI 配置

    [uwsgi]
    http = 0.0.0.0:8080
    #http-to /tmp/uwsgi.sock - use this for standalone mode
    #socket = :9050
    master = true
    
    processes = 2
    
    harakiri = 60
    harakiri-verbose = true
    limit-post = 65536
    post-buffering = 8192
    
    daemonize = ./uwsgi.log
    pidfile = ./orange_uwsgi.pid
    
    listen = 128 
    
    max-requests = 1000
    
    reload-on-as = 128 
    reload-on-rss = 96
    no-orphans = true
    
    #logto= <log file>
    log-slow = true
    
    virtualenv = <path to virtual environment>
    
    #file = /path/to/pyramid.wsgi
    #callable = application
    
    need-app = true
    

    此外,由于我们使用的是 uWSGI,我们可以从 ini 中注释掉 server 部分

    #[server:main]
    #use = egg:waitress#main
    #host = 0.0.0.0
    #port = 6544
    

    要运行服务器,请使用 uwsgi --ini-paste development.ini

    【讨论】:

    • 为什么我们在使用uWSGI时可以注释掉'server:main'?
    【解决方案2】:

    更容易!根本不需要修改“development.ini”文件。 在“开发”和“生产”ini 文件所在的 App 文件夹中创建一个名为“wsgi.app”的文件,其内容如下:

    from pyramid.paster import get_app,setup_logging
    
    ini_path = '/pathto/myapp/development.ini'
    setup_logging(ini_path)
    application = get_app(ini_path,'main')
    

    创建让我们说“myapp.conf”的内容:

    [uwsgi]
    socket = 127.0.0.1:3053
    uid = daemon
    gid = daemon
    
    venv = /pathto/myenv
    project_dir = /pathto/myapp
    chdir = %(project_dir)
    master = true
    plugins = plugins/python/python
    
    check-static = %(project_dir)
    static-skip-ext = .py
    static-skip-ext = .pyc
    static-skip-ext = .inc
    static-skip-ext = .tpl
    
    pidfile2 = /var/run/uwsgi/myinfo.pid
    disable-logging = true
    processes = 8
    cheaper = 2
    
    enable-threads = true
    offload-threads = N
    py-autoreload = 1
    wsgi-file = /pathto/myapp/wsgi.py
    

    NGINX 的配置非常简单:

    server {
     listen [xxxx:xxxx:xxxx:xxx:xxxx:xxxx]:80; #for IPv6
     listen xxx.xxx.xxx.xxx:80; #for IPv4
    
     server_name myapp.domain.com;
    
     location / {
         try_files $uri @uwsgi;
     }
    
     location @uwsgi {
          include uwsgi_params;
          uwsgi_pass 127.0.0.1:3053;
       }
    }
    
    1. 用“/path/to/usr/sbin/nginx -s reload”重启nginx
    2. 启动uwsgi进程 -> 改为“cd /usr/local/uwsgi-2.0.9” -> ./uwsgi -ini /var/www/myapp.conf

    【讨论】:

    • 本例中的 NGINX 部分只是(可选)。但是此时应用程序应该能够监听127.0.0.1:3053上的请求
    猜你喜欢
    • 2015-01-03
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2017-12-19
    • 2020-05-03
    • 2019-01-18
    相关资源
    最近更新 更多