Example configurations (0.9.6 version)
Django on Apache
Put a file (named django_wsgi.py) in your project directory:
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
Run the uWSGI server on a TCP socket
uwsgi --socket 127.0.0.1:3031 --pythonpath <path to your project directory> -w django_wsgi
or use the xml configuration:
<uwsgi> <socket>127.0.0.1:3031</socket> <pythonpath>path to your django project directory</pythonpath> <module>django_wsgi</module> </uwsgi>
uwsgi -x django.xml
Finally, add <Location> directive to apache configuration
<Location />
SetHandler uwsgi-handler
uWSGISocket 127.0.0.1:3031
</Location>
Django again but with fun
Run without configuration files or wsgi module (beware of pythonpath):
./uwsgi -s 127.0.0.1:3031 -M -p 4 --env DJANGO_SETTINGS_MODULE=djtest.settings -w "django.core.handlers.wsgi:WSGIHandler()"
Run with only xml
<uwsgi> <socket>127.0.0.1:3031</socket> <master/> <processes>4</processes> <env>DJANGO_SETTINGS_MODULE=djtest.settings</env> <module>django.core.handlers.wsgi:WSGIHandler()</module> </uwsgi>
./uwsgi -x mydjango.xml
...or with a .ini file
[uwsgi] socket = 127.0.0.1:3031 master = true processes = 4 env = DJANGO_SETTINGS_MODULE=djtest.settings module = django.core.handlers.wsgi:WSGIHandler()
./uwsgi --ini mydjango.ini
Pylons and nginx the happy way
add uWSGI configuration directly in your development.ini/production.ini or whatever config file you are using for Pylons
[uwsgi] socket = /tmp/i_am_a_unix_socket.sock master = true processes = 1
./uwsgi --ini-paste <path_to_your_pylons-configuration_file>
finally add a location to nginx.conf
location / {
include uwsgi_params
uwsgi_pass unix:///tmp/i_am_a_unix_socket.sock
}
Trac on apache in a sub-uri
Put this wsgi script (call it mytrac.py) in a dir in the pythonpath (or add the dir with the <pythonpath> directive)
import os
import trac.web.main
os.environ['TRAC_ENV'] = '<absolute_path_to_trac_project>'
applications = {'/trac':trac.web.main.dispatch_request}
Run the server
uwsgi -s /tmp/uwsgi.sock -C -w mytrac
Modify apache
<Location /trac>
SetHandler uwsgi-handler
uWSGISocket /tmp/uwsgi.sock
</Location>
Better trac (on /)
uwsgi -s /tmp/uwsgi.sock -C -w trac.web.main:dispatch_request --env TRAC_ENV=<absolute_path_to_trac_project>
Django and Mercurial just married
import uwsgi
from mercurial import demandimport; demandimport.enable()
from mercurial.hgweb.hgwebdir_mod import hgwebdir
hgapp = hgwebdir('hgweb.config')
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings'
import django.core.handlers.wsgi
djangoapp = django.core.handlers.wsgi.WSGIHandler()
uwsgi.applications = {'':djangoapp, '/hg':hgapp}
Web2Py (2uWSGI)
This is the "one-man-band" of all python frameworks.
Unzip it in your directory of choice and...
./uwsgi --pythonpath <path_to_your_web2py_dir> --module wsgihandler -s /tmp/we2py.sock
or
<uwsgi> <pythonpath>path_to_your_web2py_dir</pythonpath> <module>wsgihandler</module> <socket>/tmp/web2py.sock</socket> <master/> <processes>8</processes> <memory-report/> </uwsgi>
./uwsgi -x myweb2py.xml
3 Web2Py instances, one XML configuration
(please do not get too excited about the next lines)
<foo> <uwsgi id="app1"> <pythonpath>app1/web2py</pythonpath> <module>wsgihandler</module> <master/> <processes>4</processes> <socket>/tmp/web2py_app1.sock</socket> </uwsgi> <uwsgi id="app2"> <pythonpath>app2/web2py</pythonpath> <module>wsgihandler</module> <master/> <processes>4</processes> <socket>/tmp/web2py_app2.sock</socket> </uwsgi> <uwsgi id="app3"> <pythonpath>app3/web2py</pythonpath> <module>wsgihandler</module> <master/> <processes>4</processes> <socket>/tmp/web2py_app3.sock</socket> </uwsgi> </foo>
Now run 3 instances of uwsgi
./uwsgi -x megaconf.xml:app1 ./uwsgi -x megaconf.xml:app2 ./uwsgi -x megaconf.xml:app3
Ok, i have heard you... "Why the fu*k i cannot use 3 different xml files ?"
You can, but then you cannot do this magic:
<uwsgi> <pythonpath id="app1">app1/web2py</pythonpath> <pythonpath id="app2">app2/web2py</pythonpath> <pythonpath id="app3">app3/web2py</pythonpath> <module>wsgihandler</module> <master/> <processes>4</processes> <socket id="app1">/tmp/web2py_app1.sock</socket> <socket id="app2">/tmp/web2py_app2.sock</socket> <socket id="app3">/tmp/web2py_app3.sock</socket> </uwsgi>
A little note:
We have used the <foo> root tag for multiple <uwsgi> blocks. This can be every name you want. This will allows you to put <uwsgi> configurations on other xml files.
Flask deploy
Flask is an amazing microframework. Deploying it on uWSGI is super-fast/super-easy
(call it myapp.py)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run()
./uwsgi -s /tmp/mysock.sock --module myapp --callable app
or
./uwsgi -s /tmp/mysock.sock -w myapp:app
UNIX gurus, please sit down
No description, only code for real men
#!/bin/sh export UWSGI_SOCKET=/tmp/uwsgi.sock export UWSGI_MODULE=trac.web.main export UWSGI_CALLABLE=dispatch_request export UWSGI_MASTER=1 export UWSGI_PROCESSES=8 export UWSGI_MEMORY_REPORT=1 export UWSGI_HARAKIRI=30 export TRAC_ENV=/tmp/mytrac exec /usr/bin/uwsgi
The shebang mess
On systems like FreeBSD or OSX you can do funny things like this:
#!/usr/bin/uwsgi --socket :3031 --callable app
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run()
but on (for example) Linux systems you will get:
uwsgi: unrecognized option '--socket :3031 --callable app'
sadly there is no easy fix :(
Use the embedded HTTP server to test your work on uWSGI
Oh yes, you do not need to have a real webserver on your development/testing machine:
./uwsgi --http 127.0.0.1:8080 --module myapp --callable app
this will run myapp:app on http port 8080
Hey DO NOT USE THE EMBEDDED HTTP SERVER IN PRODUCTION ENVIRONMENTS
