Changes between Version 9 and Version 10 of Gunicorn
- Timestamp:
- Feb 18, 2015, 10:27:08 PM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Gunicorn
v9 v10 3 3 [http://gunicorn.org/ Gunicorn] (Green Unicorn) is a Python WSGI HTTP Server for UNIX. It is a pre-fork worker model ported from Ruby's Unicorn project. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy. 4 4 5 5 1. Install gunicorn 6 6 7 7 Gunicorn is a Python project which lives on pypi, so we can use easy_install or pip to install gunicorn: 8 9 {{{ 10 $> pip install gunicorn 8 {{{#!sh 9 $> pip install gunicorn 11 10 }}} 12 11 13 I prefer to use a virtualenv (http://pypi.python.org/pypi/virtualenv), to not have to install gunicorn in a system wide fashion.12 I prefer to use a [http://pypi.python.org/pypi/virtualenv virtualenv], to not have to install gunicorn in a system wide fashion. 14 13 15 14 2. Write your wsgi file 16 15 17 Gunicorn is [http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface WSGI compliant], so we need a simple Python script `tracwsgi.py` that workas an entry point:16 Gunicorn is [http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface WSGI compliant], so we need a simple Python script `tracwsgi.py` that functions as an entry point: 18 17 19 {{{ 18 {{{#!python 20 19 import sys 21 20 import os … … 35 34 Now to test it: 36 35 37 {{{ 38 36 {{{#!sh 39 37 $> gunicorn -w2 tracwsgi:application -b 0.0.0.0:8000 40 38 2011-03-04 13:21:10 [14900] [INFO] Starting gunicorn 0.12.0 … … 42 40 2011-03-04 13:21:10 [14901] [INFO] Booting worker with pid: 14901 43 41 2011-03-04 13:21:10 [14902] [INFO] Booting worker with pid: 14902 44 45 42 }}} 46 43 47 In your web browser go to http:// yourIP:8000 and it should work.44 In your web browser go to http://localhost:8000 and it should work. 48 45 How it works: gunicorn is looking for a method called "application" in the tracwsgi.py file. 49 46 50 47 3. Configure Nginx 51 48 52 49 So, now we have Gunicorn running, but we will make it work with Nginx which will redirect Trac requests to gunicorn: 53 50 54 {{{ 55 51 {{{#!nginx 56 52 upstream trac_gunicorn { 57 53 server unix:///home/repos/projects/run/trac.sock; … … 79 75 } 80 76 } 81 82 77 }}} 83 78 84 '''Note''': Nginx will redirect requests to a UNIX socket. It can be a simple ip:port combination. It can be a multiple instance of gunicorn. Take a look in the Nginx documentation: 85 http://wiki.nginx.org/HttpUpstreamModule 79 '''Note''': Nginx will redirect requests to a UNIX socket. It can be a simple ip:port combination. It can be a multiple instance of gunicorn. Take a look in the [http://wiki.nginx.org/HttpUpstreamModule Nginx documentation]. 86 80 87 81 To add a basic or digest authentication, you'll have to provide a file as in the example above, created using htpasswd file. 88 82 To make gunicorn listen on a unix socket, add this option: 89 83 90 {{{ 91 84 {{{#!sh 92 85 gunicorn -w2 -b unix:///home/repos/projects/run/trac.sock tracwsgi:application 93 94 86 }}} 95 87 96 '''Note''': It seems the WSGI entry point does not handle the Digest or Basic http authentication. To ensure the authentication middleware is passed, you have to hack a little bit the tracwsgi.py:88 '''Note''': It seems the WSGI entry point does not handle the digest or basic http authentication. To ensure the authentication middleware is passed, you have to hack a little bit the `tracwsgi.py`: 97 89 98 {{{ 99 90 {{{#!python 100 91 import sys 101 92 import os … … 116 107 '''Note''': if using digest authentication you must create the authentication handler outside of the application def, otherwise the !DigestAuthentication object won't retain state between requests. 117 108 118 {{{ 109 {{{#!python 119 110 import sys 120 111 import os