<!DOCTYPE Article PUBLIC "-//Davenport//DTD DocBook V3.0//EN">

<Article id="modsnake">
<ArtHeader>
<Title>Mod Snake</Title>
<SubTitle>Installation And Usage</SubTitle>
<Author>
  <FirstName>Jon</FirstName>
  <Surname>Travis</Surname>
  <Affiliation>
    <address><email>jtravis@p00p.org</email></address>
  </Affiliation>    
</Author>
<PubDate>v0.5.0, 27 November 2000</PubDate>

<Abstract>
  <Para>
    Mod Snake is an Apache module written for the purpose to give Python
    developers the same power that C module writers have, but with the
    added flexibility and power that Python provides. This manual will
    discuss how to install mod_snake, as well as provide some basic
    examples of usage.
  </Para>
</Abstract>

</ArtHeader>

<Sect1 id="preqreqs"><Title>Prerequisites</Title>
  <Para>
    The main prerequisite for mod_snake is Python.  If it is not already
    installed, it can be obtained from the 
    <ULink URL="http://www.python.org">Python website</Ulink>.  Both
    Python 1.5.2, and 2.0 have been tested with mod_snake.
  </Para>
</Sect1>

<Sect1 id="supvers"><Title>Supported Apache Versions</Title>
  <Para>
    Mod Snake has been tested with Apache 1.3.12, 1.3.14, and 1.3.17.  
    Since Apache 2.0 is still in a pre-beta stage, its API is continually 
    changing.  Mod Snake development is currently matched against the 
    CVS versions of Apache 2.0.  Each version of mod_snake should coorespond 
    to a publicly released version of Apache, such as Apache 2.0a9.  
  </Para>
</Sect1>

<Sect1 id="installation"><Title>Installation</Title>
  <Para>
    For purposes of demonstration, in the subsequent sections, it
    will be assumed that Apache is installed under 
    <Filename class="Directory">/home/httpd/apache</Filename>, and
    that sources will be extracted under 
    <Filename class="Directory">/usr/local/src</Filename>.  Currently, 
    mod_snake is only supported as a DSO under Apache 2.0.  Under Apache 1.3
    it is supported both as a DSO and statically built in.
  </Para>

  <Sect2 id="installdso"><Title>Existing Apache Installation with DSO Support</Title>
    <Para>
      Apache has the ability to load shared modules on the fly.  These are 
      called Dynamically Shared Objects (<acronym>DSO</acronym>).  
      Mod Snake can be installed into an existing Apache installation which
      has DSO support with very little work.  To check if Apache was built with
      DSO support, execute the following command:
    </Para><Para>
<screen width="61">
bash$ <Command>/home/httpd/apache/bin/httpd -l | grep mod_so.c</Command>
</screen>
    </Para><Para>
      If mod_so.c is returned as output, Apache contains DSO support.  If not,
      see <xref linkend="installbuild">
    </Para>
    <Para>
      To install mod_snake, the following steps can be taken:
      <OrderedList>
      <ListItem><Para>
        Download mod_snake from <ULink URL="http://modsnake.sourceforge.net">
        modsnake.sourceforge.net</ULink>.
      </Para></ListItem>
      <ListItem><Para>
	Extract, configure, and install mod_snake.
<screen width="61">
bash$ <Command>gunzip -c mod_snake-0.5.0.tar.gz | tar -xvf -</Command>
bash$ <Command>cd mod_snake-0.5.0</Command>
bash$ <Command>./configure --with-apxs=/home/httpd/apache/bin/apxs</Command>
bash$ <Command>make install</Command>
</screen>
      </Para></ListItem>
      <ListItem><Para>
        Test the server.  See <xref linkend="quicktest">.
      </Para></ListItem>
      <ListItem><Para>
        All done!
      </Para></ListItem>
      </OrderedList>      
    </Para>
  </Sect2>

  <Sect2 id="installbuild"><Title>Building Apache with Mod Snake Support</Title>
    <Para>
      <OrderedList>
      <ListItem><Para>
        Download Apache from <ULink URL="http://www.apache.org/dist">
        www.apache.org</ULink>
      </Para></ListItem>
      <ListItem><Para>
	For <Emphasis>Apache 1.3</Emphasis> builds, extract, configure, 
	and build Apache:</Para><Para>
<screen width="61">
bash$ <Command>gunzip -c apache_1.3.17.tar.gz | tar -xvf -</Command>
bash$ <Command>cd apache_1.3.17</Command>
bash$ <Command>./configure --prefix=/home/httpd/apache --enable-module=so</Command>
bash$ <Command>make</Command>
</screen>
      </Para></ListItem>
      <ListItem><Para>
        For <Emphasis>Apache 2.0</Emphasis> builds, extract, configure, 
	and build Apache:</Para><Para>
<screen width="61">
bash$ <Command>gunzip -c apache_2.0a9.tar.gz | tar -xvf -</Command>
bash$ <Command>cd apache_2.0a9</Command>
bash$ <Command>./configure --enable-so --prefix=/home/httpd/apache</Command>
bash$ <Command>make</Command>
</screen>
      </Para></ListItem>
      <ListItem><Para>
        If a DSO build is desired, or Apache 2.0 is being used, execute 
        <Command>make install</Command>, and skip 
        to <xref linkend="installdso">.
      </Para></ListItem>
      <ListItem><Para>
        Extract, configure, and install mod_snake:</Para><Para>
<screen width="61">
bash$ <Command>gunzip -c mod_snake-0.5.0.tar.gz | tar -xvf -</Command>
bash$ <Command>cd mod_snake-0.5.0</Command>
bash$ <Command>./configure --with-apache=/usr/local/src/apache_1.3.17 --disable-dso</Command>
bash$ <Command>make install</Command>
</screen>
      </Para></ListItem>
      <ListItem><Para>
        Finally, reconfigure, build, and install Apache:</Para><Para>
<screen width="61">
bash$ <Command>cd /usr/local/src/apache_1.3.17</Command>
bash$ <Command>./configure --activate-module=src/modules/mod_snake/libmod_snake.a</Command>
bash$ <Command>make install</Command>
</screen>
      </Para></ListItem>
      </OrderedList>     
    </Para>
  </Sect2>

  <Sect2><Title>Mod_Snake Configuration</Title>
    <Para>
      Aside from needing to load mod_snake into Apache, other optional
      mod_snake libraries can be installed.  These standard libraries add
      a lot of functionality, such as embedded Python and accelerated CGIs.
    </Para><Para>
<screen width="61">
bash$ <Command>cd /usr/local/src/mod_snake-0.5.0</Command>
bash$ <Command>mkdir /home/httpd/mod_snake</Command>
bash$ <Command>cp -R snake_lib examples /home/httpd/mod_snake</Command>
</screen>
    </Para><Para>
      The following lines can be appended to the 
      <Filename>httpd.conf</Filename> file:
    </Para><Para>

<screen width="61">
SnakeModuleDir /home/httpd/mod_snake/snake_lib
SnakeModuleDir /home/httpd/mod_snake/examples
</screen>
    </Para><Para>
      These directives tell mod_snake to look in those directories for 
      modules to load.  To then load a module from either of those directories 
      (e.g. SnakeLib's SnakeEPy embedded Python processor), the 
      following directive can be added:
    </Para><Para>
<screen width="61">
SnakeModule mod_snake_epy.SnakeEPy
</screen>
    </Para><Para>
      This loads in the SnakeEPy module, and gives more directives that
      can be placed in the configuration file. To enable embedded Python
      processing for the main server, the following directives can be used:
    </Para><Para>
<screen width="61">
SnakeEPy on
</screen>
    </Para><Para>
      Other PyMods can be added and configured in similar ways. See the 
      documentation for the individual PyMods, or look at the mod_snake_api 
      documentation for the SnakeLib hooks. For more information about the 
      directives, see <xref linkend="directives">
    </Para>
  </Sect2>
  <Sect2 id="quicktest"><Title>Quick Test</Title>
    <Para>
      To quickly test to ensure that everything is working correctly, 
      the following lines can be added to the server configuration file 
      (<Filename>httpd.conf</Filename>):
<screen width="61">
SnakeModuleDir /usr/local/src/mod_snake-0.5.0/examples/tut
SnakeModule 01_hello_world.Hello_World
</screen>
    </Para><Para>
      Connect to the webserver and attempt to retrieve the 
      page ``/hello_world''. If the server responds with a page 
      containing, ``Hello world!'', then everything is working correctly.
    </Para>  
  </Sect2>
</Sect1>

<Sect1 id="directives">
<Title>Mod Snake Configuration Directives</Title>
  <Sect2><Title>SnakeModuleDir</Title>
    <CmdSynopsis>
      <Command>SnakeModuleDir</Command>
      <Arg Choice="req">Directory</Arg>    
    </CmdSynopsis>

    <bridgehead renderas=sect3>Arguments</bridgehead>
    <Para>
      <ItemizedList>
      <ListItem><Para>
        <Parameter>Directory</Parameter> - A path to look for mod_snake 
        PyMods.  This path can be relative to the server root.
      </Para></ListItem>
      </ItemizedList>
    </Para>

    <bridgehead renderas=sect3>Validity</bridgehead>
    <Para>
      This configuration directive is currently only available within the
      main configuration file outside &lt;Directory&gt; and &lt;Location&gt; 
      blocks.
    </Para>

    <bridgehead renderas=sect3>Description</bridgehead>
    <Para>
      Mod Snake needs to know where it can locate PyMods to load. This 
      directive also gives the modules loaded path information where they can 
      also find other modules (for instance examples to load within the 
      mod_snake_simple module.) 
    </Para>
  </Sect2>

  <Sect2><Title>SnakeModule</Title>
    <CmdSynopsis>
      <Command>SnakeModule</Command>
      <Arg Choice="req">module.class</Arg>    
      <Arg Choice="opt">profile</Arg>    
    </CmdSynopsis>

    <bridgehead renderas=sect3>Arguments</bridgehead>
    <Para>
      <ItemizedList>
      <ListItem><Para>
        <Parameter>module.class</Parameter> 
        - A module + class pair. The module is used the same
        as an 'import', and the class is a class within that module to 
        instantiate.  For instance, something like the following should be 
        able to be performed on the module.class specified, in a typical 
        Python interpreter.
<ProgramListing>
import module

x = module.class()
</ProgramListing>
      </Para></ListItem>

      <ListItem><Para>
        <Parameter>profile</Parameter> 
        - An optional argument which allows the loaded module to
        be profiled. All calls to the module will be profiled, and can be
        analyzed through the mod_snake_info SnakeLib module.  
        <Warning><Para>
          Extra installation work is required to use profiling -- see
          <xref linkend="profiling">
        </Para></Warning>
      </Para></ListItem>        
      </ItemizedList>
    </Para>

    <bridgehead renderas=sect3>Validity</bridgehead>
    <Para>
      This configuration directive is currently only available within the
      main configuration file outside &lt;Directory&gt; and &lt;Location&gt; 
      blocks.
    </Para>

    <bridgehead renderas=sect3>Description</bridgehead>
    <Para>
      This directive loads a PyMod into mod_snake. These can be active for
      virtual servers or the main server. 
    </Para>
  </Sect2>
</Sect1>

<Sect1 id="profiling"><Title>Profiling</Title>
  <Para>
    As of Python release 1.5.2, a broken version of profile.py is 
    shipped with Python.  According to tim_one at sourceforge.net (a
    Python developer), this is not a bug, but is something one should just
    'not do.'  A hack is provided with mod_snake to allow profiling to work.
    To use it, the following steps should be taken:

    <ItemizedList>
    <ListItem><Para>
      Copy the system's <Filename>profile.py</Filename> into the 
      <Filename class="Directory">snake_lib</Filename> directory,
      or another location where your Python modules are loaded. 
<Screen width="70">
bash$ <command>cp /usr/lib/python-2.0/profile.py /path/to/mod_snake/snake_lib/profile2.py</command>
</Screen>
    </Para></ListItem>

    <ListItem><Para>
      Change into the directory containing the new 
      <Filename>profile2.py</Filename> and patch it with the 
      <Filename>profile2.patch</Filename> which ships with mod_snake.
<Screen width="70">
bash$ <command>patch -p0 < profile2.patch</command>
</Screen>
    </Para></ListItem>
    </ItemizedList>
  </Para><Para>
    After those steps, profiling should work with all modules
  </Para>
</Sect1>

</Article>
