Installation — Mod_python v3.5.0+git20211031 documentation

### Navigation

- index
- modules |
- next |
- previous |
- Mod_python v3.5.0+git20211031 documentation »

# Installation

Note

By far the best place to get help with installation and other issues is the mod_python mailing list. Please take a moment to join the mod_python mailing list by sending an e-mail with the word “subscribe” in the subject to mod_python-request@modpython.org or visit the mod_python mailing list page

## Prerequisites

In the ideal case your Operating System provides a pre-packaged version of mod_python. If not, you will need to compile it yourself. This version of mod_python requires:

- Python 2 (2.6 and up) or Python 3 (3.3 and up).
- Apache 2.2 or later. Apache 2.4 is highly recommended over 2.2.

In order to compile mod_python you will need to have the include files for both Apache and Python, as well as the Python library installed on your system. If you installed Python and Apache from source, then you already have everything needed. However, if you are using pre-packaged software then you may need to install the “development” packages which contain the include files and libraries necessary to compile mod_python. Please check your OS documentation for specifics. (Hint: look for packages named python-devel or python-dev and apache-devel or apache-dev or httpd-dev, etc.).

## Compiling

### Running `./configure`

The `./configure` script will analyze your environment and create custom Makefiles particular to your system. Aside from all the standard autoconf stuff, `./configure` does the following:

- Finds out whether a program called apxs is available. This program is part of the standard Apache distribution, and is required for compilation.

  You can manually specify the location of apxs by using the with-apxs option, e.g.:

      $ ./configure --with-apxs=/usr/local/apache/bin/apxs

  It is recommended that you specify this option.

- Checks your Python version and attempts to figure out where `libpython` is by looking at various parameters compiled into your Python binary. By default, it will use the python program found in your `PATH`.

  If the first Python binary in the path is not suitable or not the one desired for mod_python, you can specify an alternative location with the with-python option, e.g.:

      $ ./configure --with-python=/usr/local/bin/python2.3

- Sets the directory for the apache mutex locks (if the mutex mechanism chosen by APR requires one).

  Note: mutex locks are used only by mod_python Sessions and PSP (which maintains a Session implicitly). If you’re not using mod_python Sessions or PSP, then this setting should not matter.

  Default is `/tmp`. The directory must exist and be writable by the owner of the apache process.

  Use with-mutex-dir option, e.g:

      $ ./configure --with-mutex-dir=/var/run/mod_python

  The mutex directory can also be specified at run time using PythonOption `mod_python.mutex_directory`. See Configuring Apache.

  New in version 3.3.0

- Sets the maximum number of mutex locks reserved by mod_python.

  Note: mutex locks are used only by mod_python Sessions and PSP (which maintains a Session implicitly). If you’re not using mod_python Sessions or PSP, then this setting should not matter.

  The mutexes used for locking are a limited resource on some systems. Increasing the maximum number of locks may increase performance when using session locking. The default is 8. A reasonable number for higher performance would be 32. Use with-max-locks option, e.g:

      $ ./configure --with-max-locks=32

  The number of locks can also be specified at run time using PythonOption `mod_python.mutex_locks`. See Configuring Apache.

  New in version 3.2.0

- Attempts to locate flex and determine its version. If flex cannot be found in your `PATH` configure will fail. If the wrong version is found configure will generate a warning. You can generally ignore this warning unless you need to re-create `src/psp_parser.c`.

  The parser used by psp (See psp – Python Server Pager) is written in C generated using flex. (This requires a reentrant version of flex, 2.5.31 or later).

  If the first flex binary in the path is not suitable or not the one desired you can specify an alternative location with the option:with-flex: option, e.g:

      $ ./configure --with-flex=/usr/local/bin/flex

  New in version 3.2.0

### Running `make`

- To start the build process, simply run:

      $ make

## Installing

Running `make install`

- This part of the installation in most cases needs to be done as root:

      $ sudo make install

  - This will copy the mod_python library (`mod_python.so`) into your Apache `libexec` or `modules` directory, where all the other modules are.
  - Lastly, it will install the Python libraries in `site-packages` and compile them.

Note

If you wish to selectively install just the Python libraries or the DSO (mod_python.so) (which may not always require superuser privileges), you can use the following make targets: install_py_lib and install_dso.

## Configuring Apache

- LoadModule

  You need to configure Apache to load the module by adding the following line in the Apache configuration file, usually called `httpd.conf` or `apache.conf`:

      LoadModule python_module libexec/mod_python.so

  The actual path to mod_python.so may vary, but make install should report at the very end exactly where mod_python.so was placed and how the `LoadModule` directive should appear.

- See Testing below for more basic configuration parameters.

## Testing

1. Make a directory that would be visible on your web site, e.g. `htdocs/test`.

2. Add the following configuration directives to the main server config file:

       <Directory /some/directory/htdocs/test>
           AddHandler mod_python .py
           PythonHandler mptest
           PythonDebug On
       </Directory>

   (Substitute `/some/directory` above for something applicable to your system, usually your Apache ServerRoot)

   This configuration can also be specified in an `.htaccess` file. Note that `.htaccess` configuration is typically disabled by default, to enable it in a directory specify `AllowOverride` with at least `FileInfo`.

3. This causes all requests for URLs ending in `.py` to be processed by mod_python. Upon being handed a request, mod_python looks for the appropriate python handler to handle it. Here, there is a single `PythonHandler` directive defining module `mptest` as the python handler to use. We’ll see next how this python handler is defined.

4. At this time, if you made changes to the main configuration file, you will need to restart Apache in order for the changes to take effect.

5. Edit `mptest.py` file in the `htdocs/test` directory so that is has the following lines (be careful when cutting and pasting from your browser, you may end up with incorrect indentation and a syntax error):

       from mod_python import apache

       def handler(req):
           req.content_type = 'text/plain'
           req.write("Hello World!")
           return apache.OK

6. Point your browser to the URL referring to the `mptest.py`; you should see `'Hello World!'`. If you didn’t - refer to the troubleshooting section next.

7. Note that according to the configuration written above, you can point your browser to any URL ending in .py in the test directory. Therefore pointing your browser to `/test/foobar.py` will be handled exactly the same way by `mptest.py`. This is because the code in the `handler` function does not bother examining the URL and always acts the same way no matter what the URL is.

8. If everything worked well, move on to Chapter Tutorial.

## Troubleshooting

There are a few things you can try to identify the problem:

- Carefully study the error output, if any.

- Check the server error log file, it may contain useful clues.

- Try running Apache from the command line in single process mode:

      ./httpd -X

  This prevents it from backgrounding itself and may provide some useful information.

- Beginning with mod_python 3.2.0, you can use the mod_python.testhandler to diagnose your configuration. Add this to your `httpd.conf` file:

      <Location /mpinfo>
        SetHandler mod_python
        PythonHandler mod_python.testhandler
      </Location>

  Now point your browser to the `/mpinfo` URL (e.g. `http://localhost/mpinfo`) and note down the information given. This will help you reporting your problem to the mod_python list.

- Ask on the mod_python list. Please make sure to provide specifics such as:

  - mod_python version.
  - Your operating system type, name and version.
  - Your Python version, and any unusual compilation options.
  - Your Apache version.
  - Relevant parts of the Apache config, .htaccess.
  - Relevant parts of the Python code.

### Navigation

- index
- modules |
- next |
- previous |
- Mod_python v3.5.0+git20211031 documentation »