How to run Dynamic Content with CGI Script on Apache Server | Oracle Solaris 11

0

CGI, the Common Gateway Interface defines a way for a webserver to interact with external content-generating programs, which execute an external program/script, typically to process user requests. It is a simple way to put dynamic content on your web site from any external resources.

We can configure the apache server to run CGI script and pre-viewing the dynamic content in webpage by following the below steps. Here I’ve used Oracle Solaris 11 operating system for testing the dynamic content with CGI script. You can choose any operating system that you prefer. To configure apache web server, you need to know the basic knowledge on Solaris OS.

Check the Oracle Solaris 11 OS info:

# cat /etc/release
    Oracle Solaris 11.4 X86
    Copyright (c) 1983, 2018, Oracle and/or its affiliates.  All rights reserved.
    Assembled 16 August 2018

# pkg info entire | grep Version
     Version: 11.4 (Oracle Solaris 11.4.0.0.1.15.0)
# uname -a
    SunOS webhost 5.11 11.4.0.15.0 i86pc i386 i86pc

To start configuring apache server need to enable apache service. By default apache service is disabled on Solaris 11 OS. Check and enable the service by using the below command:

# svcs -a | grep apache
    disabled       19:28:36 svc:/system/apache-stats-24:default
    disabled       22:38:55 svc:/network/http:apache24

# svcadm enable svc:/network/http:apache24

# svcs -a | grep apache
    disabled       19:28:36 svc:/system/apache-stats-24:default
    online         22:52:25 svc:/network/http:apache24

Now if you check the web server with ip address, you will shown the webserver is running and default webpage is loading. This default page is basically index.html file which default location is “/var/apache2/2.4/htdocs/”. Check the file location and file content from bash terminal using the below command:

# ls -lrt /var/apache2/2.4/htdocs/index.html
    -r--r--r--   1 root     bin           45 Aug 17  2018 /var/apache2/2.4/htdocs/index.html

# cat /var/apache2/2.4/htdocs/index.html
    <html><body><h1>It works!</h1></body></html>

Check the apache webserver which is working fine using web browser:
http://192.168.10.121/
Output: It works!

Default webpage of apache webserver

Now configure httpd.conf file for CGI content which will have in “cgi-bin” directory here. You may changes this directory to any location if you need. Just replace the directory (/var/apache2/2.4/cgi-bin) in configuration file. Noted that default web root directory and CGI content directory are different here. The CGI content directory must have permission to access the CGI content.

Uncomment the both !prefork & pefork directory’s LoadModule.
Also add “Options +ExecCGI” and “AddHandler cgi-script .cgi .pl” lines in cgi-bin directory.

Note: Keep a httpd configuration file backup before any changes.

# cd /etc/apache2/2.4/

# cp -p httpd.conf httpd.conf_original

# vim /etc/apache2/2.4/httpd.conf
.....Output Skipped......
<IfModule !mpm_prefork_module>
<IfDefine !prefork>
	# Must need to uncomment the below line.
        LoadModule cgid_module libexec/mod_cgid.so
</IfDefine>
</IfModule>

<IfModule mpm_prefork_module>
<IfDefine prefork>
	# Must need to uncomment the below line.
        LoadModule cgi_module libexec/mod_cgi.so
</IfDefine>

<Directory "/var/apache2/2.4/cgi-bin">
    AllowOverride None
    #Options None
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
    Require all granted
</Directory>
.....Output Skipped......

Create a new .cgi file in allowed cgi content directory and give the execution permission to the file. Write a test bash script contents and execute the file for test purpose to show the script file is working.

# cd /var/apache2/2.4/cgi-bin

# touch test.cgi

# chmod a+x test.cgi
# ls -l *.cgi
	-rwxr-xr-x   1 root     root           0 Aug  5 23:16 test.cgi

# vim test.cgi
	#!/bin/bash
	echo "Content-type: text/html\n\n"
	echo "Welcome! to First CGI Script."

# sh test.cgi
	Content-type: text/html
	Welcome! to First CGI Script.

Check the CGI Content using any web browser:

http://192.168.10.121/cgi-bin/test.cgi
Content-type: text/html

Welcome! to First CGI Script.

Dynamic Content Checkup:
Now add the below line in test.cgi file for dynamic content check-up which will collect the log and show the system information on the webpage.

# vim test.cgi
	#!/bin/bash
	echo "Content-type: text/html\n\n"
	echo "Welcome! to First CGI Script."
	echo ""
	echo "<meta http-equiv="refresh" content="10">"
	echo "<html><head><title>Apache CGI Test </title></head><body>"
	echo "<h3>Now Time: $(date)</h3>"
	echo ""
	echo "<h3>Now The Current User: $(netstat -nuv|grep .2006|wc -l) </h3>"
	echo ""
	echo "<b>Last 2 Min interval User Statics: $(cat /var/tmp/userlist)</b>"
	echo ""
	echo "<h3>Now Time: $(date)</h3>"
	echo ""
	echo "<h3>Application Server Memory (GB):</h3>"
	echo "<h3><b><pre>$(top | grep Memory) </pre></b></h3>"
	echo ""
	echo "<h3>ZFS List Info:</h3>"
	echo "<pre> $(zfs list) </pre>"
	echo ""
	echo "<h3>Logged in User:</h3>"
	echo "<pre> $(w) </pre>"
	echo ""
	echo "<h3>Application Server Uptime:</h3>"
	echo "<b><pre> $(uptime) </pre></b>"
	echo ""
	echo "<h3><b>Oracle EM - DC:</b></h3>"
	echo "<a href="https://127.0.0.1:7803/em/" "target=_blank">Oracle Enterprise Manager (OEM)</a>"
	echo ""
	echo "<h3>*** End Here ***</h3>"
	echo "</body></html>"

Make sure that the cgi script file is working from bash terminal:

# sh test.cgi
	Content-type: text/html
	
	Welcome! to First CGI Script.
	
	<meta http-equiv=refresh content=10>
	<html><head><title>Apache CGI Test </title></head><body>
	<h3>Now Time: Fri Aug  5 23:29:06 +06 2022</h3>
	
	<h3>Now The Current User: 0 </h3>
	
	<b>Last 2 Min interval User Statics: Joe Biden, Obama, Putin, Jinping</b>
	
	<h3>Now Time: Fri Aug  5 23:29:06 +06 2022</h3>
	
	<h3>Application Server Memory (GB):</h3>
	
	<h3><b><pre>Memory: 2048M phys mem, 486M free mem, 1024M total swap, 1024M free swap </pre></b></h3>
	
	<h3>ZFS List Info:</h3>
	<pre> NAME                               USED  AVAIL  REFER  MOUNTPOINT
	rpool                             5.11G  14.2G  4.32M  /rpool
	rpool/ROOT                        3.05G  14.2G    31K  none
	rpool/ROOT/solaris                3.05G  14.2G  2.66G  /
	rpool/ROOT/solaris/var             330M  14.2G   190M  /var
	rpool/VARSHARE                    44.7M  14.2G  11.0M  /var/share
	rpool/VARSHARE/kvol               27.7M  14.2G    31K  /var/share/kvol
	rpool/VARSHARE/kvol/dump_summary  1.22M  14.2G  1.02M  -
	rpool/VARSHARE/kvol/ereports      10.2M  14.2G  10.0M  -
	rpool/VARSHARE/kvol/kernel_log    16.2M  14.2G  16.0M  -
	rpool/VARSHARE/pkg                  63K  14.2G    32K  /var/share/pkg
	rpool/VARSHARE/pkg/repositories     31K  14.2G    31K  /var/share/pkg/repositories
	rpool/VARSHARE/sstore             5.85M  14.2G  5.85M  /var/share/sstore/repo
	rpool/VARSHARE/tmp                33.5K  14.2G  33.5K  /var/tmp
	rpool/VARSHARE/zones                31K  14.2G    31K  /system/zones
	rpool/dump                        1.00G  14.2G  1.00G  -
	rpool/export                       136K  14.2G    32K  /export
	rpool/export/home                  104K  14.2G    33K  /export/home
	rpool/export/home/moni            35.5K  14.2G  35.5K  /export/home/moni
	rpool/export/home/shyamal         35.5K  14.2G  35.5K  /export/home/shyamal
	rpool/swap                        1.00G  14.2G  1.00G  - </pre>
	
	<h3>Logged in User:</h3>
	<pre>  11:29pm  up  4:01,  2 users,  load average: 0.00, 0.00, 0.00
	User     tty           login@  idle   JCPU   PCPU  what
	root     pts/1        10:41pm                      w
	root     pts/2        11:23pm     2                -bash </pre>
	
	<h3>Application Server Uptime:</h3>
	<b><pre>  11:29pm  up  4:01,  2 users,  load average: 0.00, 0.00, 0.00 </pre></b>
	
	<h3><b>Oracle EM - DC:</b></h3>
	<a href=https://127.0.0.1:7803/em/ target=_blank>Oracle Enterprise Manager (OEM)</a>
	
	<h3>*** End Here ***</h3>
	</body></html>

Browser Output: http://192.168.10.121/cgi-bin/test.cgi
	Welcome! to First CGI Script.
	Now Time: Fri Aug 5 23:39:18 +06 2022
	Now The Current User: 0
	Last 2 Min interval User Statics: Joe Biden, Obama, Putin, Jinping
	Now Time: Fri Aug 5 23:39:18 +06 2022
	Application Server Memory (GB):
	Memory: 2048M phys mem, 482M free mem, 1024M total swap, 1024M free swap 
	ZFS List Info:
	NAME                               USED  AVAIL  REFER  MOUNTPOINT
	rpool                             5.10G  14.2G  4.32M  /rpool
	rpool/ROOT                        3.05G  14.2G    31K  none
	rpool/ROOT/solaris                3.05G  14.2G  2.66G  /
	rpool/ROOT/solaris/var             330M  14.2G   190M  /var
	rpool/VARSHARE                    44.9M  14.2G  11.1M  /var/share
	rpool/VARSHARE/kvol               27.7M  14.2G    31K  /var/share/kvol
	rpool/VARSHARE/kvol/dump_summary  1.22M  14.2G  1.02M  -
	rpool/VARSHARE/kvol/ereports      10.2M  14.2G  10.0M  -
	rpool/VARSHARE/kvol/kernel_log    16.2M  14.2G  16.0M  -
	rpool/VARSHARE/pkg                  63K  14.2G    32K  /var/share/pkg
	rpool/VARSHARE/pkg/repositories     31K  14.2G    31K  /var/share/pkg/repositories
	rpool/VARSHARE/sstore             5.87M  14.2G  5.87M  /var/share/sstore/repo
	rpool/VARSHARE/tmp                33.5K  14.2G  33.5K  /var/tmp
	rpool/VARSHARE/zones                31K  14.2G    31K  /system/zones
	rpool/dump                        1.00G  14.2G  1.00G  -
	rpool/export                       136K  14.2G    32K  /export
	rpool/export/home                  104K  14.2G    33K  /export/home
	rpool/export/home/moni            35.5K  14.2G  35.5K  /export/home/moni
	rpool/export/home/shyamal         35.5K  14.2G  35.5K  /export/home/shyamal
	rpool/swap                        1.00G  14.2G  1.00G  - 
	Logged in User:
	11:39pm  up  4:11,  2 users,  load average: 0.01, 0.00, 0.00
	User     tty           login@  idle   JCPU   PCPU  what
	root     pts/1        10:41pm     3                -bash
	root     pts/2        11:23pm    12                -bash 
	Application Server Uptime:
	11:39pm  up  4:11,  2 users,  load average: 0.01, 0.00, 0.00 
	Oracle EM - DC:
	Oracle Enterprise Manager (OEM)
	*** End Here ***

A few second interval browser will refresh the page and shown the system information.

Check the below images for best result:

You can also check the YouTube video: How to run Dynamic Content with CGI Script on Apache Server – Oracle Solaris 11

Thanks for visiting my website. If you have any query please comment and i will response as soon as possible.