| 1 |
== link:index.html[Index] -> link:cookbook.html[Cookbook] |
|---|
| 2 |
|
|---|
| 3 |
Cookbook: Setting up Django |
|---|
| 4 |
--------------------------- |
|---|
| 5 |
|
|---|
| 6 |
Django is a fantastic high-level Python Web framework that can run |
|---|
| 7 |
nicely with Cherokee and either the |
|---|
| 8 |
link:modules_handlers_scgi.html[SCGI] or |
|---|
| 9 |
link:modules_handlers_fcgi.html[FastCGI] handlers. |
|---|
| 10 |
|
|---|
| 11 |
To properly set up Cherokee to use Django you will need a working |
|---|
| 12 |
Django framework, which is not difficult at all to get ready. The |
|---|
| 13 |
details vary from system to system. You can refer to the official |
|---|
| 14 |
project's documentation for more information on |
|---|
| 15 |
link:http://www.djangoproject.com/documentation/install[how to install |
|---|
| 16 |
Django]. |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
On Debian based systems this will be enough: |
|---|
| 20 |
---- |
|---|
| 21 |
# apt-get install python-django python-flup |
|---|
| 22 |
---- |
|---|
| 23 |
|
|---|
| 24 |
You will need Flup because it implements the standard interface |
|---|
| 25 |
between Python Web applications and Web servers, so you will be using |
|---|
| 26 |
it to run your web application either as FastCGI or SCGI. |
|---|
| 27 |
|
|---|
| 28 |
Once you are done with that, you must deploy your Django project: |
|---|
| 29 |
|
|---|
| 30 |
---- |
|---|
| 31 |
$ cd /var/www |
|---|
| 32 |
$ django-admin startproject example |
|---|
| 33 |
---- |
|---|
| 34 |
|
|---|
| 35 |
This will create the basic structure into a new directory called |
|---|
| 36 |
`example`. Now you are ready to configure Cherokee. You only need to |
|---|
| 37 |
know how to spawn the FastCGI or SCGI, which is done with a script |
|---|
| 38 |
provided by your project called `manage.py`. |
|---|
| 39 |
|
|---|
| 40 |
It can be run on a TCP port or on a Unix socket. In our example we |
|---|
| 41 |
will be launching it as threaded server on a TCP port with SCGI |
|---|
| 42 |
protocol. This is acomplished with the following command, which is |
|---|
| 43 |
what we will have to set up in `cherokee-admin`. |
|---|
| 44 |
|
|---|
| 45 |
---- |
|---|
| 46 |
./manage.py runfcgi method=threaded host=127.0.0.1 port=3033 protocol=scgi |
|---|
| 47 |
---- |
|---|
| 48 |
|
|---|
| 49 |
The process is fairly simple. Set up a new rule for this new path and |
|---|
| 50 |
manage it with the SCGI handler. If you wanted to use the FastCGI |
|---|
| 51 |
handler instead you would only have to omit the last parameter and |
|---|
| 52 |
FastCGI would be used by default. The configuration of the handler is |
|---|
| 53 |
exactly the same for SCGI and FastCGI. |
|---|
| 54 |
|
|---|
| 55 |
Once you have created the new rule for your `/var/www/example` |
|---|
| 56 |
directory, choose the desired handler and use the following |
|---|
| 57 |
configuration. |
|---|
| 58 |
|
|---|
| 59 |
.Common CGI options |
|---|
| 60 |
Under `Common CGI options` make sure to check the `Error handler` box and |
|---|
| 61 |
uncheck `Check file`. This is to prevent possible errors with the |
|---|
| 62 |
`INFO_PATH` generation that can happen when an application, in this |
|---|
| 63 |
case 'Django', manages the whole subtree. This is mentioned in the |
|---|
| 64 |
link:modules_handlers_cgi.html[Common CGI] section of the |
|---|
| 65 |
documentation. It is a good idea to enable the `Error handler` |
|---|
| 66 |
checkbox since it will help you determine if an error is associated |
|---|
| 67 |
with your Django application or with Cherokee. This, however, is not |
|---|
| 68 |
required. |
|---|
| 69 |
|
|---|
| 70 |
image::media/images/cookbook_django_common.png[Common CGI options] |
|---|
| 71 |
|
|---|
| 72 |
.SCGI specific |
|---|
| 73 |
Under `SCGI specific` make sure to add the hosts providing the |
|---|
| 74 |
service. This is done by adding one or more information sources. |
|---|
| 75 |
|
|---|
| 76 |
image::media/images/cookbook_django_infosources.png[Information Sources] |
|---|
| 77 |
|
|---|
| 78 |
Note that you will have to manually launch the `spawner` if |
|---|
| 79 |
you use a `Remote host` as `Information source` instead of a `Local |
|---|
| 80 |
interpreter`. |
|---|
| 81 |
|
|---|
| 82 |
You will simply have to add as many sources as needed, for instance |
|---|
| 83 |
our example uses one nicknamed `django1`, created as *local |
|---|
| 84 |
interpreter* with these parameters on port 3033. |
|---|
| 85 |
|
|---|
| 86 |
[grid="rows"] |
|---|
| 87 |
`~~~~~~~~~~~~~~`~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 88 |
Host , Interpreter |
|---|
| 89 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 90 |
localhost:3033 , `/var/www/example/manage.py runfcgi method=threaded ` \ |
|---|
| 91 |
`host=127.0.0.1 port=3033 protocol=scgi` |
|---|
| 92 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 93 |
|
|---|
| 94 |
You can set up as many hosts as desired and Cherokee will balance the |
|---|
| 95 |
load among them. |
|---|
| 96 |
|
|---|
| 97 |
Once everything is done you can check if Django is really |
|---|
| 98 |
working. Simply navigate to the path configured by your rule, |
|---|
| 99 |
http://localhost/example for instance, and you should see some notes |
|---|
| 100 |
about your recently created project. |
|---|
| 101 |
|
|---|
| 102 |
image::media/images/cookbook_django.png[Django example] |
|---|
| 103 |
|
|---|
| 104 |
********************************************************************* |
|---|
| 105 |
Should you wish to deploy your Django application directly instead of |
|---|
| 106 |
through `manage.py` this could be easily achieved. Just remember to |
|---|
| 107 |
daemonize the script used to launch the interpreter in the relevant |
|---|
| 108 |
`information source`. |
|---|
| 109 |
|
|---|
| 110 |
This is done adding the appropriate parameter to the `runfastcgi` |
|---|
| 111 |
method of `django.core.servers.fastcgi` in your script. |
|---|
| 112 |
|
|---|
| 113 |
.Using a unix socket: |
|---|
| 114 |
---- |
|---|
| 115 |
from django.core.servers.fastcgi import runfastcgi |
|---|
| 116 |
|
|---|
| 117 |
runfastcgi(method="threaded", daemonize="true", maxrequests=5, |
|---|
| 118 |
protocol="scgi", socket="/tmp/cherokee-django.socket", |
|---|
| 119 |
pidfile="/tmp/cherokee-django.pid") |
|---|
| 120 |
---- |
|---|
| 121 |
|
|---|
| 122 |
.Using a host:port configuration: |
|---|
| 123 |
---- |
|---|
| 124 |
from django.core.servers.fastcgi import runfastcgi |
|---|
| 125 |
|
|---|
| 126 |
runfastcgi(method="threaded", daemonize="true", maxrequests=5, |
|---|
| 127 |
protocol="scgi", host="127.0.0.1", port=3033) |
|---|
| 128 |
---- |
|---|
| 129 |
********************************************************************* |
|---|