1.3. Proxy mode

1.3.1 Introduction

In this mode, Apache is used as a proxy between the client and the built-in server. You have to configure it so that it sends all the requests to this server running in the background. We will suppose that you start Apache on port 80 and the built-in server on port 8081 (for security reasons, on Linux Karrigell should be started on a port above 1024 and not as root)

I copy most of this section from Remi Delon's CherryPy documentation : http://www.cherrypy.org/wiki/BehindApache

1.3.2 Editing httpd.conf

First you must edit the Apache config file httpd.conf

In the Dynamic Shared Object (DSO) section, uncomment the lines

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so

Set the DocumentRoot option to the Root Directory of your Karrigell distribution

Add the following lines to enable proxy mode :

ProxyRequests On
<Proxy *>
    Order allow,deny
    Deny from none
    Allow from all
</Proxy>

mod_rewrite is used to ask Apache to send the requests to the built-in server. This module parses the original url and changes it according to regular expressions

Add these lines in the section <Directory > for the Document Root :

RewriteEngine On
RewriteCond  %{SCRIPT_FILENAME} !autostart\.cgi$
RewriteRule ^(.*) http://localhost:8081/$1 [P]
ErrorDocument 502 /cgi-bin/autostart.cgi
ErrorDocument 503 /cgi-bin/autostart.cgi
The main functionality is provided by the RewriteRule : it tells Apache to rewrite all the urls to an absolute url corresponding to the built-in server running on port 8081 and use the proxy mode to pass the request

If the built-in server is not running, an HTTP error 502 or 503 is returned ; the last lines tell Apache to call the script autostart.cgi in this case. The second line prevents Apache to use the RewriteRule for this specific script (otherwise it would enter an infinite loop)

1.3.3 Editing autostart.cgi

autoscript.cgi is a short script, looking like this on Linux/Unix :

#!/usr/bin/python
print "Content-type: text/html\r\n\r\n"
print """<html><head><META HTTP-EQUIV="Refresh" CONTENT="10; 
      URL=/"></head><body>Restarting site ...<a href="/">click 
      here<a></body></html>"""
import os
import sys
os.setpgid(os.getpid(), 0)
server_script = r"c:\Karrigell\20090614\Karrigell.py"
conf_dir = os.path.dirname(server_script)
line = sys.executable + ' '+server_script +' %s &' %conf_dir
try:
    os.system(line)
except:
    import traceback
    traceback.print_exc(file=out)

What you have to adapt is

  • the location of the Python interpreter on the first line (#!python on Windows)
  • the path to Karrigell.py on the line "server_script"

This script must be put in the cgi-bin directory defined in httpd.conf (normally by the ScriptAlias directive). Don't forget to set the "execution" mode for it

1.3.4 Starting the server

The server must be configured with these values in server_config.py:

  • port = 8081
  • silent = True

Start the built-in web server on port 8081 and start Apache. This should be enough to get it going. Depending on what ServerName is set to (use 'localhost' for testing), enter the URL of your server into the URL bar of a web browser and Karrigell/Apache should serve web pages like normal Karrigell.