Project

General

Profile

Apache » History » Version 1

Elmer de Looff, 2012-05-10 18:26
Apache documentation!

1 1 Elmer de Looff
h1. Requirements
2 1 Elmer de Looff
3 1 Elmer de Looff
For µWeb to work on Apache, you need the @mod_python@ package installed and enabled. On Debian (version 5 and up) this requires no effort beyond the straightforward installation of the package (here included with apache2 for ease of use):
4 1 Elmer de Looff
5 1 Elmer de Looff
<pre><code class="shell">
6 1 Elmer de Looff
sudo apt-get install apache2 libapache2-mod-python
7 1 Elmer de Looff
</code></pre>
8 1 Elmer de Looff
9 1 Elmer de Looff
The module automatically gets enabled and requires no further user-configuration.
10 1 Elmer de Looff
11 1 Elmer de Looff
h2. Apache config example
12 1 Elmer de Looff
13 1 Elmer de Looff
The second step is setting up an Apache config file. This is a basic configuration that sets up a VirtualHost for a specific hostname. This is the common scenario for Apache configurations, though many options are possible. We create the following file in Apache's @sites-available@ directory, and then enable it using @a2ensite@ (or an equivalent). This example is a copy of the router contained in the @uweb_info@ demo project:
14 1 Elmer de Looff
15 1 Elmer de Looff
<pre><code class="config">
16 1 Elmer de Looff
# /var/www/uweb_info/ should contain the uWeb router
17 1 Elmer de Looff
# This can be achieved by either copying the router (including optional
18 1 Elmer de Looff
# configuration file) to this directory, or creating a symbolic link where
19 1 Elmer de Looff
# /var/www/uweb_info points to the directory containing the router.
20 1 Elmer de Looff
21 1 Elmer de Looff
<VirtualHost *:80>
22 1 Elmer de Looff
    ServerName      uweb.local
23 1 Elmer de Looff
    DocumentRoot    /var/www/uweb_info/
24 1 Elmer de Looff
</VirtualHost>
25 1 Elmer de Looff
26 1 Elmer de Looff
<Directory "/var/www/uweb_info">
27 1 Elmer de Looff
    SetHandler        mod_python
28 1 Elmer de Looff
    PythonHandler     router
29 1 Elmer de Looff
    PythonPath        "['/home/elmer', '/home/elmer/devel'] + sys.path"
30 1 Elmer de Looff
    PythonDebug       on
31 1 Elmer de Looff
    PythonAutoReload  on
32 1 Elmer de Looff
</Directory>
33 1 Elmer de Looff
</code></pre>
34 1 Elmer de Looff
35 1 Elmer de Looff
The VirtualHost statement configures Apache to respond only to the given ServerName, so that it blends in nicely with a multi-website setup. The directory command contains the functional @mod_python@ statements:
36 1 Elmer de Looff
37 1 Elmer de Looff
|_.Directive |_.Meaning |
38 1 Elmer de Looff
| SetHandler | Instructs Apache to handle all requests for (and under) this directory to be handled through the @mod_python@ handler. |
39 1 Elmer de Looff
| PythonHandler | Name of the module that should route requests. Points to the location of the router (relative from the @DocumentRoot@). |
40 1 Elmer de Looff
| PythonPath | This is a Python statement that is necessary to be able to include the uWeb framework. If µWeb is installed in @'/home/john/devel/uweb/'@ then the added path should be @'/home/john/devel/'@ |
41 1 Elmer de Looff
| PythonDebug | Prints @mod_python@ debug reports in case there is an error causing µWeb itself to break. Like other debugging, it is suggested you turn this 'off' for production sites. |
42 1 Elmer de Looff
| PythonAutoReload | Automatically reloads python source files if and when they have changed. Not very reliable on the µWeb Framework stack. Default to 'on', suggested you turn this 'off' for production sites. |
43 1 Elmer de Looff
44 1 Elmer de Looff
h2. µWeb router example
45 1 Elmer de Looff
46 1 Elmer de Looff
After setting up Apache configuration, it's time to move the *[[router]]* file to the appropriate location. For the example above, the router should be placed in @'/var/www/uweb_info/router.py'@ (of course, '.pyc' and '.pyo' would also work).
47 1 Elmer de Looff
48 1 Elmer de Looff
<pre><code class="python">
49 1 Elmer de Looff
#!/usr/bin/python
50 1 Elmer de Looff
"""A uWeb demonstration project."""
51 1 Elmer de Looff
52 1 Elmer de Looff
# uWeb Framework
53 1 Elmer de Looff
import uweb
54 1 Elmer de Looff
55 1 Elmer de Looff
# Project's controller
56 1 Elmer de Looff
from uweb.uweb_info import pages
57 1 Elmer de Looff
58 1 Elmer de Looff
# uWeb configuration variables
59 1 Elmer de Looff
CONFIG = 'example.conf'
60 1 Elmer de Looff
PACKAGE = 'uweb_info'
61 1 Elmer de Looff
62 1 Elmer de Looff
PAGE_CLASS = pages.PageMaker
63 1 Elmer de Looff
ROUTES = (
64 1 Elmer de Looff
    ('/static/(.*)', 'Static'),
65 1 Elmer de Looff
    ('/(broken.*)', 'FourOhFour'),
66 1 Elmer de Looff
    ('/haltandcatchfire', 'MakeFail'),
67 1 Elmer de Looff
    ('/json', 'Json'),
68 1 Elmer de Looff
    ('/text', 'Text'),
69 1 Elmer de Looff
    ('/redirect/(.*)', 'Redirect'),
70 1 Elmer de Looff
    ('/OpenIDLogin', '_OpenIdInitiate'),
71 1 Elmer de Looff
    ('/OpenIDValidate', '_OpenIdValidate'),
72 1 Elmer de Looff
    ('/ULF-Challenge', '_ULF_Challenge'),
73 1 Elmer de Looff
    ('/ULF-Login', '_ULF_Verify'),
74 1 Elmer de Looff
    ('/(.*)', 'Index'))
75 1 Elmer de Looff
76 1 Elmer de Looff
uweb.ServerSetup(apache_logging=True)
77 1 Elmer de Looff
</code></pre>
78 1 Elmer de Looff
79 1 Elmer de Looff
Compared to the *[[request router]]*, there are a few things different here (broadly similar to Standalone):
80 1 Elmer de Looff
81 1 Elmer de Looff
# @CONFIG@ is used to indicate the path for the configuration file, if there is any. This is a path to the configuration file relative from the router location. In this router, the configuration file is a file named example.conf in the same directory as the router. If there is no configuration file, this variable should be omitted.
82 1 Elmer de Looff
# @PACKAGE@ is used to indicate the package name of the project. The package name is used to group logfiles by. Logfiles will be automatically created and written to either. If no explicit @PACKAGE@ variable is set, it defaults to @'uweb_project'@.
83 1 Elmer de Looff
# On the @uweb.ServerSetup@ call, there is an optional argument @'apache_logging'@ that controls application logging (enabled by default). This refers to the explicit logging done inside µWeb using @logging.log*@ methods. For this to work, there must be a writable path for µWeb either
84 1 Elmer de Looff
#* on @'/var/log/underdark/'@ (the latter is recommended for Apache mode). This creates a subdirectory after the @PACKAGE@ name, and in there creates SQLite logfiles based on the router name.
85 1 Elmer de Looff
#* on the home-directory of the apache user, in a subdirectory @.underdark@ (this directory will be created if it doesn't exist). From here, a directory will be created after the @PACKAGE@ name, and in a @'logs'@ directory, the various SQLite logfiles will be stored.
86 1 Elmer de Looff
87 1 Elmer de Looff
h2. Reload and run
88 1 Elmer de Looff
89 1 Elmer de Looff
After reloading the Apache configuration, the site will be available when we browse to http://uweb.local (assuming the path is resolved by DNS, or added to the machine's @hosts@ file).