Python CGI in Apache httpd server

Pre-requisite: Working Apache Server.

 

In httpd-vhosts.conf:

Add below content inside <VirtualHost …>

ScriptAlias /cgi-bin/ "/cgi-bin/" 
<directory "<httpd-installed-path="">/cgi-bin/">
         Options Indexes FollowSymLinks ExecCGI
         AddHandler cgi-script .cgi .py
         AllowOverride None
         Require all granted 

Now create a new file ‘test.py’ in /cgi-bin/  with content:

#!/usr/bin/env python

import cgi
cgi.test()

Make sure the below line is not commented in httpd.conf

LoadModule cgi_module modules/mod_cgi.so

Start/Restart apache server.

You can verify the loaded cgi module using the command:

sudo [path/]apachectl -M | grep cgi

Try below url in web-browser:

http://<ip/hostname>:/cgi-bin/test.py

eg:
http://localhost:1025/cgi-bin/test.py
http://localhost:80/cgi-bin/test.py

You will get a page with details on current working directory, command line arguments, etc..

apachectl status – How to solve lynx: command not found

apachectl status” will not work if you don’t have lynx ; text-based web browser.

You realize that when you get below message while using the command.

Error
bin/apachectl: line 95: lynx: command not found

Install the lynx and tell it to Apache httpd, he will surely understand.

Download the lynx from here. Change your working directory to the extracted folder and run the command:

./configure --prefix=<path-to-install-lynx>
make
make install

During lynx installation, it may stop with an error:

configure: error: No curses header-files found

Run the below command before proceeding:

sudo yum search ncurses-devel

Now it’s time to tell httpd about lynx.

In apachectl file (either in bin or conf directory), change the line starting with LYNX=”lynx -dump” to LYNX=”<lynx-installed-path>/lynx -dump”

apachectl status will start working..

Apache httpd: APR not found; error while installation

During Apache httpd installation if you get below error, that means Apache Portable Run-time environment was not found or not installed.

checking for APR... no
configure: error: APR not found.  Please read the documentation.

and/or

checking for APR-util... no
configure: error: APR-util not found. Please read the documentation.

So install APR and APR-util and give httpd those paths. Httpd will be happy to run for you after that… 🙂

Go further if you want to know how to install APR.

You can download APR and APR-util from here.

Then extract those to separate folders.

First change to the extract APR directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-path>

sudo make

sudo make install

So you have a working APR. Next is APR-util.

Change to the extract APR-Util directory and install it. (Tip: cd command to change directory)

sudo ./configure --prefix=<apr-util-path> --with-prefix=<apr-path>

sudo make

sudo make instal

Install zlib packages with the command (optional; you can turn this off at your will):

yum install zlib zlib-devel

Apart from APR and APR-Util, you may need to install pcre too. So download it from here first.

Change to the directory and use the below commands to install.

sudo ./configure --prefix=<pcre-path>

sudo make

sudo make install

Now lets make httpd understand that we have a working pcre, APR and APR-util.

./configure --enable-file-cache --enable-cache --enable-disk-cache --enable-mem-cache --enable-deflate --enable-expires --enable-headers --enable-usertrack --enable-cgi --enable-vhost-alias --enable-rewrite --enable-so --prefix=<httpd-path> --with-apr=<apr-path> --with-apr-util=<apr-util-path> --with-pcre=<pcre-path>

# Rest is history make make install