Simple CGI and Apache examples on Ubuntu Linux

Introduction

CGI ( Common gateway Interface ) is an interface between Web client and the web server that runs your CGI script/program. CGI is a quite old and was largely superseded by different programing languages such as PHP, etc. However, it still can find its place in Linux system administrator’s hands as a quick tool for system monitoring and administration via web browser. This article describes in step-by-step manner how to run basic CGI scripts with various programming languages and scripts using Apache web server on Ubuntu Linux.

Install Apache

First we need to install apache web server. This is a fairly simple task and can be achieved with the apt-get command:

$ sudo apt-get install apache2

The above command will install Apache 2 web server with a default site located at: /etc/apache2/sites-available/default. When you open this default configuration site you will see that it is configured to run CGI scripts from /usr/lib/cgi-bin directory with URL alias /cgi-bin/:

 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">

Optionally, you can change your DNS settings to point to your new web server’s IP address or simply edit your local /etc/hosts file. For example:

10.1.1.61       cgi-example.local

Where 10.1.1.61 is an IP of your web server running apache and cgi-example.local is some arbitrary domain name.

Create CGI script

CGI works with multiple languages, but for now we start with bash shell. We will show examples for some other languages later. Here is a simples version of the CGI bash example:

#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash Example'

Copy the above code and paste it to a new file in /usr/lib/cgi-bin/ called example-bash.sh. Once done make the file executable using the chmod command:

$ sudo chmod 755 /usr/lib/cgi-bin/example-bash.sh


View CGI script

All what remains is to navigate with your browser to host-name or IP address of your web server. In our case the URL will be: http://cgi-example.local/cgi-bin/example-bash.sh

CGI bash example

You can edit this example to display a disk usage of server’s root partition “/”. You are only limited by your imagination:

#!/bin/bash
echo "Content-type: text/html"
echo ''
echo 'CGI Bash example<br>'
echo `df -h / | grep -v Filesystem`

The above code will check for free disk space for a root partition and produce the following page:

CGI bash example 2

More CGI examples

As promised, here are more CGI examples for a few more programming languages to get you started.

Perl

Create and make executable the following /usr/lib/cgi-bin/example-perl.pl with a content:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print <<htmlcode;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
htmlcode

Python

Create and make executable the following /usr/lib/cgi-bin/example-python.py with a content:

<html>
<head>
<title>CGI Python Example</title>
</head>
<body>
<h1>CGI Python Example</h1>
<p>CGI Python Example</p>
</body></html>
"""


C

For C and C++ to work we will need to have a compiler installed. First, install compiler with:

$ sudo apt-get install build-essential

Once installed create a file example-c.c with the following code:

#include <stdio.h>
int main(void)
{
printf("Content-Type: text/plain \n\n");
printf("CGI C Example \n");
}

save the content of example-c.c file and compile it with the following linux command:

$ sudo gcc -o /usr/lib/cgi-bin/example-c example-c.c

now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-c

C++

For C and C++ to work we will need to have a compiler installed. First, install compiler with:

$ sudo apt-get install build-essential

Once installed create a file example-cpp.c with a following code:

#include <iostream>
using namespace std;

int main()
{
cout << "content-type: text/html" << endl << endl;
cout << "<h1>CGI C++ example</h1>" << endl;
return 0;
}

save the content of example-cpp.c file and compile it with the following linux command:

$ sudo g++ -o /usr/lib/cgi-bin/example-cpp example-cpp.c

now you should be able to access your C compiled CGI script with: http://cgi-example.local/cgi-bin/example-cpp

Conclusion

As mentioned earlier the CGI is quite old and was largely superseded by different programing languages such as PHP, etc. However, as you can see, it is still relatively simple tool to use to automate your Linux administration tasks such as a remote custom monitoring for your Linux servers using a web browser.