Setup uWSGI as webserver with pyramid (no NGINX)

2019-03-21 13:29发布

问题:

Most of the available tutorials show how to set up uWSGI with an upstream HTTP server (like NGINX). But uWSGI alone can act beautifully as router/proxy/load-balancer - refer this For my project, I didn't want to setup NGINX at this moment so I started off exploring the option of serving webpages through uWSGI. The answer here shows how to set it up with Pyramid.

回答1:

I am using pyramid_mongodb scaffold, which I have modified to get it working on python3. See here for details. Assuming that we have a Pyramid project (created with pcreate -s pyramid_mongodb MyProject). Here are the uWSGI configurations needed in development/production.ini

[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

Also since we are using uWSGI we can comment out server portion from the ini

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

To run the server use uwsgi --ini-paste development.ini



回答2:

Much more easier! No need to modify at all the "development.ini" file. Create in the App folder where your "development" and "production" ini files reside, a file called "wsgi.app" with the following content:

from pyramid.paster import get_app,setup_logging

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

create let's say "myapp.conf" with it's content:

[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

and the NGINX configuation is very simple:

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. restart nginx with "/path/to/usr/sbin/nginx -s reload"
  2. start the uwsgi process -> change to "cd /usr/local/uwsgi-2.0.9" -> ./uwsgi -ini /var/www/myapp.conf


标签: pyramid uwsgi