Compiling Apache mod_python module for Centos 7 / Redhat 7 11


Introduction.

Check_mk continues to use to date (December 2014) the old python module for Apache mod_python than is discontinued now. It is a module that started to discard distributions for the more update mod_wsgi module. The more new versions of the most popular linux distros do not include this module so if you need it you’ll have to compile it yourself.
This may change so before compiling looking if there is already a package for your linux distro.
Tests in this posts were made with with SELINUX disabled. Careful with Firewalld so you can access your 80 port too.

Compiling and Installing

To install mod_python in Apache we need to compile the module. We are going to install apache sources first (It need a binary included in these -apxs-). We also need the python sources and generic development tools.

yum install httpd httpd-devel python-devel
yum groupinstall "Development Tools"

Download module from http://modpython.org/ . Version 3.5.x. is required.
Untar and start the compilation process.

tar zxfv mod_python-3.5.0.tg
cd mod_python-3.5.0
./configure -with-apxs=/usr/bin/apxs

Before “make” we will modify two files. There is more information at the end of the post in “References”.
src/Makefile. Add the marked lines.

version.c:
 @MP_GIT_SHA=$$(git describe --always); \
 echo > version.c ; \
 echo "/* THIS FILE IS AUTO-GENERATED BY Makefile */" >> version.c ; \
 echo "#include \"mp_version.h\"" >> version.c ; \
 include/mod_python.h include/psp_flex.h include/psp_parser.h \
 include/requestobject.h include/tableobject.h include/connobject.h \
 include/finfoobject.h include/hlistobject.h include/mp_version.h \
 include/_pspmodule.h include/psp_string.h include/serverobject.h \
 include/util.h \
 echo "const char * const mp_git_sha = \"$${MP_GIT_SHA}\";" >> version.c ; \

dist/version.sh. Coment / add the marked lines

#!/bin/sh
MPV_PATH="`dirname $0`/../src/include/mp_version.h"
MAJ=`awk '/MP_VERSION_MAJOR/ {print $3}' $MPV_PATH`
MIN=`awk '/MP_VERSION_MINOR/ {print $3}' $MPV_PATH`
PCH=`awk '/MP_VERSION_PATCH/ {print $3}' $MPV_PATH`
#GIT=`git describe --always`
#echo $MAJ.$MIN.$PCH-$GIT
echo $MAJ.$MIN.$PCH-

Now we’re going to finish compiling module.

make
make install

Testing mod_python module.

To test mod_python we simply follow the instructions on the next page to perform a test using our default Apache structure. We add to our apache directory:

mkdir /var/www/html/test
chown apache.apache /var/www/html/test

Create a new configuration file e.g. /etc/httpd/conf.d/test_mod_python.conf to handle .py files

# FILE /etc/httpd/conf.d/test_mod_python.conf
<Directory /var/www/html/test>
  AddHandler mod_python .py
  PythonHandler mptest
  PythonDebug On
</Directory>

Create a file /var/www/html/test/mptest.py for testing.

# FILE /var/www/html/test/mptest.py
from mod_python import apache
def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello World!")
    return apache.OK

 

Activate Apache module in python. We create a file e.g. /etc/httpd/conf.modules.d/01-mod_python.conf

# FILE /etc/httpd/conf.modules.d/01-mod_python.conf
LoadModule python_module modules/mod_python.so

Restart Apache.

systemctl restart httpd.service

Test to to access the URI to obtain a “Hello Word!” -> http://server/test/mptest.py

References.

If the source files are not modified as indicated mod_python will not work and we find problems in the Apache error_log the following type:

[Sun Dec 07 10:31:13.539980 2014] [:error] [pid 25070] make_obcallback: could not import mod_python.apache.\n
Traceback (most recent call last):
 File "/usr/lib64/python2.7/site-packages/mod_python/__init__.py", line 25, in 
 from . import version
 File "/usr/lib64/python2.7/site-packages/mod_python/version.py", line 3
 version = "fatal: Not a git repository (or any of the parent directories): .git
 ^
SyntaxError: EOL while scanning string literal
[Sun Dec 07 10:31:13.540476 2014] [:error] [pid 25070] make_obcallback: Python path being used "['/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']".
[Sun Dec 07 10:31:13.540593 2014] [:error] [pid 25070] get_interpreter: no interpreter callback found.
[Sun Dec 07 10:31:13.540752 2014] [:error] [pid 25070] [client 192.168.164.1:55805] python_handler: Can't get/create interpreter.

In the post solution in the following post link is used.


Leave a comment

Your email address will not be published. Required fields are marked *

*

11 thoughts on “Compiling Apache mod_python module for Centos 7 / Redhat 7

  • Dave

    Thanks!
    Two errors though:
    chown apache.apache /var/www/test
    should be
    chown apache.apache /var/www/html/test

    and more importantly, the /etc/httpd/conf.d/test_mod_python.conf file needs to be wrapped in some code that didn’t show up on the website. Download the PDF to see it, it is the difference between the test working and not working.

  • sanka

    httpd.service failed centos 7
    httpd.service failed because the control process exited with error code. See “systemctl status httpd.service” and “journalctl -xe” for details

    [root@localhost mod_python-3.5.0]# systemctl status httpd.service -l
    ● httpd.service – The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    Active: failed (Result: exit-code) since Wed 2016-05-18 00:22:33 IST; 1min 44s ago
    Docs: man:httpd(8)
    man:apachectl(8)
    Process: 44347 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
    Process: 36283 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
    Process: 44346 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
    Main PID: 44346 (code=exited, status=1/FAILURE)

    May 18 00:22:33 localhost.localdomain systemd[1]: Starting The Apache HTTP Server…
    May 18 00:22:33 localhost.localdomain httpd[44346]: httpd: Syntax error on line 56 of /etc/httpd/conf/httpd.conf: Syntax error on line 2 of /etc/httpd/conf.modules.d/01-mod_python.conf: Cannot load modules/mod_python.so into server: /etc/httpd/modules/mod_python.so: undefined symbol: mp_version_component
    May 18 00:22:33 localhost.localdomain systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
    May 18 00:22:33 localhost.localdomain kill[44347]: kill: cannot find process “”
    May 18 00:22:33 localhost.localdomain systemd[1]: httpd.service: control process exited, code=exited status=1
    May 18 00:22:33 localhost.localdomain systemd[1]: Failed to start The Apache HTTP Server.
    May 18 00:22:33 localhost.localdomain systemd[1]: Unit httpd.service entered failed state.
    May 18 00:22:33 localhost.localdomain systemd[1]: httpd.service failed.
    [root@localhost mod_python-3.5.0]# ^C
    [root@localhost mod_python-3.5.0]

  • Murali

    I followed the document and managed to install mod_python under Centos 7.0 VM. However when I tried to execute the test script through the browser ( http://localhost/test/mptest.py ), I get error as below.

    Mod_python error: “PythonHandler mptest”
    Traceback (most recent call last):
      File “/usr/lib64/python2.7/site-packages/mod_python/apache.py”, line 353, in HandlerDispatch
        log=debug)
      File “/usr/lib64/python2.7/site-packages/mod_python/apache.py”, line 637, in import_module
        module = imp.load_module(mname, f, p, d)
      File “/var/www/html/test/mptest.py”, line 5
          req.content_type=’text/plain’
        ^
    SyntaxError: invalid syntax

    Could you please help me?

    Thanks,
    Murali

  • Sattyam triapthi

    In src/Makefile:-
    Why do we need to add these lines and what is the use of adding these lines after echo and the color of remaining echo changes if we add these commands in between , do we need to write echo before these lines too?