Simplest WSGI application

hello.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

Run it in the foreground while debugging or checking your web server configuration

Why all the similar examples? I'll discuss the pros and cons later.

HTTP protocol, TCP port

$ uwsgi --http-socket 127.0.0.1:2001 --wsgi-file hello.py
...
            

Important! Don't use simply --http, which enables an HTTP endpoint which routes to the application over the uwsgi protocol. That sounds reasonable, but performance is worse than routing HTTP directly to the worker running the application.

HTTP protocol, Unix socket

$ uwsgi --http-socket /tmp/helloHTTP.s --wsgi-file hello.py
...
            

(not simply --http)

FastCGI protocol, TCP port

$ uwsgi --fastcgi-socket 127.0.0.1:2001 --wsgi-file hello.py
...
            

FastCGI protocol, Unix socket

$ uwsgi --fastcgi-socket /tmp/helloFastCGI.s --wsgi-file hello.py
...
            

Want to use SCGI instead? Replace --http-socket or --fastcgi-socket with --scgi-socket.

Want to use uwsgi (the wire protocol, not the software implementation) instead? Replace --http-socket or --fastcgi-socket with --socket.

Towards more realistic use of uWSGI

For more realistic control of the application, create a uWSGI .ini file for the application to contain the options, and pass the .ini file on the uwsgi command line.

Important! These uWSGI configurations aren't optimized for any particular application. In particular, the values for processes and threads should be determined for your particular application.

Command-line usage for starting and stopping the application:

# uwsgi /path/to/hello-http-tcp.ini
# uwsgi --stop /path/to/hello-http-tcp.pid

You can use init scripts or daemontools or other suitable mechanisms to invoke uwsgi as necessary.

HTTP protocol, TCP port

[uwsgi]
pidfile = /tmp/hello-http-tcp.pid
daemonize = /tmp/uwsgi.log
http-socket = 127.0.0.1:2000
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon

Important! Don't use simply http = ..., which enables an HTTP endpoint which routes to the application over the uwsgi protocol. That sounds reasonable, but performance is worse than routing HTTP directly to the worker running the application.

HTTP protocol, Unix socket

[uwsgi]
pidfile = /tmp/hello-http-unix.pid
daemonize = /tmp/uwsgi.log
http-socket = /tmp/helloHTTP.s
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon

(not simply http = ...)

FastCGI protocol, TCP port

[uwsgi]
pidfile = /tmp/hello-fastcgi-tcp.pid
daemonize = /tmp/uwsgi.log
fastcgi-socket = 127.0.0.1:2001
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon

FastCGI protocol, Unix socket

[uwsgi]
pidfile = /tmp/hello-fastcgi-unix.pid
daemonize = /tmp/uwsgi.log
fastcgi-socket = /tmp/helloFastCGI.s
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon

SCGI protocol, TCP port

[uwsgi]
pidfile = /tmp/hello-scgi-tcp.pid
daemonize = /tmp/uwsgi.log
scgi-socket = 127.0.0.1:2002
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon

SCGI protocol, Unix socket

[uwsgi]
pidfile = /tmp/hello-scgi-unix.pid
daemonize = /tmp/uwsgi.log
scgi-socket = /tmp/helloSCGI.s
wsgi-file = hello.py
master = true
processes = 4
threads = 2
uid = daemon
gid = daemon